Skip to content

perf: precompute preview highlight state#106

Merged
sorafujitani merged 1 commit into
mainfrom
perf/preview-precompute-highlight
Jul 3, 2026
Merged

perf: precompute preview highlight state#106
sorafujitani merged 1 commit into
mainfrom
perf/preview-precompute-highlight

Conversation

@sorafujitani

Copy link
Copy Markdown
Owner

Summary

  • build preview colors and highlight regexp once per text render
  • pass precomputed render state into each message write
  • keep invalid regex fallback silent and unhighlighted
  • add a highlight-render benchmark so the hot path is measured directly

Fixes #101

Benchmark

Command:

go test -bench=BenchmarkRenderText -benchmem ./internal/preview -count=10

benchstat /tmp/ccsession-101-before.txt /tmp/ccsession-101-after.txt:

RenderTextLargeTranscript-12              2.757m ± 1%   2.756m ± 2%       ~ (p=0.853 n=10)
RenderTextHighlightLargeTranscript-12     2.858m ± 2%   2.856m ± 1%       ~ (p=0.739 n=10)

RenderTextLargeTranscript-12              1.927Mi ± 0%   1.927Mi ± 0%  +0.00% (p=0.000 n=10)
RenderTextHighlightLargeTranscript-12     2.012Mi ± 0%   1.955Mi ± 0%  -2.83% (p=0.000 n=10)

RenderTextLargeTranscript-12              17.26k ± 0%   17.26k ± 0%       ~ (p=1.000 n=10)
RenderTextHighlightLargeTranscript-12     18.00k ± 0%   17.50k ± 0%  -2.74% (p=0.000 n=10)

Wall time is dominated by transcript loading in this benchmark, but the highlight path now avoids per-message regexp compilation and drops allocations.

Verification

go test ./...
go vet ./...
golangci-lint run
gofmt -l $(git ls-files '*.go')
git diff --check

@sorafujitani sorafujitani merged commit 37c5099 into main Jul 3, 2026
7 checks passed
@sorafujitani sorafujitani deleted the perf/preview-precompute-highlight branch July 3, 2026 05:39
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.

perf: build highlight regex and color table once per preview render

1 participant