From 107fe64005f792f44bfef2f735aa7098e9febd8c Mon Sep 17 00:00:00 2001 From: Ryan Caudy Date: Wed, 22 May 2024 18:59:29 -0400 Subject: [PATCH] Remove reference from IntrusivePage to its SoftReference --- .../intrusive/IntrusiveSoftLRU.java | 43 ++++--------------- .../intrusive/TestIntrusiveSoftLRU.java | 5 +-- .../parquet/table/pagestore/PageCache.java | 2 +- 3 files changed, 11 insertions(+), 39 deletions(-) diff --git a/Util/src/main/java/io/deephaven/util/datastructures/intrusive/IntrusiveSoftLRU.java b/Util/src/main/java/io/deephaven/util/datastructures/intrusive/IntrusiveSoftLRU.java index c20c4e05dc9..7280589e946 100644 --- a/Util/src/main/java/io/deephaven/util/datastructures/intrusive/IntrusiveSoftLRU.java +++ b/Util/src/main/java/io/deephaven/util/datastructures/intrusive/IntrusiveSoftLRU.java @@ -22,6 +22,7 @@ * @param the cached type */ public class IntrusiveSoftLRU { + private static final SoftReference NULL = new SoftReference<>(null); private final Adapter adapter; @@ -126,7 +127,7 @@ private void touchLRU(@NotNull T itemToTouch) { softReferences[slot].get() != itemToTouch) { // A new entry, place it on top of the tail adapter.setSlot(itemToTouch, tail); - softReferences[tail] = adapter.getOwner(itemToTouch); + softReferences[tail] = new SoftReference<>(itemToTouch); // Move the tail back one slot tail = prevs[tail]; @@ -170,7 +171,7 @@ private void touchSimple(@NotNull final T itemToTouch) { // No, cool. Just add it. final int slotForItem = size++; adapter.setSlot(itemToTouch, slotForItem); - softReferences[slotForItem] = adapter.getOwner(itemToTouch); + softReferences[slotForItem] = new SoftReference<>(itemToTouch); } else { // Yep, resize, copy, and potentially switch to LRU mode. doResize(); @@ -270,20 +271,11 @@ int size() { /** * An interface defining the required intrusive property getters and setters. Users should not directly call these * methods, or they risk corrupting the internal data structure. - * + * * @param */ public interface Adapter { - /** - * Get a {@link SoftReference} object which refers to the object being cached and will act as its "owner" in the - * cache. - * - * @param cachedObject the object being cached. - * @return a {@link SoftReference} that refers to the item and will act as its "owner" in the cache. - */ - SoftReference getOwner(T cachedObject); - /** * Get the slot in which the object is stored in the cache. * @@ -303,12 +295,8 @@ public interface Adapter { /** * An intrusive node for storing items in the cache. - * - * @param the actual node type */ - public interface Node> { - - SoftReference getOwner(); + public interface Node { int getSlot(); @@ -316,25 +304,15 @@ public interface Node> { /** * The base node implementation for items in the cache. - * - * @param */ - class Impl> implements Node { + class Impl implements Node { - private final SoftReference owner; private int slot; protected Impl() { - // noinspection unchecked - owner = new SoftReference<>((T) this); slot = -1; } - @Override - public SoftReference getOwner() { - return owner; - } - @Override public int getSlot() { return slot; @@ -351,20 +329,15 @@ public void setSlot(int slot) { * * @param */ - class Adapter> implements IntrusiveSoftLRU.Adapter { + class Adapter implements IntrusiveSoftLRU.Adapter { private static final IntrusiveSoftLRU.Adapter INSTANCE = new Adapter<>(); - public static > IntrusiveSoftLRU.Adapter getInstance() { + public static IntrusiveSoftLRU.Adapter getInstance() { // noinspection unchecked return (IntrusiveSoftLRU.Adapter) INSTANCE; } - @Override - public SoftReference getOwner(@NotNull T cachedObject) { - return cachedObject.getOwner(); - } - @Override public int getSlot(@NotNull T cachedObject) { return cachedObject.getSlot(); diff --git a/Util/src/test/java/io/deephaven/util/datastructures/intrusive/TestIntrusiveSoftLRU.java b/Util/src/test/java/io/deephaven/util/datastructures/intrusive/TestIntrusiveSoftLRU.java index 6abb4d743b9..a4a69f39a76 100644 --- a/Util/src/test/java/io/deephaven/util/datastructures/intrusive/TestIntrusiveSoftLRU.java +++ b/Util/src/test/java/io/deephaven/util/datastructures/intrusive/TestIntrusiveSoftLRU.java @@ -16,7 +16,8 @@ public class TestIntrusiveSoftLRU { private final IntrusiveSoftLRU.Adapter ADAPTER = new IntrusiveSoftLRU.Node.Adapter<>(); - static class TestNode extends IntrusiveSoftLRU.Node.Impl { + static class TestNode extends IntrusiveSoftLRU.Node.Impl { + final int index; TestNode(int index) { @@ -126,7 +127,6 @@ public void testExpandToMax() { i.remove(); } else { assertTrue(testNode.index < 16 || testNode.index >= 272); - assertEquals(testNode, ADAPTER.getOwner(testNode).get()); assertTrue(used.add(ADAPTER.getSlot(testNode))); } } @@ -163,7 +163,6 @@ private void iterateAssertAndCount(@NotNull final List> } else { nonNullItems++; assertTrue(testNode.index >= lru.currentCapacity()); - assertEquals(testNode, ADAPTER.getOwner(testNode).get()); assertTrue(used.add(ADAPTER.getSlot(testNode))); } } diff --git a/extensions/parquet/table/src/main/java/io/deephaven/parquet/table/pagestore/PageCache.java b/extensions/parquet/table/src/main/java/io/deephaven/parquet/table/pagestore/PageCache.java index fd971793845..033ab4a8aa0 100644 --- a/extensions/parquet/table/src/main/java/io/deephaven/parquet/table/pagestore/PageCache.java +++ b/extensions/parquet/table/src/main/java/io/deephaven/parquet/table/pagestore/PageCache.java @@ -33,7 +33,7 @@ public static WeakReference> getNullPage( /** * Intrusive data structure for page caching. */ - public static class IntrusivePage extends IntrusiveSoftLRU.Node.Impl> { + public static class IntrusivePage extends IntrusiveSoftLRU.Node.Impl { private final ChunkPage page;