Skip to content

profile: avoid unused label map allocations and clear labelX in postDecode - #1064

Merged
nolanmar511 merged 1 commit into
google:mainfrom
aalexand:postdecode-labels
Nov 5, 2026
Merged

nolanmar511 merged 1 commit into
google:mainfrom
aalexand:postdecode-labels

Conversation

@aalexand

@aalexand aalexand commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

For every sample with labels, postDecode previously allocated all three label maps (Label, NumLabel, and NumUnit) sized to len(s.labelX), assigned an empty NumUnit map even when numeric labels carried no units, and never cleared the raw s.labelX slice after populating the maps.

Count string, numeric, and unit entries first so only non-empty maps are allocated with appropriate capacity hints, leave s.NumUnit nil when no units are present, and clear s.labelX consistently with the other *X fields in postDecode.

name                                old time/op         new time/op         delta
ParseLabels/num_label-64                   35.5ms ± 4%         32.8ms ± 5%   -7.54%  (p=0.009 n=6+6)
ParseLabels/num_label_with_unit-64         41.9ms ± 2%         39.7ms ± 5%   -5.13%  (p=0.009 n=5+6)
ParseLabels/string_label-64                35.5ms ± 4%         34.4ms ± 4%     ~     (p=0.132 n=6+6)
ParseLabels/many_labels-64                 80.7ms ± 2%         62.3ms ± 3%  -22.86%  (p=0.002 n=6+6)

name                                old inuse-alloc/op  new inuse-alloc/op  delta
ParseLabels/num_label-64                   20.1MB ± 0%         18.4MB ± 0%   -8.14%  (p=0.000 n=5+6)
ParseLabels/num_label_with_unit-64         27.6MB ± 0%         26.9MB ± 0%   -2.37%  (p=0.008 n=5+5)
ParseLabels/string_label-64                19.3MB ± 0%         18.6MB ± 0%   -3.39%  (p=0.002 n=6+6)
ParseLabels/many_labels-64                 70.9MB ± 0%         28.2MB ± 0%  -60.16%  (p=0.002 n=6+6)

name                                old alloc/op        new alloc/op        delta
ParseLabels/num_label-64                   33.9MB ± 0%         31.9MB ± 0%   -5.79%  (p=0.002 n=6+6)
ParseLabels/num_label_with_unit-64         41.4MB ± 0%         40.4MB ± 0%   -2.37%  (p=0.002 n=6+6)
ParseLabels/string_label-64                34.1MB ± 0%         32.1MB ± 0%   -5.76%  (p=0.002 n=6+6)
ParseLabels/many_labels-64                 94.5MB ± 0%         62.3MB ± 0%  -34.06%  (p=0.004 n=5+6)

name                                old allocs/op       new allocs/op       delta
ParseLabels/num_label-64                     297k ± 0%           257k ± 0%  -13.74%  (p=0.002 n=6+6)
ParseLabels/num_label_with_unit-64           338k ± 0%           318k ± 0%   -6.04%  (p=0.002 n=6+6)
ParseLabels/string_label-64                  297k ± 0%           257k ± 0%  -13.74%  (p=0.002 n=6+6)
ParseLabels/many_labels-64                   706k ± 0%           543k ± 0%  -23.15%  (p=0.026 n=5+6)

…ecode

For every sample with labels, `postDecode` previously allocated all three
label maps (`Label`, `NumLabel`, and `NumUnit`) sized to `len(s.labelX)`,
assigned an empty `NumUnit` map even when numeric labels carried no units,
and never cleared the raw `s.labelX` slice after populating the maps.

Count string, numeric, and unit entries first so only non-empty maps are
allocated with appropriate capacity hints, leave `s.NumUnit` nil when no
units are present, and clear `s.labelX` consistently with the other `*X`
fields in `postDecode`.

```
name                                old time/op         new time/op         delta
ParseLabels/num_label-64                   35.5ms ± 4%         32.8ms ± 5%   -7.54%  (p=0.009 n=6+6)
ParseLabels/num_label_with_unit-64         41.9ms ± 2%         39.7ms ± 5%   -5.13%  (p=0.009 n=5+6)
ParseLabels/string_label-64                35.5ms ± 4%         34.4ms ± 4%     ~     (p=0.132 n=6+6)
ParseLabels/many_labels-64                 80.7ms ± 2%         62.3ms ± 3%  -22.86%  (p=0.002 n=6+6)

name                                old inuse-alloc/op  new inuse-alloc/op  delta
ParseLabels/num_label-64                   20.1MB ± 0%         18.4MB ± 0%   -8.14%  (p=0.000 n=5+6)
ParseLabels/num_label_with_unit-64         27.6MB ± 0%         26.9MB ± 0%   -2.37%  (p=0.008 n=5+5)
ParseLabels/string_label-64                19.3MB ± 0%         18.6MB ± 0%   -3.39%  (p=0.002 n=6+6)
ParseLabels/many_labels-64                 70.9MB ± 0%         28.2MB ± 0%  -60.16%  (p=0.002 n=6+6)

name                                old alloc/op        new alloc/op        delta
ParseLabels/num_label-64                   33.9MB ± 0%         31.9MB ± 0%   -5.79%  (p=0.002 n=6+6)
ParseLabels/num_label_with_unit-64         41.4MB ± 0%         40.4MB ± 0%   -2.37%  (p=0.002 n=6+6)
ParseLabels/string_label-64                34.1MB ± 0%         32.1MB ± 0%   -5.76%  (p=0.002 n=6+6)
ParseLabels/many_labels-64                 94.5MB ± 0%         62.3MB ± 0%  -34.06%  (p=0.004 n=5+6)

name                                old allocs/op       new allocs/op       delta
ParseLabels/num_label-64                     297k ± 0%           257k ± 0%  -13.74%  (p=0.002 n=6+6)
ParseLabels/num_label_with_unit-64           338k ± 0%           318k ± 0%   -6.04%  (p=0.002 n=6+6)
ParseLabels/string_label-64                  297k ± 0%           257k ± 0%  -13.74%  (p=0.002 n=6+6)
ParseLabels/many_labels-64                   706k ± 0%           543k ± 0%  -23.15%  (p=0.026 n=5+6)
```
@aalexand aalexand changed the title profile: avoid unused label map allocations and clear labelX in postD… profile: avoid unused label map allocations and clear labelX in postDecode Nov 5, 2026
@codecov-commenter

codecov-commenter commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.55%. Comparing base (27c4d34) to head (03186a9).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1064      +/-   ##
==========================================
+ Coverage   73.50%   73.55%   +0.05%     
==========================================
  Files          46       46              
  Lines        7941     7957      +16     
==========================================
+ Hits         5837     5853      +16     
  Misses       2082     2082              
  Partials       22       22              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nolanmar511
nolanmar511 merged commit 4bf881d into google:main Nov 5, 2026
22 checks passed
@aalexand
aalexand deleted the postdecode-labels branch November 9, 2026 22:45
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.

5 participants