RFR: 8354076: LinkedBlockingDeque offer() creates nodes even if capacity has been reached
kabutz
duke at openjdk.org
Wed Apr 9 10:44:02 UTC 2025
In the JavaDoc of LinkedBlockingDeque, it states: "Linked nodes are dynamically created upon each insertion unless this would bring the deque above capacity." However, in the current implementation, nodes are always created, even if the deque is full. This is because count is non-volatile, and we only check inside the linkFirst/Last() methods whether the queue is full. At this point we have already locked and have created the Node. Instead, the count could be volatile, and we could check before locking.
In the current version, calling offer() on a full LinkedBlockingDeque creates unnecessary objects and contention. Similarly for poll() and peek(), we could exit prior to locking by checking the count field.
Our suggestion is to make count volatile, and then exiting early from poll() and offer()
-------------
Commit messages:
- Made code consistent with JavaDoc for only creating nodes if there is space in the LBD.
Changes: https://git.openjdk.org/jdk/pull/24521/files
Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=24521&range=00
Issue: https://bugs.openjdk.org/browse/JDK-8354076
Stats: 47 lines in 1 file changed: 18 ins; 16 del; 13 mod
Patch: https://git.openjdk.org/jdk/pull/24521.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/24521/head:pull/24521
PR: https://git.openjdk.org/jdk/pull/24521
More information about the core-libs-dev
mailing list