perf(c++): remove shared_ptr from type info to reduce atomic counter cost #2951
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why?
Eliminate
shared_ptr<TypeInfo>overhead from the hot serialization code path. The atomic reference counting inshared_ptradds unnecessary overhead when TypeInfo objects are accessed frequently during serialization/deserialization. By switching to raw pointers with clear ownership semantics, we can improve performance on the critical path.What does this PR do?
This PR refactors the C++ serialization library's TypeInfo ownership model:
TypeInfo Changes
type_meta,encoded_namespace,encoded_type_name) fromshared_ptrtounique_ptrTypeInfo::deep_clone()method for creating deep copiesTypeInfonon-copyable (deleted copy constructor/assignment)TypeResolver Storage Changes
map<K, shared_ptr<TypeInfo>>to primary storage pattern:vector<unique_ptr<TypeInfo>>(owns all TypeInfo objects)TypeInfo*) pointing to primary storageconst TypeInfo*for nullable lookups (get_type_info_by_id,get_type_info_by_name)Result<const TypeInfo&>for error-handling lookups (get_type_info,get_struct_type_info<T>)Context Ownership Changes
WriteContextandReadContextnow holdunique_ptr<TypeResolver>instead ofshared_ptrForyclass usesstd::optionalfor contexts to support lazy initializationCode Pattern Updates
FORY_TRYwithResult<const TypeInfo&>to use explicit error handling (since TypeInfo is now non-copyable)read_struct_fields_compatiblesignature fromshared_ptr<TypeMeta>toconst TypeMeta*Related issues
#2906
#2944
#2958
Does this PR introduce any user-facing change?
Benchmark
This change eliminates atomic reference counting overhead on the hot serialization path:
shared_ptrcopy/destruction involves atomic increment/decrement operationsExpected improvement: Reduced CPU overhead in tight serialization loops, especially noticeable when serializing many small objects where TypeInfo lookups are frequent relative to data size.