Skip to content

Commit

Permalink
apacheGH-1930: CacheSimple - low overhead cache for a single thread
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Jun 28, 2023
1 parent 414e512 commit f7a9104
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,28 @@ public static <Key, Value> Cache<Key, Value> createCache(int maxSize, BiConsumer
return new CacheCaffeine<>(maxSize, dropHandler) ;
}

/** Wrap an existing Caffeine cache */
public static <Key, Value> Cache<Key, Value> wrap(com.github.benmanes.caffeine.cache.Cache<Key,Value> caffeine) {
// Use a configured and built Caffeine cache with this API.
return new CacheCaffeine<>(caffeine) ;
}

/**
* Create a null cache.
* <p>
* This cache never retains a value and always
* evaluates in {@link Cache#getOrFill}.
* <p>
* This cache is thread-safe.
*/
public static <Key, Value> Cache<Key, Value> createNullCache() {
return new Cache0<>() ;
}

/** Create a lightweight cache (e.g. slot replacement) */
/**
* Create a lightweight cache (e.g. slot replacement).
* This cache is not thread-safe.
*/
public static <Key, Value> Cache<Key, Value> createSimpleCache(int size) {
return new CacheSimple<>(size, null) ;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@

import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
import java.util.function.Function;

import org.apache.jena.atlas.AtlasException;
import org.apache.jena.atlas.iterator.Iter;
import org.apache.jena.atlas.lib.Cache;

/**
* A simple fixed size cache that uses the hash code to address a slot. Clash policy
* is to overwrite. No object creation during lookup or insert.
* A simple fixed size cache that uses the hash code to address a slot.
* The clash policy is to overwrite.
* <p>
* The cache has very low overhead - there is no object creation during lookup or insert.
* <p>
* This cache is not thread safe.
*/
public class CacheSimple<K, V> implements Cache<K, V> {
private final V[] values;
Expand Down Expand Up @@ -66,25 +72,28 @@ public void clear() {

@Override
public boolean containsKey(K key) {
return getIfPresent(key) != null;
Objects.requireNonNull(key);
return index(key) >= 0 ;
}

// Return key index : -(index+1) if the key slot is empty.
// Return key index (>=0): return -(index+1) if the key slot is empty.
private final int index(K key) {
int x = (key.hashCode() & 0x7fffffff) % size;
if ( key.equals(keys[x]) )
return x;
return -x - 1;
}

// Convert to a slot index.
private final int decode(int x) {
if ( x >= 0 )
return x;
return -x - 1;
return -(x+1);
}

@Override
public V getIfPresent(K key) {
Objects.requireNonNull(key);
int x = index(key);
if ( x < 0 )
return null;
Expand All @@ -93,16 +102,54 @@ public V getIfPresent(K key) {

@Override
public V getOrFill(K key, Callable<V> callable) {
return CacheOps.getOrFillSync(this, key, callable);
return getOrFillNoSync(this, key, callable);
}

@Override
public V get(K key, Function<K, V> function) {
return CacheOps.getOrFillSync(this, key, function);
return getOrFillNoSync(this, key, function);
}

/**
* Implementation of getOrFill based on Cache.get and Cache.put
* This function is not thread safe.
*/
public static <K,V> V getOrFillNoSync(Cache<K,V> cache, K key, Function<K,V> function) {
V value = cache.getIfPresent(key) ;
if ( value == null ) {
try { value = function.apply(key) ; }
catch (RuntimeException ex) { throw ex; }
catch (Exception e) {
throw new AtlasException("Exception on cache fill", e) ;
}
if ( value != null )
cache.put(key, value) ;
}
return value ;
}

/**
* Implementation of getOrFill based on Cache.get and Cache.put
* This function is not thread safe.
*/
public static <K,V> V getOrFillNoSync(Cache<K,V> cache, K key, Callable<V> callable) {
V value = cache.getIfPresent(key) ;
if ( value == null ) {
try { value = callable.call() ; }
catch (RuntimeException ex) { throw ex; }
catch (Exception e) {
throw new AtlasException("Exception on cache fill", e) ;
}
if ( value != null )
cache.put(key, value) ;
}
return value ;
}


@Override
public void put(K key, V thing) {
// thing may be null.
int x = index(key);
x = decode(x);
V old = values[x];
Expand All @@ -114,6 +161,8 @@ public void put(K key, V thing) {
if ( dropHandler != null )
dropHandler.accept(keys[x], old);
currentSize--;
//keys[x] = null;
//values[x] = null;
}

// Already decremented if we are overwriting a full slot.
Expand All @@ -135,11 +184,13 @@ public void remove(K key) {
@Override
public long size() {
return currentSize;
// long x = 0 ;
// for ( K key : keys )
// if ( key != null )
// x++ ;
// return x ;
// long x = 0;
// for ( int i = 0 ; i < size ; i++ ) {
// K key = keys[i];
// if ( key != null )
// x++;
// }
// return x;
}

@Override
Expand Down

0 comments on commit f7a9104

Please sign in to comment.