-
Notifications
You must be signed in to change notification settings - Fork 6.3k
8330465: Stable Values and Collections (Internal) #18794
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
Conversation
src/java.base/share/classes/jdk/internal/lang/stable/StableAccess.java
Outdated
Show resolved
Hide resolved
…ess.java Co-authored-by: ExE Boss <[email protected]>
src/java.base/share/classes/jdk/internal/lang/stable/StableValueImpl.java
Show resolved
Hide resolved
Mailing list message from Olexandr Rotan on compiler-dev: Is it possible to make stable values and collections Serializable? I see On Thu, May 16, 2024 at 5:07?PM Per Minborg <pminborg at openjdk.org> wrote: -------------- next part -------------- |
|
Here are some results of a recently added benchmark that uses a memorized function (with 0 and 1 as input values): See test/micro/org/openjdk/bench/java/lang/stable/MemoizedFunctionBenchmark.java for details |
* @param <V> type of StableValue the stable array holds | ||
* @throws IllegalArgumentException if the provided {@code length} is {@code < 0} | ||
*/ | ||
static <V> StableArray<V> of(int length) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I interpret the method name of
as a method that creates an object that contains the argument as some kind of member, in the way that List.of
and friends work.
My intuitive interpretation of StableArray.of(10)
is that it returns an array with the single element 10.
I think a method like this should be named empty
, or emptyWithLength
or something like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stable arrays aren't supposed to be initialized with values, so I think your point is moot.
We are considering another implementation with less complexity. So, for now, thank you for all the feedback so far. We will try to make sure to carry over them to a new PR. |
Thanks for the insights. Also, I wonder what is a good amount of metadata you are considering, as original stable values only take one possible representation out of all to indicate a mutable state, much lighter in weight compared to the current implementation, which takes many bits; this might disencourage StableValue use. |
@minborg this pull request can not be integrated into git checkout stable-value
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push |
A new PR will be made available shortly. |
Stable Values & Collections (Internal)
Summary
This PR proposes to introduce an internal Stable Values & Collections API, which provides immutable value holders where elements are initialized at most once. Stable Values & Collections offer the performance and safety benefits of final fields while offering greater flexibility as to the timing of initialization.
Goals
For more details, see the draft JEP: https://openjdk.org/jeps/8312611
Performance
Performance compared to instance variables using two
AtomicReference
and two protected by double-checked locking under concurrent access by all threads:Performance compared to static variables protected by
AtomicReference
, class-holder idiom holder, and double-checked locking (all threads):Performance for stable lists (thread safe) in both instance and static contexts whereby we access a single value compared to
ArrayList
instances (which are not thread-safe) (all threads):Performance when summing lists:
Performance for stable maps in a static context compared to a
ConcurrentHashMap
(all threads):All figures above are from local tests on a Mac M1 laptop and should only be constructed as indicative figures.
Implementation details
There are some noteworthy implementation details in this PR:
final StableValue
. Previously, the determination of trustworthiness was connected to the class in which it was declared (e.g. is it arecord
or a hidden class). In order to grant such trust, there are extra restrictions imposed on reflection andsun.misc.Unsafe
usage for such declaredStableValue
fields. This is similar to howrecord
classes are handled.volatile
semantics which is slower and which is normally required for double-checked-locking access), we perform a freeze operation before an object becomes visible to other threads. This will prevent store-store reordering and hence, we are able to guarantee complete objects are always seen even under plain memory semantics.StableValue
elements/values, a transientStableValue
view backed by internal arrays is created upon read operations. This improves initialization time, reduces storage requirements, and improves access performance as these transient objects are eliminated by the C2 compiler.Progress
Issue
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/18794/head:pull/18794
$ git checkout pull/18794
Update a local copy of the PR:
$ git checkout pull/18794
$ git pull https://git.openjdk.org/jdk.git pull/18794/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 18794
View PR using the GUI difftool:
$ git pr show -t 18794
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/18794.diff
Webrev
Link to Webrev Comment