Repository navigation
Avoid repeated union-splitting on oneof fields in generated encode methods - #316
Conversation
…thods
Non-parametrized oneof fields have a non-concrete type such as
`Union{Nothing,OneOf{<:Union{Int32,String}}}`. The generated `PB.encode`
and `PB._encoded_size` methods read `x.<oneof>` in every branch and call
`isnothing` on it, each of which makes Julia union-split with a runtime
`isa(v, OneOf{<:Union{...}})` check that goes through the general (slow)
subtyping algorithm.
Read the oneof field once into a local inside a `let` block, test it
with the `===` builtin and compare against its `name` bound once.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #316 +/- ##
==========================================
+ Coverage 93.59% 94.19% +0.60%
==========================================
Files 25 25
Lines 3093 3120 +27
==========================================
+ Hits 2895 2939 +44
+ Misses 198 181 -17 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@quinnj Are you the right person to have a look at this? Thank you! |
There was a problem hiding this comment.
[review generated via AI] Thanks @bachdavi, this looks right and it measurably helps.
Correctness. After if oneof === nothing, Julia narrows the local oneof to the OneOf{...} part of the union without a runtime isa. So oneof.name and oneof[] are plain calls with no subtype check, which is the slow path you saw in your profile. Each branch still sees the same value as before, and the output bytes are identical (checked below).
Measured on Julia 1.12.6, with a message that has an 8-way oneof (int64/int32/uint64/uint32/double/float/bool/bytes) and a repeated field of 80,000 of them:
| median encode | |
|---|---|
| master | 6.4 s |
| this PR | 6.5 s |
master + the truncate fix below |
23 ms |
this PR + the truncate fix below |
4.6 ms |
The first two rows are dominated by a separate problem I found while benchmarking. On Julia 1.12+, _with_size grows the buffer with truncate, and Base's truncate copies the whole buffer each time it grows it, so encoding a repeated field is quadratic in its length. I'll open a separate PR for that. With it in place, this PR makes encoding about 5x faster on this workload.
One non-blocking thought: the generated let introduces the locals oneof and oneof_name. A oneof member whose Julia type name is literally oneof would be shadowed inside the let. That's very unlikely, so I don't think it's worth changing.
🤖 Generated with Claude Code
Problem
With the default
parametrize_oneofs=false, oneof fields are generated with a non-concrete type, e.g.and the generated
PB.encode/PB._encoded_sizemethods look likeEvery
x.valueread followed by a call (includingisnothing(...)—v === nothingis a builtin and does not have this problem) makes Julia union-splitUnion{Nothing,OneOf{<:Union{...}}}, i.e. emit a runtimeisa(v, OneOf{<:Union{...}})check. That check goes through the general subtyping algorithm (ijl_subtype_env/_forall_exists_subtype) and is very slow. In a profile of a real workload ~77% of serialization time was spent there.Fix
Read the oneof field once into a local, test it with
===, and compare against itsnamebound once. The locals live in aletblock so they can't clash with other generated code or other oneofs in the same message:Same for
PB._encoded_size. The generated code is the same forparametrize_oneofs=trueandfalse(andforce_requiredoneofs, where the=== nothingcheck is just constant-false).Numbers (Julia 1.10.4)
On 1M values (half
Int64, half string) of aPrimitiveValue-like message:parametrize_oneofs=false)parametrize_oneofs=true(before this change)parametrize_oneofs=false)Output is byte-identical. A smaller standalone benchmark (7-variant oneof, 1M values) with this branch vs
master: encode 1293 → 74 ns/value,_encoded_size1226 → 27 ns/value, identical bytes.Notes
!isnothing(x.f) && PB.encode(e, n, x.f). For the usualUnion{Nothing,B}with concreteBthis is cheap (small concrete union), so I left them alone; they could only be affected whenBis itself a non-concrete (parametrized-oneof stub) type.encode/_encoded_sizecovering bothparametrize_oneofssettings. Full test suite passes locally.