swim.util.HashGenCacheSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swim-util Show documentation
Show all versions of swim-util Show documentation
Extended collection, iterator, and builder interfaces, lightweight cache sets and maps, and other foundational utilities
The newest version!
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package swim.util;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceArray;
/**
* A hashed generational cache set discards the least recently used value
* with the worst hit rate per hash bucket. HashGenCacheSet is a concurrent
* and lock-free LRFU cache, with O(1) access time.
*
* Maintaining four "generations" of cached values per hash bucket, the cache
* discards from the younger generations based on least recent usage, and
* promotes younger generations to older generations based on most frequent
* usage. Cache misses count as negative usage of the older generations,
* biasing the cache against least recently used values with poor hit rates.
*
* The cache soft references the older generations, and weak references the
* younger generations; the garbage collector can reclaim the entire cache,
* but will preferentially wipe the younger cache generations before the older
* cache generations.
*/
public class HashGenCacheSet {
final AtomicReferenceArray> buckets;
volatile long gen4Hits;
volatile long gen3Hits;
volatile long gen2Hits;
volatile long gen1Hits;
volatile long misses;
public HashGenCacheSet(int size) {
this.buckets = new AtomicReferenceArray>(size);
this.gen4Hits = 0L;
this.gen3Hits = 0L;
this.gen2Hits = 0L;
this.gen1Hits = 0L;
this.misses = 0L;
}
public T put(T value) {
if (this.buckets.length() == 0) {
return value;
}
final int index = Math.abs(value.hashCode()) % this.buckets.length();
HashGenCacheSetBucket bucket = this.buckets.get(index);
if (bucket == null) {
bucket = new HashGenCacheSetBucket();
}
final T gen4Val = bucket.gen4ValRef.get();
if (gen4Val != null && value.equals(gen4Val)) {
HashGenCacheSet.GEN4_HITS.incrementAndGet(this);
HashGenCacheSetBucket.GEN4_WEIGHT.incrementAndGet(bucket);
return gen4Val;
}
final T gen3Val = bucket.gen3ValRef.get();
if (gen3Val != null && value.equals(gen3Val)) {
HashGenCacheSet.GEN3_HITS.incrementAndGet(this);
if (HashGenCacheSetBucket.GEN3_WEIGHT.incrementAndGet(bucket) > bucket.gen4Weight) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen2ValRef, bucket.gen2Weight,
bucket.gen1ValRef, bucket.gen1Weight));
}
return gen3Val;
}
final T gen2Val = bucket.gen2ValRef.get();
if (gen2Val != null && value.equals(gen2Val)) {
HashGenCacheSet.GEN2_HITS.incrementAndGet(this);
if (HashGenCacheSetBucket.GEN2_WEIGHT.incrementAndGet(bucket) > bucket.gen3Weight) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen2ValRef, bucket.gen2Weight,
bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen1ValRef, bucket.gen1Weight));
}
return gen2Val;
}
final T gen1Val = bucket.gen1ValRef.get();
if (gen1Val != null && value.equals(gen1Val)) {
HashGenCacheSet.GEN1_HITS.incrementAndGet(this);
if (HashGenCacheSetBucket.GEN1_WEIGHT.incrementAndGet(bucket) > bucket.gen2Weight) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen1ValRef, bucket.gen1Weight,
bucket.gen2ValRef, bucket.gen2Weight));
}
return gen1Val;
}
HashGenCacheSet.MISSES.incrementAndGet(this);
if (gen4Val == null) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen2ValRef, bucket.gen2Weight,
bucket.gen1ValRef, bucket.gen1Weight,
new SoftReference(value), 1));
} else if (gen3Val == null) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen2ValRef, bucket.gen2Weight,
bucket.gen1ValRef, bucket.gen1Weight,
new SoftReference(value), 1));
} else if (gen2Val == null) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen1ValRef, bucket.gen1Weight,
new SoftReference(value), 1));
} else if (gen1Val == null) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen2ValRef, bucket.gen2Weight,
new SoftReference(value), 1));
} else {
// Penalize older gens for thrash. Promote gen1 to prevent nacent gens
// from flip-flopping. If sacrificed gen2 was worth keeping, it likely
// would have already been promoted.
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight - 1,
bucket.gen3ValRef, bucket.gen3Weight - 1,
bucket.gen1ValRef, bucket.gen1Weight,
new SoftReference(value), 1));
}
return value;
}
public boolean weaken(T value) {
if (this.buckets.length() == 0) {
return false;
}
final int index = Math.abs(value.hashCode()) % this.buckets.length();
final HashGenCacheSetBucket bucket = this.buckets.get(index);
if (bucket == null) {
return false;
}
final T gen4Val = bucket.gen4ValRef.get();
if (gen4Val != null && value.equals(gen4Val)) {
this.buckets.set(index, new HashGenCacheSetBucket(HashGenCacheSet.weakRef(bucket.gen4ValRef), bucket.gen4Weight,
bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen2ValRef, bucket.gen2Weight,
bucket.gen1ValRef, bucket.gen1Weight));
return true;
}
final T gen3Val = bucket.gen3ValRef.get();
if (gen3Val != null && value.equals(gen3Val)) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
HashGenCacheSet.weakRef(bucket.gen3ValRef), bucket.gen3Weight,
bucket.gen2ValRef, bucket.gen2Weight,
bucket.gen1ValRef, bucket.gen1Weight));
return true;
}
final T gen2Val = bucket.gen2ValRef.get();
if (gen2Val != null && value.equals(gen2Val)) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen3ValRef, bucket.gen3Weight,
HashGenCacheSet.weakRef(bucket.gen2ValRef), bucket.gen2Weight,
bucket.gen1ValRef, bucket.gen1Weight));
return true;
}
final T gen1Val = bucket.gen1ValRef.get();
if (gen1Val != null && value.equals(gen1Val)) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen2ValRef, bucket.gen2Weight,
HashGenCacheSet.weakRef(bucket.gen1ValRef), bucket.gen1Weight));
return true;
}
if (gen4Val == null) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen2ValRef, bucket.gen2Weight,
bucket.gen1ValRef, bucket.gen1Weight,
HashGenCacheSet.nullRef(), 1));
} else if (gen3Val == null) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen2ValRef, bucket.gen2Weight,
bucket.gen1ValRef, bucket.gen1Weight,
HashGenCacheSet.nullRef(), 1));
} else if (gen2Val == null) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen1ValRef, bucket.gen1Weight,
HashGenCacheSet.nullRef(), 1));
} else if (gen1Val == null) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen2ValRef, bucket.gen2Weight,
HashGenCacheSet.nullRef(), 1));
}
return false;
}
public boolean remove(T value) {
if (this.buckets.length() == 0) {
return false;
}
final int index = Math.abs(value.hashCode()) % this.buckets.length();
final HashGenCacheSetBucket bucket = this.buckets.get(index);
if (bucket == null) {
return false;
}
final T gen4Val = bucket.gen4ValRef.get();
if (gen4Val != null && value.equals(gen4Val)) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen2ValRef, bucket.gen2Weight,
bucket.gen1ValRef, bucket.gen1Weight,
HashGenCacheSet.nullRef(), 0));
return true;
}
final T gen3Val = bucket.gen3ValRef.get();
if (gen3Val != null && value.equals(gen3Val)) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen2ValRef, bucket.gen2Weight,
bucket.gen1ValRef, bucket.gen1Weight,
HashGenCacheSet.nullRef(), 0));
return true;
}
final T gen2Val = bucket.gen2ValRef.get();
if (gen2Val != null && value.equals(gen2Val)) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen1ValRef, bucket.gen1Weight,
HashGenCacheSet.nullRef(), 0));
return true;
}
final T gen1Val = bucket.gen1ValRef.get();
if (gen1Val != null && value.equals(gen1Val)) {
this.buckets.set(index, new HashGenCacheSetBucket(bucket.gen4ValRef, bucket.gen4Weight,
bucket.gen3ValRef, bucket.gen3Weight,
bucket.gen2ValRef, bucket.gen2Weight,
HashGenCacheSet.nullRef(), 0));
return true;
}
return false;
}
public void clear() {
for (int i = 0; i < this.buckets.length(); i += 1) {
this.buckets.set(i, null);
}
}
public double hitRatio() {
final double hits = (double) HashGenCacheSet.GEN4_HITS.get(this)
+ (double) HashGenCacheSet.GEN3_HITS.get(this)
+ (double) HashGenCacheSet.GEN2_HITS.get(this)
+ (double) HashGenCacheSet.GEN1_HITS.get(this);
return hits / (hits + (double) HashGenCacheSet.MISSES.get(this));
}
@SuppressWarnings("unchecked")
static final AtomicLongFieldUpdater> GEN4_HITS =
AtomicLongFieldUpdater.newUpdater((Class>) (Class>) HashGenCacheSet.class, "gen4Hits");
@SuppressWarnings("unchecked")
static final AtomicLongFieldUpdater> GEN3_HITS =
AtomicLongFieldUpdater.newUpdater((Class>) (Class>) HashGenCacheSet.class, "gen3Hits");
@SuppressWarnings("unchecked")
static final AtomicLongFieldUpdater> GEN2_HITS =
AtomicLongFieldUpdater.newUpdater((Class>) (Class>) HashGenCacheSet.class, "gen2Hits");
@SuppressWarnings("unchecked")
static final AtomicLongFieldUpdater> GEN1_HITS =
AtomicLongFieldUpdater.newUpdater((Class>) (Class>) HashGenCacheSet.class, "gen1Hits");
@SuppressWarnings("unchecked")
static final AtomicLongFieldUpdater> MISSES =
AtomicLongFieldUpdater.newUpdater((Class>) (Class>) HashGenCacheSet.class, "misses");
static final SoftReference