Open
Conversation
Keep the new map in the stack so that it is not leaked on exception. Move-construct to the heap when exchanging for the old map. Constructing the new RawPolicyMap on the stack first is both safe and efficient here. It keeps the whole update transactional: while parsing and populating the map, all temporary state remains thread-local and unpublished, so any exception simply unwinds without exposing a partially built policy map to readers. Only once the map is fully ready do we allocate the final heap object and move the finished absl::flat_hash_map into it for atomic publication. That move is efficient: for this flat_hash_map<std::string, std::shared_ptr<...>> type, Abseil’s move constructor transfers ownership of the existing backing table rather than copying or rebuilding the entries, so the extra cost at publication time is essentially just one heap allocation for the map object itself. Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Keep the new map in the stack so that it is not leaked on exception. Move-construct to the heap when exchanging for the old map.
Constructing the new
RawPolicyMapon the stack first is both safe and efficient here. It keeps the whole update transactional: while parsing and populating the map, all temporary state remains thread-local and unpublished, so any exception simply unwinds without exposing a partially built policy map to readers. Only once the map is fully ready do we allocate the final heap object and move the finishedabsl::flat_hash_mapinto it for atomic publication. That move is efficient: for thisflat_hash_map<std::string, std::shared_ptr<...>>type, Abseil’s move constructor transfers ownership of the existing backing table rather than copying or rebuilding the entries, so the extra cost at publication time is essentially just one heap allocation for the map object itself.