Make NodePath always calculate its own hash, to avoid multithreading and performance issues#113480
Open
Ivorforce wants to merge 1 commit into
Open
Make NodePath always calculate its own hash, to avoid multithreading and performance issues#113480Ivorforce wants to merge 1 commit into
NodePath always calculate its own hash, to avoid multithreading and performance issues#113480Ivorforce wants to merge 1 commit into
Conversation
…ultithreading and unforeseen performance issues.
6fae62e to
02fc6be
Compare
Member
|
This seems good in principle, but it seems that https://github.com/godotengine/godot/blob/master/scene/resources/resource_format_text.cpp#L208 uses It also looks like Intuitively I agree with you that NodePath compares are probably more common than creation. But I think this does require some benchmarks. If this isn't true then this might make things overall slower, possibly by quite a bit. |
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.
NodePathcopy-on-write, to avoid it changing accidentally (through copies) #113204This eliminates a theoretical race condition where
NodePathhash_cacheis read while it is incorrect. I think the race condition may not actually manifest because of hardware details, but this is not guaranteed. I explain this further below.This change also optimizes
NodePathby makinghash_cachemore available (e.g. foroperator==, which can exit early more often).Explanation
The race condition exists if one thread is calling
_update_hash_cachewhile another is getting the hash (e.g.operator==). In this situation, we have no guarantees thathash_cache_validis set afterhash_cacheis set, so there is a race condition wherehash_cachecan be read with an invalid value.I see three potential fixes:
hash_cacheatomic or use thread fences (but this would incur a substantial cost on hash access).hash_cacheandhash_cache_validare effectively atomic by aligning them across a 64-bit boundary (they already are aligned right now, but we could make this explicit). This should be safe in most architectures in practice, but it's not guaranteed by the C++ language, and assumes that the compiler uses the 64-bit setters for the values.hash_cache(instead of lazily).Always calculating
hash_cachemight seem wasteful at first, but calculating the hash cache at the constructors is actually ideal since most of the values should be in cache. This is assuming the hash cache is probably needed sooner or later — for example, nodes themselves are indexed by hash maps (which needs the hash cache), and so are properties.A side effect benefit is that we can always assume
hash_cacheexists, which should make other operations (likehash()andoperator==) more efficient.Side note
prepend_periodis removed because it is unused. This is better addressed in #113204, but removing it here does eliminate a potential failure case already.