-
Notifications
You must be signed in to change notification settings - Fork 10.9k
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
Suppliers.memoize()
thread pinning
#7140
Comments
This is already addressed by the JDK team’s EA build. That’ll avoid surprises as a better long term solution. |
That's cool to see and would definitely help long term, but unfortunately my company only uses LTS versions of Java so that'd mean we'd have to wait until Java 25 (or whatever it is) to be released. |
If you read the thread, you’ll see that VTs still have serious problems and must be used very carefully for now. You might not want to use them until 25 when hopefully they’ll be less error prone. Libraries trying to support them early might not really help; imho it’s best to wait.
|
Thanks for the info @ben-manes. Given that information, would you say that it's better to keep the current implementation as-is? |
+1, thanks. One other note: We did a little work to improve the memory usage of There is probably a way to write our own even more efficient implementation of Whatever we decide for now, it's possible that we'll revisit as we get more experience with virtual threads. (If we encounter the level of problems that Ben Manes passes along, then we may not get that experience anytime soon :)) |
Oh, also: As the Java platform develops more broadly, we may also see alternatives to |
This let us move away from the current hack (which was never in a Guava _release_) of using a lock of `new Integer(1)` (because we needed something `Serializable` while we figured this all out). RELNOTES=n/a PiperOrigin-RevId: 659714359
API(s)
How do you want it to be improved?
Change it from
synchronized
toReentrantLock
Why do we need it to be improved?
Suppliers.memoize()
internally uses the double-checked locking idiom with thesynchronized
keyword. Usingsynchronized
with virtual threads can cause thread pinning and slow down performance.Example
Current Behavior
Functions properly but causes performance degradation when used with virtual threads on Java 21
Desired Behavior
Performs optimally with virtual threads.
Concrete Use Cases
In our virtual thread enabled REST API, added lazy loading using
Suppliers.memoize()
around some of our database interactions to improve performance but ended up seeing worse performance than going directly to database.Checklist
I agree to follow the code of conduct.
I have read and understood the contribution guidelines.
I have read and understood Guava's philosophy, and I strongly believe that this proposal aligns with it.
The text was updated successfully, but these errors were encountered: