-
Notifications
You must be signed in to change notification settings - Fork 861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Thread safe slot maps without containers #1785
Open
aardvark179
wants to merge
13
commits into
mozilla:master
Choose a base branch
from
aardvark179:aardvark179-remove-thread-safe-map-continaer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Thread safe slot maps without containers #1785
aardvark179
wants to merge
13
commits into
mozilla:master
from
aardvark179:aardvark179-remove-thread-safe-map-continaer
Conversation
This file contains 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
Another really interesting observation i guess the same is true for htmlunit. Will try it next year 😉 |
Refactor `SingleSlotMap` to be immutable.
Refactor test to match `SlotMapTest` to match `SingleEntrySlotMap`.
aardvark179
force-pushed
the
aardvark179-remove-thread-safe-map-continaer
branch
from
January 9, 2025 20:14
2a70a9a
to
5b1569b
Compare
Rebased after merge of #1782. |
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.
Thread safe slot maps without containers
This is a stacked PR on top of
To enable thread safe slot maps without containers we want two properties to hold true:
Compare and exchange operation
We utilise the
java.lang.invoke.VarHandle
methods to perform this operation. These are not available on older versions of Android, or Java 8, but if thread safe maps are required in those environments thenSlotMapOwner
could be refactored to use ajava.util.concurrent.atomic.AtomicReference
at the expense of one object (and one level of indirection).Promotion and lock sharing
Empty maps to single entry maps
Promoting from the empty map to a single entry slot map is simple, the empty map and single entry maps are immutable so we can construct the single entry map, perform the compare and exchange, and then we are either done, or we ask the currently installed map to perform the operation.
Single entry maps to larger maps
Again, we can construct the larger
ThreadSafeEmbeddedSlotMap
and perform a compare and exchange operation to install it. The exception to this is the compute operation, where we install the map (as we know a mutation will occur) and then perform the compute operation on this new map. This is done to avoid any side effects that might result from performing the compute operation twice.Promotion between large maps
The promotion of new maps looks something like the following. Blue links represent references that exist up until the new map is installed, and red linked represent objects and references that only exist in the new map. Since old map cannot be mutated, nor the new map installed, without a lock this operation does not need to be entirely atomic. Note that both the old and new maps have the same lock object.
This approach of sharing a single lock enables sequences like the following to work correctly: