Shudhanshu Singh
ee10ae168c
perf(emoji): optimize FindEmojiSubmatchIndex using slice-based Trie (#38573)
This pull request optimizes `FindEmojiSubmatchIndex` in Gitea's emoji
package (`modules/emoji/emoji.go`) by replacing the
`strings.Replacer`-based search with a slice-based trie and a
constant-time starting-byte check (`isStartingByte`).
The new implementation avoids heap allocations during the search and
reduces CPU overhead when rendering Markdown, particularly for plain
text that does not contain emojis.
### Verification
Verified with unit tests:
```sh
go test -count=1 ./modules/emoji/...
```
Benchmarks:
```sh
go test -bench=. -benchmem ./modules/emoji/...
```
### Results
| Benchmark | Before | After |
| ---------- | ------ | ----- |
| `BenchmarkFindEmojiSubmatchIndex` | 168.3 ns/op, 2 allocs/op | 85.78
ns/op, 1 alloc/op |
| `BenchmarkFindEmojiSubmatchIndexNoMatch` | 239.8 ns/op, 1 alloc/op |
105.1 ns/op, 0 allocs/op |
### Benchmark Output
```text
goos: linux
goarch: amd64
pkg: gitea.dev/modules/emoji
cpu: Intel(R) Core(TM) i5-7400 CPU @ 3.00GHz
BenchmarkFindEmojiSubmatchIndex-4 13498539 85.78 ns/op 16 B/op 1 allocs/op
BenchmarkFindEmojiSubmatchIndexNoMatch-4 11220450 105.1 ns/op 0 B/op 0 allocs/op
BenchmarkFindEmojiSubmatchIndexOld-4 6569360 168.3 ns/op 48 B/op 2 allocs/op
BenchmarkFindEmojiSubmatchIndexOldNoMatch-4 5026116 239.8 ns/op 32 B/op 1 allocs/op
```
---------
Signed-off-by: Sudhanshu Singh <sudhanshuwriterblc@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
2026-07-23 08:29:28 +00:00
..
2026-07-21 06:43:45 +00:00
2026-02-01 10:35:51 +00:00
2026-05-26 15:49:31 -07:00
2026-07-12 17:14:09 +00:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2025-04-09 16:34:38 +00:00
2026-05-26 15:49:31 -07:00
2026-03-31 17:22:18 +00:00
2026-06-08 18:18:58 +00:00
2024-11-11 04:07:54 +08:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-07-23 08:29:28 +00:00
2026-05-26 15:49:31 -07:00
2026-07-15 17:30:01 +00:00
2026-06-08 18:18:58 +00:00
2026-07-23 01:22:26 +00:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-07-12 17:14:09 +00:00
2026-06-14 15:52:37 +02:00
2026-06-04 00:12:02 +08:00
2026-06-04 00:12:02 +08:00
2026-07-20 16:07:38 +00:00
2026-07-17 18:44:31 +08:00
2026-04-14 14:00:20 +00:00
2026-05-29 01:12:11 +00:00
2026-07-20 16:07:38 +00:00
2026-07-17 22:28:59 +08:00
2026-05-26 15:49:31 -07:00
2026-07-08 23:52:06 +02:00
2026-07-21 13:41:54 +00:00
2026-05-26 15:49:31 -07:00
2026-05-29 01:12:11 +00:00
2026-05-26 15:49:31 -07:00
2026-05-29 01:12:11 +00:00
2026-05-26 15:49:31 -07:00
2026-07-12 17:14:09 +00:00
2026-06-03 17:40:38 +00:00
2026-07-17 22:28:59 +08:00
2026-07-17 22:28:59 +08:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-05-28 06:14:52 +00:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-07-08 23:52:06 +02:00
2026-05-26 15:49:31 -07:00
2026-07-23 01:22:26 +00:00
2026-05-26 15:49:31 -07:00
2025-02-21 00:05:40 +08:00
2026-05-26 15:49:31 -07:00
2026-07-16 17:38:33 +00:00
2026-06-14 18:26:22 +00:00
2026-07-04 22:30:12 +02:00
2026-07-07 07:32:25 +00:00
2026-07-23 14:49:52 +08:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2025-01-21 18:57:07 +00:00
2026-05-26 15:49:31 -07:00
2026-07-20 18:03:28 +00:00
2026-07-22 20:14:18 +00:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-05-26 15:49:31 -07:00
2026-07-12 11:33:14 +00:00
2026-03-26 00:37:48 +08:00
2026-05-26 15:49:31 -07:00
2026-07-12 17:14:09 +00:00
2025-01-09 09:21:47 +08:00
2026-07-23 08:29:28 +00:00
2026-05-26 15:49:31 -07:00
2026-07-10 16:39:01 +00:00
2026-02-20 16:12:22 +00:00
2026-06-04 13:38:56 +00:00