Repository navigation
Conversation
**Context** Today users of vtproto benefit from fully unrolled ser/des but cannot take advantage of pooling, creating GC churn where there would otherwise be none. This diff: * Adds pooling support to the generator for well-known types. * Updates Makefile w/ pooling arguments for gen-wkt and gen-testproto * Adds additional fields to WKTNestedPool test type for testing * Regenerates test wkt and test protos * Adds IsLocalWrapper helper func to generator * Adds new unit tests exercising cloning behavior The new pooling code is inert for existing stubs; as such this change should be backwards compatible. Users can leverage this by regenerating their stubs. **⚠️ Caveats⚠️ ** While the code is backwards compatible, this is a _material change in behavior_. Previously there were cases where calling `ResetVT` on a type containing a singular WKT would simply clear the pointer. This meant it was safe to share/reuse WKT objects in certain cases. This is no longer true; all the same terms and conditions that apply to other pooled types apply to WKT wrappers. Currently Struct's do not benefit much from this as we do not have pooling for maps. This will be addressed in a follow on change.
This diff refactors planetscale#156 to add support for pooling maps. **Caveats** * Just like with pooled slices, pooled maps do not shrink. This is a footgun we may want to document explicitly.
This diff regenerates the wkt wrappers with map pooling support.
This diff updates the generated unmarshal code to pull map values from pools when pooling is enabled for both parent messages and the value type. **Caveats** * If a given key is repeated, the old pooled value will be dropped and picked up by GC. This is done for simplicity. * If unmarshal fails partway through, any values pulled from the pool will be left for GC. This behavior matches what happens with oneof's.
|
@mattlord I'd appreciate some eyes on this whenever you get a chance <3 |
mattlord
left a comment
There was a problem hiding this comment.
Thanks, @sgg! Bundling the three changes seems fine to me, since they build on each other. Two things seem worth a look:
-
I wonder if the well-known types' own methods should keep allocating normally. With
pool=google.golang.org/protobuf/types/known/*.*ingen-wkt, the shipped wrappers now allocate from the pool in their ownCloneVTandUnmarshalVT. For exampleTimestamp.CloneVTcallsTimestampFromVTPool(),Value.CloneVTcallsValueFromVTPool(), andStruct.UnmarshalVTtakes each map value from the pool. That applies to every user oftypes/known, including those who never use pooling. It isn't a correctness problem, but it adds async.Poolround trip to each of those allocations for everyone. Generating only the pool API for the well-known types (ResetVT,ReturnToVTPool,<Type>FromVTPool), and leaving their own allocations as they are, would keep pooling opt-in. The pooled path in a user's own messages would still call the WKTFromVTPoolfunctions. -
I think that the map change in
ResetVTdeserves a note in the README or release notes. Onmain, a pooled message's map fields aren't pooled:ResetVTdrops the map, and its values are left to the GC. NowResetVTreturns pooled map values to their pool and keeps the cleared map. So code that keeps a reference to a map value after returning the parent to the pool will now see that value reset and reused. Singular pooled fields already behave this way, so it's consistent, but it's a silent change for existing users of pooled messages with maps.
This PR makes three changes to the generator:
The commits reflect the changes in the order above. I elected to bundle them because all three changes are small and related. I'm happy to break these into separate PRs if folks feel that's appropriate. I'm also happy to make any doc updates that seem relevant.