Skip to content

8354076: LinkedBlockingDeque offer() creates nodes even if capacity has been reached#24521

Closed
kabutz wants to merge 1 commit into
openjdk:masterfrom
kabutz:lbd-check-capacity-before-node-creation
Closed

8354076: LinkedBlockingDeque offer() creates nodes even if capacity has been reached#24521
kabutz wants to merge 1 commit into
openjdk:masterfrom
kabutz:lbd-check-capacity-before-node-creation

Conversation

@kabutz

@kabutz kabutz commented Apr 8, 2025

Copy link
Copy Markdown
Contributor

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()


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8354076: LinkedBlockingDeque offer() creates nodes even if capacity has been reached (Bug - P4) ⚠️ Issue is not open.

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/24521/head:pull/24521
$ git checkout pull/24521

Update a local copy of the PR:
$ git checkout pull/24521
$ git pull https://git.openjdk.org/jdk.git pull/24521/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24521

View PR using the GUI difftool:
$ git pr show -t 24521

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/24521.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper

bridgekeeper Bot commented Apr 8, 2025

Copy link
Copy Markdown

👋 Welcome back kabutz! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk

openjdk Bot commented Apr 8, 2025

Copy link
Copy Markdown

❗ This change is not yet ready to be integrated.
See the Progress checklist in the description for automated requirements.

@openjdk

openjdk Bot commented Apr 8, 2025

Copy link
Copy Markdown

@kabutz The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk Bot added the core-libs core-libs-dev@openjdk.org label Apr 8, 2025
@kabutz kabutz changed the title LinkedBlockingDeque offer() creates nodes even if capacity has been reached 8354076: LinkedBlockingDeque offer() creates nodes even if capacity has been reached Apr 9, 2025
@openjdk openjdk Bot added the rfr Pull request is ready for review label Apr 9, 2025
@mlbridge

mlbridge Bot commented Apr 9, 2025

Copy link
Copy Markdown

Webrevs

@liach

liach commented Apr 15, 2025

Copy link
Copy Markdown
Member

If we strictly adhere to this interpretation of "create nodes", then we must only call a constructor after the locked capacity check has passed in places like offerLast, and this patch has not accomplished that yet.

I think the correct way to interpret the spec here is that "create node" means a node is created in the linked list, so we can interpret that as a change in collection membership reflected in accessor methods.

@kabutz

kabutz commented Apr 15, 2025

Copy link
Copy Markdown
Contributor Author

My code is similar to how LinkedBlockingQueue works.

From LinkedBlockingQueue:

    public boolean offer(E e) {
        if (e == null) throw new NullPointerException();
        final AtomicInteger count = this.count;
        if (count.get() == capacity)
            return false;
        final int c;
        final Node<E> node = new Node<E>(e);
        final ReentrantLock putLock = this.putLock;
        putLock.lock();
        try {
            if (count.get() == capacity)
                return false;
            enqueue(node);
            c = count.getAndIncrement();
            if (c + 1 < capacity)
                notFull.signal();
        } finally {
            putLock.unlock();
        }
        if (c == 0)
            signalNotEmpty();
        return true;
    }

However, I'm not sure whether it is a good idea to make this change, since making count volatile in LBD might impact performance for the most common use case (unbounded deque). For example:

import java.util.*;
import java.util.concurrent.*;
import java.util.stream.*;

public class NodeCreationBeforeLockLBD {
    /*
      This tests how quickly we can offer and poll on an "unbounded" deque,
      in order to see whether it is better to create the node before the lock.
     */

    public static void main(String... args) {
        for (int i = 0; i < 3; i++) {
            test(new LinkedBlockingDeque<>());
        }
    }

    private static void test(Queue<Integer> q) {
        System.out.println(q.getClass().getSimpleName());
        long time = System.nanoTime();
        try {
            IntStream.range(0, 10_000_000)
                    .parallel()
                    .forEach(i -> {
                        for (int j = 0; j < 5; j++) q.offer(j);
                        for (int j = 0; j < 5; j++) q.poll();
                    });
            System.out.println("q = " + q);
        } finally {
            time = System.nanoTime() - time;
            System.out.printf("time = %dms%n", (time / 1_000_000));
        }
    }
}

Output with Java 25+17:

$ java -showversion NodeCreationBeforeLockLBD.java
openjdk version "25-ea" 2025-09-16
OpenJDK Runtime Environment (build 25-ea+17-1966)
OpenJDK 64-Bit Server VM (build 25-ea+17-1966, mixed mode, sharing)
LinkedBlockingDeque
q = []
time = 4790ms
LinkedBlockingDeque
q = []
time = 4411ms
LinkedBlockingDeque
q = []
time = 4588ms

Output with these changes:

$ java -showversion NodeCreationBeforeLockLBD.java
openjdk version "25-internal" 2025-09-16
OpenJDK Runtime Environment (build 25-internal-adhoc.kabutz.jdk)
OpenJDK 64-Bit Server VM (build 25-internal-adhoc.kabutz.jdk, mixed mode)
LinkedBlockingDeque
q = []
time = 6436ms
LinkedBlockingDeque
q = []
time = 5871ms
LinkedBlockingDeque
q = []
time = 5982ms

@bridgekeeper

bridgekeeper Bot commented May 13, 2025

Copy link
Copy Markdown

@kabutz This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply issue a /touch or /keepalive command to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration!

@liach

liach commented May 13, 2025

Copy link
Copy Markdown
Member

@viktorklang-ora Is it possible for you to take a look at this?

@viktorklang-ora

Copy link
Copy Markdown
Contributor

@liach This PR should be closed, it's superseded by #24925

@kabutz

kabutz commented May 13, 2025

Copy link
Copy Markdown
Contributor Author

These changes have been included in #24925

@kabutz kabutz closed this May 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs core-libs-dev@openjdk.org rfr Pull request is ready for review

Development

Successfully merging this pull request may close these issues.

3 participants