Commit 099a9d7
authored
stats: optimize StatName hot paths in symbol table (envoyproxy#44448)
Optimizations to StatName to make it more friendly for use as a key in a
map. Specifically we improve `operator==` and similar calls and clean up
some duplicate unnecessary work across the file.
(nb: if preferred i can resubmit the commits as separated PRs, do let me
know)
Key changes:
- Inline `dataSize()`, `decodeNumber()`, `encodingSizeBytes()` into the
header to make inlining easier
- Fast-path `decodeNumber()` for values <= 127 - skip loop for values
with length less than 127
- Branch-free `encodingSizeBytes()` via `std::countl_zero`
- `StatNameList::front()` + `MetricHelper::statName()` bypass - avoid
`std::function` allocation and iterator setup for the common "get first
element"
- `operator==` fast-path pointer-identity check and early size
comparison before `memcmp`; `AbslHashValue` also avoids redundant varint
decode
- `empty()` checks first byte instead of full varint decode - `0x00`
uniquely encodes zero length (`d040567`)
Cumulative benchmark results (`--benchmark_min_time=5s`):
| Benchmark | Before | After | Improvement |
|---|---|---|---|
| `BM_StatsNoTls` | 5697 us | 5077 us | **-10.9%** |
| `BM_StatsWithTls` | 6023 us | 5453 us | **-9.5%** |
| `BM_StatsWithTlsAndRejectionsWithDot` | 2889 us | 2715 us | **-6.1%**
|
| `BM_StatsWithTlsAndRejectionsWithoutDot` | 5486 us | 4866 us |
**-11.3%** |
| `bmCreateRace` | 49.2 ms | 49.7 ms | ~flat |
| `bmJoinStatNames` | 101 ns | 87.6 ns | **-13.3%** |
| `bmJoinElements` | 233 ns | 208 ns | **-10.7%** |
| `bmCompareElements` | 34.3 ns | 33.5 ns | **-2.3%** |
| `bmSortByStatNames` | 49976 us | 50547 us | ~flat |
| `bmStdSort` | 60993 us | 56058 us | **-8.1%** |
| `bmStatNameMapLookup` | 22.4 ns | 8.52 ns | **-62.0%** |
| `bmStatNameMapLookupDifferentPointer` | 22.4 ns | 11.8 ns | **-47.3%**
|
| `bmStatNameMapLookupMiss` | 12.1 ns | 6.70 ns | **-44.6%** |
| `bmSortStrings` | 20443 us | 20488 us | ~flat |
| `bmSetStrings` | 38079 us | 38100 us | ~flat |
Optimizations were developed and benchmarked incrementally (see
individual commit messages). The map-lookup benchmarks were added in the
first commit to establish a baseline.
Generative AI (Claude) was used as a development aid for this change. I
understand all code in the PR.
Risk Level: Medium - all changes are to internal implementation details
with no API or behavioral changes. The varint encoding format is
unchanged. But, it is a core component.
Testing:
- Existing `bazel test //test/common/stats/...` passes incl config=asan.
- New `StatNameEqualityFastPaths` test covers pointer-identity,
null-vs-zero-length, and null-vs-non-empty cases
- Benchmarked with `symbol_table_benchmark` and
`thread_local_store_speed_test`
Docs Changes: N/A
Release Notes: N/A
Platform Specific Features: N/A
---------
Signed-off-by: Jason Koch <jkoch@netflix.com>1 parent 3f5a818 commit 099a9d7
5 files changed
Lines changed: 696 additions & 67 deletions
File tree
- source/common/stats
- test/common/stats
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
| 35 | + | |
43 | 36 | | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
| 37 | + | |
62 | 38 | | |
63 | 39 | | |
64 | 40 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | 17 | | |
25 | 18 | | |
26 | 19 | | |
| |||
31 | 24 | | |
32 | 25 | | |
33 | 26 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | 27 | | |
42 | 28 | | |
43 | 29 | | |
| |||
105 | 91 | | |
106 | 92 | | |
107 | 93 | | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | 94 | | |
120 | 95 | | |
121 | 96 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
| 195 | + | |
| 196 | + | |
195 | 197 | | |
196 | | - | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
197 | 220 | | |
198 | 221 | | |
199 | 222 | | |
| |||
587 | 610 | | |
588 | 611 | | |
589 | 612 | | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
590 | 621 | | |
591 | 622 | | |
592 | 623 | | |
| |||
595 | 626 | | |
596 | 627 | | |
597 | 628 | | |
598 | | - | |
| 629 | + | |
599 | 630 | | |
600 | 631 | | |
601 | 632 | | |
| |||
624 | 655 | | |
625 | 656 | | |
626 | 657 | | |
627 | | - | |
| 658 | + | |
| 659 | + | |
628 | 660 | | |
629 | 661 | | |
630 | 662 | | |
| |||
635 | 667 | | |
636 | 668 | | |
637 | 669 | | |
638 | | - | |
639 | | - | |
640 | | - | |
641 | | - | |
| 670 | + | |
642 | 671 | | |
643 | 672 | | |
644 | 673 | | |
645 | 674 | | |
646 | 675 | | |
647 | 676 | | |
648 | 677 | | |
649 | | - | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
650 | 683 | | |
651 | 684 | | |
652 | 685 | | |
| |||
660 | 693 | | |
661 | 694 | | |
662 | 695 | | |
663 | | - | |
664 | | - | |
665 | | - | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
666 | 699 | | |
667 | 700 | | |
668 | | - | |
669 | | - | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
670 | 706 | | |
671 | 707 | | |
672 | 708 | | |
| |||
863 | 899 | | |
864 | 900 | | |
865 | 901 | | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
866 | 923 | | |
867 | 924 | | |
868 | 925 | | |
| |||
0 commit comments