Added LockFreeIntMap
parent
7f1b1c0c0c
commit
f3855bed38
|
@ -54,7 +54,7 @@ import java.util.*
|
|||
* @author Nathan Sweet
|
||||
* @author Tommy Ettinger
|
||||
*/
|
||||
class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
||||
open class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
||||
companion object {
|
||||
const val version = Collections.version
|
||||
}
|
||||
|
@ -198,14 +198,14 @@ class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
|||
return null
|
||||
}
|
||||
|
||||
fun putAll(map: IntMap<out V>) {
|
||||
ensureCapacity(map.size_)
|
||||
if (map.hasZeroValue) {
|
||||
put(0, map.zeroValue!!)
|
||||
open fun putAll(from: IntMap<out V>) {
|
||||
ensureCapacity(from.size_)
|
||||
if (from.hasZeroValue) {
|
||||
put(0, from.zeroValue!!)
|
||||
}
|
||||
|
||||
val keyTable = map.keyTable
|
||||
val valueTable = map.valueTable
|
||||
val keyTable = from.keyTable
|
||||
val valueTable = from.valueTable
|
||||
var i = 0
|
||||
val n = keyTable.size
|
||||
while (i < n) {
|
||||
|
@ -306,7 +306,7 @@ class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
|||
* nothing is done. If the map contains more items than the specified capacity, the next highest power of two capacity is used
|
||||
* instead.
|
||||
*/
|
||||
fun shrink(maximumCapacity: Int) {
|
||||
open fun shrink(maximumCapacity: Int) {
|
||||
require(maximumCapacity >= 0) { "maximumCapacity must be >= 0: $maximumCapacity" }
|
||||
val tableSize = tableSize(maximumCapacity, loadFactor)
|
||||
if (keyTable.size > tableSize) resize(tableSize)
|
||||
|
@ -315,7 +315,7 @@ class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
|||
/**
|
||||
* Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.
|
||||
* */
|
||||
fun clear(maximumCapacity: Int) {
|
||||
open fun clear(maximumCapacity: Int) {
|
||||
val tableSize = tableSize(maximumCapacity, loadFactor)
|
||||
if (keyTable.size <= tableSize) {
|
||||
clear()
|
||||
|
@ -357,7 +357,7 @@ class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
|||
* @param identity If true, uses == to compare the specified value with values in the map. If false, uses
|
||||
* [.equals].
|
||||
*/
|
||||
fun containsValue(value: Any?, identity: Boolean = false): Boolean {
|
||||
open fun containsValue(value: Any?, identity: Boolean): Boolean {
|
||||
val valueTable = valueTable
|
||||
if (value == null) {
|
||||
if (hasZeroValue && zeroValue == null) return true
|
||||
|
@ -503,7 +503,7 @@ class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
|||
* Uses == for comparison of each value.
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun equalsIdentity(other: Any?): Boolean {
|
||||
open fun equalsIdentity(other: Any?): Boolean {
|
||||
if (other === this) return true
|
||||
if (other !is IntMap<*>) return false
|
||||
other as IntMap<V>
|
||||
|
@ -568,7 +568,7 @@ class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
|||
* Use the [Entries] constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun entries(): Entries<V?> {
|
||||
open fun entries(): Entries<V?> {
|
||||
if (allocateIterators) return Entries(this as IntMap<V?>)
|
||||
if (entries1 == null) {
|
||||
entries1 = Entries(this as IntMap<V?>)
|
||||
|
@ -592,7 +592,7 @@ class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
|||
* If [Collections.allocateIterators] is false, the same iterator instance is returned each time this method is called.
|
||||
* Use the [Entries] constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
fun values(): Values<V> {
|
||||
open fun values(): Values<V> {
|
||||
if (allocateIterators) return Values(this)
|
||||
if (values1 == null) {
|
||||
values1 = Values(this)
|
||||
|
@ -616,7 +616,7 @@ class IntMap<V> : MutableMap<Int, V>, MutableIterable<IntMap.Entry<V?>> {
|
|||
* If [Collections.allocateIterators] is false, the same iterator instance is returned each time this method is called.
|
||||
* Use the [Entries] constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
fun keys(): Keys {
|
||||
open fun keys(): Keys {
|
||||
if (allocateIterators) return Keys(this)
|
||||
if (keys1 == null) {
|
||||
keys1 = Keys(this)
|
||||
|
|
|
@ -1,230 +0,0 @@
|
|||
/*
|
||||
* Copyright 2018 dorkbox, llc.
|
||||
*
|
||||
* 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 dorkbox.collections;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
||||
|
||||
import dorkbox.collections.IntMap.Entries;
|
||||
import dorkbox.collections.IntMap.Keys;
|
||||
import dorkbox.collections.IntMap.Values;
|
||||
|
||||
|
||||
/**
|
||||
* This class uses the "single-writer-principle" for lock-free publication.
|
||||
* <p>
|
||||
* Since there are only 2 methods to guarantee that modifications can only be called one-at-a-time (either it is only called by
|
||||
* one thread, or only one thread can access it at a time) -- we chose the 2nd option -- and use 'synchronized' to make sure that only
|
||||
* one thread can access this modification methods at a time. Getting or checking the presence of values can then happen in a lock-free
|
||||
* manner.
|
||||
* <p>
|
||||
* According to my benchmarks, this is approximately 25% faster than ConcurrentHashMap for (all types of) reads, and a lot slower for
|
||||
* contended writes.
|
||||
* <p>
|
||||
* This data structure is for many-read/few-write scenarios
|
||||
*
|
||||
* This is an unordered map that uses int keys. This implementation is a cuckoo hash map using 3 hashes, random walking, and a small stash
|
||||
* for problematic keys. Null values are allowed. No allocation is done except when growing the table size. <br>
|
||||
* <br>
|
||||
* This map performs very fast get, containsKey, and remove (typically O(1), worst case O(log(n))). Put may be a bit slower,
|
||||
* depending on hash collisions. Load factors greater than 0.91 greatly increase the chances the map will have to rehash to the
|
||||
* next higher POT size.
|
||||
* @author Nathan Sweet
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public final
|
||||
class LockFreeIntMap<V> implements Cloneable, Serializable {
|
||||
public static final String version = Collections.version;
|
||||
|
||||
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
||||
private static final AtomicReferenceFieldUpdater<LockFreeIntMap, IntMap> mapREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||
LockFreeIntMap.class,
|
||||
IntMap.class,
|
||||
"map");
|
||||
|
||||
private volatile IntMap<V> map;
|
||||
|
||||
// synchronized is used here to ensure the "single writer principle", and make sure that ONLY one thread at a time can enter this
|
||||
// section. Because of this, we can have unlimited reader threads all going at the same time, without contention (which is our
|
||||
// use-case 99% of the time)
|
||||
|
||||
/**
|
||||
* Constructs an empty <tt>LockFreeIntMap</tt> with the default initial capacity
|
||||
* (16) and the default load factor (0.75).
|
||||
*/
|
||||
public
|
||||
LockFreeIntMap() {
|
||||
map = new IntMap<V>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a copy of an <tt>LockFreeIntMap</tt>
|
||||
*/
|
||||
public
|
||||
LockFreeIntMap(LockFreeIntMap map) {
|
||||
this.map = new IntMap<V>(map.map);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an empty <tt>LockFreeIntMap</tt> with the specified initial
|
||||
* capacity and the default load factor (0.75).
|
||||
*
|
||||
* @param initialCapacity the initial capacity.
|
||||
*
|
||||
* @throws IllegalArgumentException if the initial capacity is negative.
|
||||
*/
|
||||
public
|
||||
LockFreeIntMap(int initialCapacity) {
|
||||
map = new IntMap<V>(initialCapacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an empty <tt>LockFreeIntMap</tt> with the specified initial
|
||||
* capacity and load factor.
|
||||
*
|
||||
* @param initialCapacity the initial capacity
|
||||
* @param loadFactor the load factor
|
||||
*
|
||||
* @throws IllegalArgumentException if the initial capacity is negative
|
||||
* or the load factor is nonpositive
|
||||
*/
|
||||
public
|
||||
LockFreeIntMap(int initialCapacity, float loadFactor) {
|
||||
this.map = new IntMap<V>(initialCapacity, loadFactor);
|
||||
}
|
||||
|
||||
|
||||
public
|
||||
int size() {
|
||||
// use the SWP to get a lock-free get of the value
|
||||
return mapREF.get(this)
|
||||
.size;
|
||||
}
|
||||
|
||||
public
|
||||
boolean isEmpty() {
|
||||
// use the SWP to get a lock-free get of the value
|
||||
return mapREF.get(this)
|
||||
.size == 0;
|
||||
}
|
||||
|
||||
public
|
||||
boolean containsKey(final int key) {
|
||||
// use the SWP to get a lock-free get of the value
|
||||
return mapREF.get(this)
|
||||
.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the specified value is in the map. Note this traverses the entire map and compares every value, which may be
|
||||
* an expensive operation.
|
||||
*
|
||||
* @param identity If true, uses == to compare the specified value with values in the map. If false, uses
|
||||
* {@link #equals(Object)}.
|
||||
*/
|
||||
public
|
||||
boolean containsValue(final Object value, boolean identity) {
|
||||
// use the SWP to get a lock-free get of the value
|
||||
return mapREF.get(this)
|
||||
.containsValue(value, identity);
|
||||
}
|
||||
|
||||
public
|
||||
V get(final int key) {
|
||||
// use the SWP to get a lock-free get of the value
|
||||
return (V) mapREF.get(this)
|
||||
.get(key);
|
||||
}
|
||||
|
||||
public synchronized
|
||||
V put(final int key, final V value) {
|
||||
return map.put(key, value);
|
||||
}
|
||||
|
||||
public synchronized
|
||||
V remove(final int key) {
|
||||
return map.remove(key);
|
||||
}
|
||||
|
||||
public synchronized
|
||||
void putAll(final IntMap<V> map) {
|
||||
this.map.putAll(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* DO NOT MODIFY THE MAP VIA THIS (unless you synchronize around it!) It will result in unknown object visibility!
|
||||
*
|
||||
* Returns an iterator for the keys in the map. Remove is supported. Note that the same iterator instance is returned each
|
||||
* time this method is called. Use the {@link Entries} constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
public
|
||||
Keys keys() {
|
||||
return mapREF.get(this)
|
||||
.keys();
|
||||
}
|
||||
|
||||
/**
|
||||
* DO NOT MODIFY THE MAP VIA THIS (unless you synchronize around it!) It will result in unknown object visibility!
|
||||
*
|
||||
* Returns an iterator for the values in the map. Remove is supported. Note that the same iterator instance is returned each
|
||||
* time this method is called. Use the {@link Entries} constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
public
|
||||
Values<V> values() {
|
||||
return mapREF.get(this)
|
||||
.values();
|
||||
}
|
||||
|
||||
/**
|
||||
* DO NOT MODIFY THE MAP VIA THIS (unless you synchronize around it!) It will result in unknown object visibility!
|
||||
*
|
||||
* Returns an iterator for the entries in the map. Remove is supported. Note that the same iterator instance is returned each
|
||||
* time this method is called. Use the {@link Entries} constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
public
|
||||
Entries<V> entries() {
|
||||
return mapREF.get(this)
|
||||
.entries();
|
||||
}
|
||||
|
||||
public synchronized
|
||||
void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Identity equals only!
|
||||
*/
|
||||
@Override
|
||||
public
|
||||
boolean equals(final Object o) {
|
||||
return this == o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
int hashCode() {
|
||||
return mapREF.get(this)
|
||||
.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public
|
||||
String toString() {
|
||||
return mapREF.get(this)
|
||||
.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,217 @@
|
|||
/*
|
||||
* Copyright 2023 dorkbox, llc
|
||||
*
|
||||
* 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 dorkbox.collections
|
||||
|
||||
import java.io.Serializable
|
||||
import java.util.concurrent.atomic.*
|
||||
|
||||
/**
|
||||
* This class uses the "single-writer-principle" for lock-free publication.
|
||||
*
|
||||
*
|
||||
* Since there are only 2 methods to guarantee that modifications can only be called one-at-a-time (either it is only called by
|
||||
* one thread, or only one thread can access it at a time) -- we chose the 2nd option -- and use 'synchronized' to make sure that only
|
||||
* one thread can access this modification methods at a time. Getting or checking the presence of values can then happen in a lock-free
|
||||
* manner.
|
||||
*
|
||||
*
|
||||
* According to my benchmarks, this is approximately 25% faster than ConcurrentHashMap for (all types of) reads, and a lot slower for
|
||||
* contended writes.
|
||||
*
|
||||
*
|
||||
* This data structure is for many-read/few-write scenarios
|
||||
*
|
||||
*
|
||||
* An unordered map. This implementation is a cuckoo hash map using 3 hashes, random walking, and a small stash for problematic
|
||||
* keys. Null keys are not allowed. Null values are allowed. No allocation is done except when growing the table size. <br></br>
|
||||
*
|
||||
*
|
||||
* This map performs very fast get, containsKey, and remove (typically O(1), worst case O(log(n))). Put may be a bit slower,
|
||||
* depending on hash collisions. Load factors greater than 0.91 greatly increase the chances the map will have to rehash to the
|
||||
* next higher POT size.
|
||||
*
|
||||
*
|
||||
* Iteration can be very slow for a map with a large capacity. [.clear] and [.shrink] can be used to reduce
|
||||
* the capacity. [OrderedMap] provides much faster iteration.
|
||||
*/
|
||||
class LockFreeIntMap<V> : IntMap<V>, Cloneable, Serializable {
|
||||
|
||||
@Volatile
|
||||
private var hashMap: IntMap<V>
|
||||
|
||||
// synchronized is used here to ensure the "single writer principle", and make sure that ONLY one thread at a time can enter this
|
||||
// section. Because of this, we can have unlimited reader threads all going at the same time, without contention (which is our
|
||||
// use-case 99% of the time)
|
||||
/**
|
||||
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
|
||||
* (16) and the default load factor (0.75).
|
||||
*/
|
||||
constructor() {
|
||||
hashMap = IntMap()
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an empty <tt>HashMap</tt> with the specified initial
|
||||
* capacity and the default load factor (0.75).
|
||||
*
|
||||
* @param initialCapacity the initial capacity.
|
||||
*
|
||||
* @throws IllegalArgumentException if the initial capacity is negative.
|
||||
*/
|
||||
constructor(initialCapacity: Int) {
|
||||
hashMap = IntMap(initialCapacity)
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an empty <tt>HashMap</tt> with the specified initial
|
||||
* capacity and load factor.
|
||||
*
|
||||
* @param initialCapacity the initial capacity
|
||||
* @param loadFactor the load factor
|
||||
*
|
||||
* @throws IllegalArgumentException if the initial capacity is negative
|
||||
* or the load factor is nonpositive
|
||||
*/
|
||||
constructor(initialCapacity: Int, loadFactor: Float) {
|
||||
hashMap = IntMap(initialCapacity, loadFactor)
|
||||
}
|
||||
|
||||
override val size: Int
|
||||
get() {
|
||||
// use the SWP to get a lock-free get of the value
|
||||
return mapREF[this].size
|
||||
}
|
||||
|
||||
override fun isEmpty(): Boolean {
|
||||
// use the SWP to get a lock-free get of the value
|
||||
return mapREF[this].size == 0
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun containsKey(key: Int): Boolean {
|
||||
// use the SWP to get a lock-free get of the value
|
||||
val value = mapREF[this] as IntMap<V>
|
||||
return value.containsKey(key)
|
||||
}
|
||||
|
||||
override fun containsValue(value: Any?, identity: Boolean): Boolean {
|
||||
// use the SWP to get a lock-free get of the value
|
||||
return mapREF[this].containsValue(value, identity)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override operator fun get(key: Int): V? {
|
||||
// use the SWP to get a lock-free get of the value
|
||||
val value = mapREF[this] as IntMap<V>
|
||||
return value.get(key)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun put(key: Int, value: V): V? {
|
||||
return hashMap.put(key, value)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun remove(key: Int): V? {
|
||||
return hashMap.remove(key)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun putAll(from: IntMap<out V>) {
|
||||
hashMap.putAll(from)
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
override fun clear() {
|
||||
hashMap.clear()
|
||||
}
|
||||
|
||||
/**
|
||||
* DO NOT MODIFY THE MAP VIA THIS (unless you synchronize around it!) It will result in unknown object visibility!
|
||||
*
|
||||
* Returns an iterator for the keys in the map. Remove is supported. Note that the same iterator instance is returned each
|
||||
* time this method is called. Use the [ObjectMap.Entries] constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
override fun keys(): Keys {
|
||||
return mapREF[this].keys()
|
||||
}
|
||||
|
||||
/**
|
||||
* DO NOT MODIFY THE MAP VIA THIS (unless you synchronize around it!) It will result in unknown object visibility!
|
||||
*
|
||||
* Returns an iterator for the values in the map. Remove is supported. Note that the same iterator instance is returned each
|
||||
* time this method is called. Use the [ObjectMap.Entries] constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun values(): Values<V> {
|
||||
return mapREF[this].values() as Values<V>
|
||||
}
|
||||
|
||||
/**
|
||||
* DO NOT MODIFY THE MAP VIA THIS (unless you synchronize around it!) It will result in unknown object visibility!
|
||||
*
|
||||
* Returns an iterator for the entries in the map. Remove is supported. Note that the same iterator instance is returned each
|
||||
* time this method is called. Use the [ObjectMap.Entries] constructor for nested or multithreaded iteration.
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun entries(): Entries<V?> {
|
||||
return mapREF[this].entries() as Entries<V?>
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return mapREF[this] == other
|
||||
}
|
||||
|
||||
override fun equalsIdentity(other: Any?): Boolean {
|
||||
return mapREF[this].equalsIdentity(other)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return mapREF[this].hashCode()
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return mapREF[this].toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the map and reduces the size of the backing arrays to be the specified capacity, if they are larger. The reduction
|
||||
* is done by allocating new arrays, though for large arrays this can be faster than clearing the existing array.
|
||||
*/
|
||||
@Synchronized
|
||||
override fun clear(maximumCapacity: Int) {
|
||||
mapREF[this].clear(maximumCapacity)
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduces the size of the backing arrays to be the specified capacity or less. If the capacity is already less, nothing is
|
||||
* done.
|
||||
* If the map contains more items than the specified capacity, the next highest power of two capacity is used instead.
|
||||
*/
|
||||
@Synchronized
|
||||
override fun shrink(maximumCapacity: Int) {
|
||||
mapREF[this].shrink(maximumCapacity)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val version = Collections.version
|
||||
|
||||
// Recommended for best performance while adhering to the "single writer principle". Must be static-final
|
||||
private val mapREF = AtomicReferenceFieldUpdater.newUpdater(
|
||||
LockFreeIntMap::class.java, IntMap::class.java, "hashMap"
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue