Skip to content

perf: restore preformatted blocks in one pass - #80

Merged
hownowstephen merged 1 commit into
mainfrom
perf_pre_restore
Sep 23, 2026
Merged

hownowstephen merged 1 commit into
mainfrom
perf_pre_restore

Conversation

@hownowstephen

Copy link
Copy Markdown
Contributor

Each <pre> block was put back with strings.Replace(wrapped, prePlaceholder, block, 1). Every call scanned from the start and copied the whole text, so k blocks cost k full scans and k copies. restorePre now walks forward once with strings.Cut into a builder sized for the text plus the blocks.

Measured on documents of paragraphs, each followed by a <pre> block:

blocks     sec/op                  B/op                      allocs/op
2          9.41µ →  9.41µ    ~     14.7Ki →  14.1Ki   -4.7%   40 →  39
20        103.6µ →  86.1µ  -17%    268Ki  →  118Ki   -56%    215 → 196
200        2.16ms → 0.84ms -61%    15.0Mi →  1.0Mi   -93%    1,848 → 1,649

Most mail has no <pre> blocks, and the corpus documents are unchanged in both output and timing. The gain is on code-heavy mail such as release notes and developer newsletters. The benchmark was an ad hoc one and is not committed.

TestManyPreformattedBlocks checks that 50 blocks come back in order. Output is byte-for-byte identical to main across the corpus under every link style.

@hownowstephen
hownowstephen marked this pull request as ready for review September 23, 2026 04:18
@hownowstephen
hownowstephen merged commit 1cffa2e into main Sep 23, 2026
2 checks passed
@hownowstephen
hownowstephen deleted the perf_pre_restore branch September 23, 2026 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant