swim.util.HashGenCacheSet Maven / Gradle / Ivy
// Copyright 2015-2019 SWIM.AI 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.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 int gen4Hits;
volatile int gen3Hits;
volatile int gen2Hits;
volatile int gen1Hits;
volatile int misses;
public HashGenCacheSet(int size) {
this.buckets = new AtomicReferenceArray>(size);
}
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)) {
GEN4_HITS.incrementAndGet(this);
BUCKET_GEN4_WEIGHT.incrementAndGet(bucket);
return gen4Val;
}
final T gen3Val = bucket.gen3ValRef.get();
if (gen3Val != null && value.equals(gen3Val)) {
GEN3_HITS.incrementAndGet(this);
if (BUCKET_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)) {
GEN2_HITS.incrementAndGet(this);
if (BUCKET_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)) {
GEN1_HITS.incrementAndGet(this);
if (BUCKET_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;
}
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(
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,
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,
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,
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);
}
}
long hits() {
return (long) this.gen4Hits + (long) this.gen3Hits + (long) this.gen2Hits + (long) this.gen1Hits;
}
public double hitRatio() {
final double hits = (double) hits();
return hits / (hits + (double) this.misses);
}
static final WeakReference weakRef(Reference ref) {
if (ref instanceof WeakReference>) {
return (WeakReference) ref;
} else {
return new WeakReference(ref.get());
}
}
@SuppressWarnings("unchecked")
static final SoftReference nullRef() {
return (SoftReference) NULL_REF;
}
static final SoftReference
© 2015 - 2025 Weber Informatics LLC | Privacy Policy