From 239ae6a500e170135ffd31f281213dd1a852c7ec Mon Sep 17 00:00:00 2001 From: Keshava Munegowda Date: Fri, 31 May 2024 23:18:49 +0530 Subject: [PATCH 1/2] Improve the performance of Atomicqueue Signed-off-by: Keshava Munegowda --- .../java/io/perl/api/impl/AtomicQueue.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/perl/src/main/java/io/perl/api/impl/AtomicQueue.java b/perl/src/main/java/io/perl/api/impl/AtomicQueue.java index 90127ddf..a1dd3090 100644 --- a/perl/src/main/java/io/perl/api/impl/AtomicQueue.java +++ b/perl/src/main/java/io/perl/api/impl/AtomicQueue.java @@ -41,8 +41,7 @@ public AtomicQueue() { this.tail = new AtomicReference<>(firstNode); } - @Override - public T poll() { + public T pollOnce() { final Node curHead = head.getAndSet(null); if (curHead == null) { return null; @@ -56,6 +55,25 @@ public T poll() { return nxt.item; } + @Override + public T poll() { + Node curHead = head.get(); + Node nxt = curHead.next.get(); + + while ( nxt != null && !head.compareAndSet(curHead, nxt) ) { + curHead = head.get(); + nxt = curHead.next.get(); + } + + if (nxt == null) { + return null; + } + curHead.next.set(null); + + return nxt.item; + } + + @Override public boolean add(T data) { final Node node = new Node<>(data); @@ -66,6 +84,7 @@ public boolean add(T data) { @Override public void clear() { + firstNode.next.set(null); Node first = head.getAndSet(firstNode); tail.set(first); /* From 46587eb9376fc324f8ff755cbe51b7864f7167de Mon Sep 17 00:00:00 2001 From: Keshava Munegowda Date: Fri, 31 May 2024 23:59:21 +0530 Subject: [PATCH 2/2] Improve the performance of CQueue Signed-off-by: Keshava Munegowda --- .../main/java/io/perl/api/impl/CQueue.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/perl/src/main/java/io/perl/api/impl/CQueue.java b/perl/src/main/java/io/perl/api/impl/CQueue.java index 26f2a7df..6883e2ed 100644 --- a/perl/src/main/java/io/perl/api/impl/CQueue.java +++ b/perl/src/main/java/io/perl/api/impl/CQueue.java @@ -58,8 +58,7 @@ public CQueue() { this.tail = firstNode; } - @Override - public T poll() { + public T pollOnce() { final Object cur = NEXT.getAndSet(head, null); if (cur == null) { return null; @@ -68,6 +67,23 @@ public T poll() { return (T) ITEM.getAndSet(cur, null); } + @Override + public T poll() { + Object curHead = HEAD.get(this); + Object nxt = NEXT.get(curHead); + + while (nxt != null && !HEAD.compareAndSet(this, curHead, nxt)) { + curHead = HEAD.get(this); + nxt = NEXT.get(curHead); + } + + if (nxt == null) { + return null; + } + NEXT.set(curHead, null); + return (T) ITEM.getAndSet(nxt, null); + } + @Override public boolean add(T data) { final Node node = new Node<>(data); @@ -78,6 +94,7 @@ public boolean add(T data) { @Override public void clear() { + NEXT.set(firstNode, null); Object first = HEAD.getAndSet(this, firstNode); TAIL.set(this, firstNode); /*