Class JCasHashMap
The update occurs in the code in JCasGenerated classes, which do: a call to get the value of the map for a key if that is "null", it creates the new JCas cover object, and does a "put" to add the value.
The creation of the new JCas cover object can, in turn, run arbitrary user code, which can result in updates to the JCasHashMap which occur before this original update occurs.
In a multi-threaded environment, multiple threads can do a "get" for the same Feature Structure instance. If it's not in the Map, the correct behavior is:
one of the threads adds the new element the other threads wait for the one thread to finish adding, and then return the object that the one thread added.
The implementation works as follows:
1) The JCasHashMap is split into "n" sub-maps. The number is the number of cores, but grows more slowly as the # of cores > 16. This number can be specified, but this is not currently exposed in the tuning parameters Locking occurs on the sub-maps; the outer method calls are not synchronized 2) The number of sub maps is rounded to a power of 2, to allow the low order bits of the hash of the key to be used to pick the map (via masking). 3) A getReserve that results in not-found returns a null, but adds to the table a special reserved element. 3a) This adding may result in table resizing 4) A getReserve that finds a special reserved element, knows that some other thread is in the process of adding an entry for that key, so it waits. 5) A put, if it finds a reserved-for-that-key element, replaces that with the real element, and then does a notifyAll to wake up any threads that were waiting (on this sub-map), and these threads then re-do the get. Multiple threads could be waiting on this, and they will all wake-up.
All calls are of the getReserved, followed by a put if the getReserved returns null. (Experiment - disabled after no change noted To improve locality of reference, an aux data structure of size to fit in one cache line of a Power7 (128 bytes) caches the latest lookups)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private static final int
(package private) static final boolean
private final int
private final int
private final int
(package private) static int
must be a power of 2, > 0 package private for testing not final to allow test case to reset it must not be changed during multi-thread operationprivate final int
private final float
private final JCasHashMapSubMap
private static final int
private final int
private final JCasHashMapSubMap[]
(package private) static final boolean
private final boolean
-
Constructor Summary
ConstructorsConstructorDescriptionJCasHashMap
(int capacity, boolean doUseCache) JCasHashMap
(int capacity, boolean doUseCache, int aConcurrencyLevel) -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
(package private) static boolean
concurrencyLimitedByInitialCapacity
(int currentConcurrencyLevel, int curMapSize) initial capacity (other than testing), is by default (from JCasImpl) is bigger of 256 and cas heap initial size (500,000) / 16 = 31K but users may set it lower in their uima configuration We use the current capacity of the JCasHashMap to set the concurrency limit(package private) int
(package private) int[]
(package private) int
int
(package private) static int
getReserve
(int key) private JCasHashMapSubMap
getSubMap
(int hash) static int
hashInt
(int k1) put
(FeatureStructureImpl value) (package private) static void
setDEFAULT_CONCURRENCY_LEVEL
(int dEFAULT_CONCURRENCY_LEVEL) void
(package private) static int
sizeAdjustedConcurrency
(int curMapSize)
-
Field Details
-
TUNE
static final boolean TUNE- See Also:
-
check
static final boolean check- See Also:
-
DEFAULT_CONCURRENCY_LEVEL
static int DEFAULT_CONCURRENCY_LEVELmust be a power of 2, > 0 package private for testing not final to allow test case to reset it must not be changed during multi-thread operation -
loadFactor
private final float loadFactor- See Also:
-
initialCapacity
private final int initialCapacity -
useCache
private final boolean useCache -
concurrencyLevel
private final int concurrencyLevel -
concurrencyBitmask
private final int concurrencyBitmask -
concurrencyLevelBits
private final int concurrencyLevelBits -
subMaps
-
subMapInitialCapacity
private final int subMapInitialCapacity -
oneSubmap
-
C1
private static final int C1- See Also:
-
C2
private static final int C2- See Also:
-
seed
private static final int seed- See Also:
-
-
Constructor Details
-
JCasHashMap
JCasHashMap(int capacity, boolean doUseCache) -
JCasHashMap
JCasHashMap(int capacity, boolean doUseCache, int aConcurrencyLevel)
-
-
Method Details
-
getDEFAULT_CONCURRENCY_LEVEL
static int getDEFAULT_CONCURRENCY_LEVEL() -
setDEFAULT_CONCURRENCY_LEVEL
static void setDEFAULT_CONCURRENCY_LEVEL(int dEFAULT_CONCURRENCY_LEVEL) -
concurrencyLimitedByInitialCapacity
static boolean concurrencyLimitedByInitialCapacity(int currentConcurrencyLevel, int curMapSize) initial capacity (other than testing), is by default (from JCasImpl) is bigger of 256 and cas heap initial size (500,000) / 16 = 31K but users may set it lower in their uima configuration We use the current capacity of the JCasHashMap to set the concurrency limit- Parameters:
casCapacity
- the capacity- Returns:
- true if the concurrency is limited, and could increase with reallocation
-
sizeAdjustedConcurrency
static int sizeAdjustedConcurrency(int curMapSize) -
clear
public void clear() -
getSubMap
-
getReserve
-
put
-
hashInt
public static int hashInt(int k1) -
getCapacities
int[] getCapacities() -
getCapacity
int getCapacity() -
getApproximateSize
int getApproximateSize() -
showHistogram
public void showHistogram() -
getConcurrencyLevel
public int getConcurrencyLevel()
-