Files
gitea/modules
Wei-Lun Liandsilverwind 901859a2e2 perf(highlight): reuse one chroma HTML formatter (#40418)
`RenderCodeByLexer` created a new chroma HTML formatter on every call,
so the formatter rebuilt the CSS map of the style each time. That runs
for every fenced code block in markdown (since
https://github.com/go-gitea/gitea/pull/39751) and for every line when
the diff view highlights line by line (files over 256 KiB, or after the
full-file highlight time limit). The formatter caches that map behind a
mutex and is otherwise read-only, so one shared instance is enough.
Output is unchanged.

Local Go benchmarks, i7-9700 on Windows 11, base and patched run
alternately 6 times, medians:

| | before | after |
|---|---|---|
| 1000 lines of `services/gitdiff/gitdiff.go`, one call per line | 115.3
ms, 802k allocs | 40.8 ms, 180k allocs |
| 3-line shell code block | 142 µs, 889 allocs | 66 µs, 267 allocs |
| Markdown render of README + CONTRIBUTING + 3 docs (45 KB, 31 code
blocks) | 16.0 ms, 63.8k allocs | 14.4 ms, 45.8k allocs |

This is separate from https://github.com/go-gitea/gitea/pull/40394,
which caches highlighted lines but still creates the formatter on each
call.

Assisted-by: Claude Code:claude-opus-5-5

---------

Co-authored-by: silverwind <me@silverwind.io>
2026-11-18 08:20:24 +00:00
..
2026-10-24 13:48:19 +00:00
2026-10-24 13:48:19 +00:00
2026-10-08 00:33:55 +08:00
2026-08-26 19:32:44 +00:00
2026-07-12 17:14:09 +00:00
2026-10-24 13:48:19 +00:00