Skip to content

Commit

Permalink
Remove reference from IntrusivePage to its SoftReference
Browse files Browse the repository at this point in the history
  • Loading branch information
rcaudy committed May 22, 2024
1 parent c67d5d7 commit 107fe64
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @param <T> the cached type
*/
public class IntrusiveSoftLRU<T> {

private static final SoftReference<?> NULL = new SoftReference<>(null);

private final Adapter<T> adapter;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 <T>
*/
public interface Adapter<T> {

/**
* 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<T> getOwner(T cachedObject);

/**
* Get the slot in which the object is stored in the cache.
*
Expand All @@ -303,38 +295,24 @@ public interface Adapter<T> {

/**
* An intrusive node for storing items in the cache.
*
* @param <T> the actual node type
*/
public interface Node<T extends Node<T>> {

SoftReference<T> getOwner();
public interface Node {

int getSlot();

void setSlot(int slot);

/**
* The base node implementation for items in the cache.
*
* @param <T>
*/
class Impl<T extends Impl<T>> implements Node<T> {
class Impl implements Node {

private final SoftReference<T> owner;
private int slot;

protected Impl() {
// noinspection unchecked
owner = new SoftReference<>((T) this);
slot = -1;
}

@Override
public SoftReference<T> getOwner() {
return owner;
}

@Override
public int getSlot() {
return slot;
Expand All @@ -351,20 +329,15 @@ public void setSlot(int slot) {
*
* @param <T>
*/
class Adapter<T extends Node<T>> implements IntrusiveSoftLRU.Adapter<T> {
class Adapter<T extends Node> implements IntrusiveSoftLRU.Adapter<T> {

private static final IntrusiveSoftLRU.Adapter<?> INSTANCE = new Adapter<>();

public static <T extends Node<T>> IntrusiveSoftLRU.Adapter<T> getInstance() {
public static <T extends Node> IntrusiveSoftLRU.Adapter<T> getInstance() {
// noinspection unchecked
return (IntrusiveSoftLRU.Adapter<T>) INSTANCE;
}

@Override
public SoftReference<T> getOwner(@NotNull T cachedObject) {
return cachedObject.getOwner();
}

@Override
public int getSlot(@NotNull T cachedObject) {
return cachedObject.getSlot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class TestIntrusiveSoftLRU {

private final IntrusiveSoftLRU.Adapter<TestNode> ADAPTER = new IntrusiveSoftLRU.Node.Adapter<>();

static class TestNode extends IntrusiveSoftLRU.Node.Impl<TestNode> {
static class TestNode extends IntrusiveSoftLRU.Node.Impl {

final int index;

TestNode(int index) {
Expand Down Expand Up @@ -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)));
}
}
Expand Down Expand Up @@ -163,7 +163,6 @@ private void iterateAssertAndCount(@NotNull final List<WeakReference<TestNode>>
} else {
nonNullItems++;
assertTrue(testNode.index >= lru.currentCapacity());
assertEquals(testNode, ADAPTER.getOwner(testNode).get());
assertTrue(used.add(ADAPTER.getSlot(testNode)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static <ATTR extends Any> WeakReference<IntrusivePage<ATTR>> getNullPage(
/**
* Intrusive data structure for page caching.
*/
public static class IntrusivePage<ATTR extends Any> extends IntrusiveSoftLRU.Node.Impl<IntrusivePage<ATTR>> {
public static class IntrusivePage<ATTR extends Any> extends IntrusiveSoftLRU.Node.Impl {

private final ChunkPage<ATTR> page;

Expand Down

0 comments on commit 107fe64

Please sign in to comment.