Restore fast variant.Marshal and Encode for scalars, short strings and empty containers
perfloop-oss/parquet-go · Allocation In Hot Loop
https://perfloop.ai/t/oss/case_efvnk6yp9h
Verdict
VERIFIED · settled 2026-11-15 · merged as parquet-go/parquet-go#638
What happened: The paired measurements met the required improvement.
Hypothesis
Marshal uses the pooled encoder even for scalars and short strings. Encode also uses the pool for empty arrays and objects. The submitter reports that this makes small calls slower after PR #550, despite improvements for large inputs. The reported timing comparisons remain unconfirmed, and the consumer input mix, call rate and overall savings are unknown.
The source subject is Marshal at variant/marshal.go:15-31, already the entry of an accepted public library-operation path. It passes &b to getEncoder before inspecting the input, copies out the encoded value, releases the encoder and builds metadata. getEncoder stores that pointer in encoder.b and calls sync.Pool.Get; releaseEncoder clears the pointer and calls Put (variant/encoding.go:168-233). A focused go build -gcflags=-m ./variant succeeded here with Go 1.27.3 on linux/amd64 and reported variant/marshal.go:16:6: moved to heap: b. This confirms the compiler diagnostic, not the submitter's timing or allocation totals. Encode acquires the pool before reaching either composite encoder, without an empty-container check (variant/encoding.go:18-49). Its primitive branch already bypasses the pool; the requested scalar repair concerns Marshal, alongside the empty-array and empty-object repair in Encode.
The submitter attributes the regression to upstream PR #550, merged as 3564a3f3f6ac. They report interleaved benchstat pairs against base 91684bba5200: Marshal(int64(7)) 67.5 -> 90.3 ns/op (+34%, 2 -> 3 allocs/op), short string 64.2 -> 80.3 ns/op (+25%), and Encode empty array 18.7 -> 48.8 ns/op (+162%), all p=0.000. They also report large inputs are 22-80% faster and require those gains to remain. These are attributed, unconfirmed reports to reproduce, not results established by this run. The exact short-string input and the original benchmark environment were not supplied. The shallow checkout lacks the historical commit objects, so the pre-#550 baseline was not rebuilt here.
The requested outcome includes both non-pooled encoding for values that are not arrays, objects or structs, eliminating the MetadataBuilder escape on those small paths, and direct fixed encodings for empty arrays and objects. It must retain #550's encoder for large inputs and byte-identical output. This is an internal optimization of the existing Marshal and Encode APIs, not a replacement API. The allocation and pooling mechanism is architecture-independent; the compiler host above is provenance, not a hardware requirement.
Upstream https://github.com/parquet-go/parquet-go/pull/550 is confirmed merged as 3564a3f3f6acda6cb3e2298cf50c802bf4d2aa92, with checked base 91684bba52007d392dc0616b6571ef09bb317847 and head c302741d3a2258cdf3779bfb7395335c0a3fd3cf. Its patch introduces the pooled reflect-walk encoder and representative-shape benchmarks. This proposal preserves that work rather than reverting its large-input optimization. The complete current open-PR listing also exposed https://github.com/parquet-go/parquet-go/pull/554, a draft at head 9b6c953b118765987daa587a833f0cbe43bee602. Its inspected encoding.go, marshal_test.go and metadata.go hunks overlap metadata handling and comments but do not implement these scalar or empty-container bypasses. Its discussion describes a rebase, not an accepted replacement decision. Other #554 hunks were not reviewed, and the inspected PR records supplied no linked issue about this small-input regression; this is not upstream-wide clearance.
The smallest useful next check is a result-consuming benchmark and allocation test for unchanged Marshal(int64(7)), short strings, and empty arrays and objects, followed by interleaved baseline/proposed pairs analyzed with benchstat. Both APIs and both empty container kinds remain in scope. Also run the PR's existing BenchmarkMarshalRepresentativeShapes: Int64Slice(1000), StringSlice(1000), MixedSlice(300), NestedMap(100) and LargeObject(300) (variant/benchmark_test.go:26-51,83-129). Compare with the historical base and the current pooled encoder to check restoration of small-input performance without losing the reported large-input gains. Confirm byte-identical metadata and value output and use an allocation assertion plus escape analysis to test the claimed allocation removal. Faster isolated calls would not establish production prevalence or aggregate customer savings.
Change to test: Encode values that are not arrays, objects or structs without the pooled encoder so MetadataBuilder no longer escapes, and return fixed encodings for empty arrays and objects directly. Retain the #550 large-input encoder, its large-input gains and byte-identical output, with interleaved pairs on scalar, short-string and empty inputs and the PR's representative shapes, plus an allocation test.
Where it lives
perfloop-oss/parquet-go · parquet.go
Evidence
BenchmarkMarshalSmallValues/Int64 · 19 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
129.3 |
52.22 |
−59% (−76.33) |
−87.74 to −65.85 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated |
PASSED |
B/op |
88 |
16 |
−81.8% (−72) |
−72 to −72 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
3 |
2 |
−33.3% (−1) |
−1 to −1 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated; base median within 3 to +∞ |
PASSED |
BenchmarkMarshalSmallValues/ShortString · 19 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
120.6 |
53.59 |
−56.7% (−68.39) |
−79.36 to −55.08 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated |
PASSED |
B/op |
80 |
10 |
−87.5% (−70) |
−70 to −70 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
3 |
2 |
−33.3% (−1) |
−1 to −1 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated |
PASSED |
BenchmarkMarshalSmallValues/EmptyArray · 19 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
144.7 |
69.99 |
−51.4% (−74.4) |
−83.3 to −70.03 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated |
PASSED |
B/op |
80 |
6 |
−92.5% (−74) |
−74 to −74 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
3 |
2 |
−33.3% (−1) |
−1 to −1 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated |
PASSED |
BenchmarkMarshalSmallValues/EmptyObject · 19 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
143.5 |
72.09 |
−51.1% (−73.3) |
−83.1 to −60.29 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated |
PASSED |
B/op |
80 |
6 |
−92.5% (−74) |
−74 to −74 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
3 |
2 |
−33.3% (−1) |
−1 to −1 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated |
PASSED |
BenchmarkEncodeEmptyContainers/Array · 19 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
46.1 |
12.3 |
−73.5% (−33.89) |
−35.9 to −30.16 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated |
PASSED |
B/op |
3 |
3 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
1 |
1 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
BenchmarkEncodeEmptyContainers/Object · 19 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
44.82 |
11.23 |
−74.9% (−33.59) |
−36.42 to −32.43 |
gain > 0; gain > 0.05 × |baseline median|; arm medians separated |
PASSED |
B/op |
3 |
3 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
1 |
1 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
BenchmarkMarshalRepresentativeShapes/Int64Slice · 19 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
22014 |
22099 |
+44 |
−1820 to +646 |
loss ≤ 0.05 × |baseline median| |
PASSED |
B/op |
12373 |
12376 |
+1 |
−7 to +13 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
3 |
3 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
BenchmarkMarshalRepresentativeShapes/StringSlice · 19 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
35135 |
35112 |
+82 |
−3249 to +1299 |
loss ≤ 0.05 × |baseline median| |
PASSED |
B/op |
49376 |
49392 |
+8 |
−66 to +96 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
3 |
3 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
BenchmarkMarshalRepresentativeShapes/MixedSlice · 49 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
214327 |
215371 |
+2608 |
−7682 to +9246 |
loss ≤ 0.05 × |baseline median| |
PASSED |
B/op |
83760 |
83769 |
+5 |
−39 to +47 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
2710 |
2710 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
BenchmarkMarshalRepresentativeShapes/NestedMap · 34 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
82827 |
82780 |
+245 |
−5402 to +3512 |
loss ≤ 0.05 × |baseline median| |
PASSED |
B/op |
27593 |
27594 |
+2 |
−12 to +20 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
534 |
534 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
BenchmarkMarshalRepresentativeShapes/LargeObject · 19 sample pairs
| metric | baseline | candidate | paired median change | confidence range | required | result |
|---|---|---|---|---|---|---|
ns/op |
171416 |
170350 |
−921 |
−12255 to +7238 |
loss ≤ 0.05 × |baseline median| |
PASSED |
B/op |
154412 |
154531 |
+69 |
−350 to +608 |
loss ≤ 0.05 × |baseline median| |
PASSED |
allocs/op |
641 |
641 |
0 |
0 to 0 |
loss ≤ 0.05 × |baseline median| |
PASSED |
Checks: 6 of 6 passed. Verification: no defect found.