Forráskód Böngészése

Added -D no_map_cache, which disables the cachedIndex/cachedKey logic from Map implementations. These are not safe in multithreaded environments, where you would expect read-only access to a Map to be safe.

Dan Ogles 10 éve
szülő
commit
72c4103ddf

+ 25 - 4
std/cs/_std/haxe/ds/IntMap.hx

@@ -46,12 +46,16 @@ import cs.NativeArray;
 	private var nOccupied:Int;
 	private var upperBound:Int;
 
+#if !no_map_cache
 	private var cachedKey:Int;
 	private var cachedIndex:Int;
+#end
 
 	public function new() : Void
 	{
+#if !no_map_cache
 		cachedIndex = -1;
+#end
 	}
 
 	public function set( key : Int, value : T ) : Void
@@ -135,17 +139,20 @@ import cs.NativeArray;
 	public function get( key : Int ) : Null<T>
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
-
+#end
 			return vals[idx];
 		}
 
@@ -155,17 +162,20 @@ import cs.NativeArray;
 	private function getDefault( key : Int, def : T ) : T
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
-
+#end
 			return vals[idx];
 		}
 
@@ -175,16 +185,20 @@ import cs.NativeArray;
 	public function exists( key : Int ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return true;
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
+#end
 
 			return true;
 		}
@@ -195,7 +209,9 @@ import cs.NativeArray;
 	public function remove( key : Int ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (! (cachedKey == key && ( (idx = cachedIndex) != -1 )))
+#end
 		{
 			idx = lookup(key);
 		}
@@ -204,9 +220,10 @@ import cs.NativeArray;
 		{
 			return false;
 		} else {
+#if !no_map_cache
 			if (cachedKey == key)
 				cachedIndex = -1;
-
+#end
 			if (!isEither(flags, idx))
 			{
 				setIsDelTrue(flags, idx);
@@ -253,9 +270,11 @@ import cs.NativeArray;
 
 		if (j != 0)
 		{ //rehashing is required
+#if !no_map_cache
 			//resetting cache
 			cachedKey = 0;
 			cachedIndex = -1;
+#end
 
 			j = -1;
 			var nBuckets = nBuckets, _keys = _keys, vals = vals, flags = flags;
@@ -438,8 +457,10 @@ private class IntMapKeyIterator<T> {
 
 	public function next():Int {
 		var ret = m._keys[i];
+#if !no_map_cache
 		m.cachedIndex = i;
 		m.cachedKey = ret;
+#end
 		i++;
 		return ret;
 	}
@@ -471,4 +492,4 @@ private class IntMapValueIterator<T> {
 	public inline function next():T {
 		return m.vals[i++];
 	}
-}
+}

+ 26 - 0
std/cs/_std/haxe/ds/ObjectMap.hx

@@ -46,8 +46,10 @@ import cs.NativeArray;
 	private var nOccupied:Int;
 	private var upperBound:Int;
 
+#if !no_map_cache
 	private var cachedKey:K;
 	private var cachedIndex:Int;
+#end
 
 #if DEBUG_HASHTBL
 	private var totalProbes:Int;
@@ -58,7 +60,9 @@ import cs.NativeArray;
 
 	public function new() : Void
 	{
+#if !no_map_cache
 		cachedIndex = -1;
+#end
 	}
 
 	public function set( key : K, value : V ) : Void
@@ -122,8 +126,10 @@ import cs.NativeArray;
 			vals[x] = value;
 		}
 
+#if !no_map_cache
 		cachedIndex = x;
 		cachedKey = key;
+#end
 	}
 
 	@:final private function lookup( key : K ) : Int
@@ -188,9 +194,11 @@ import cs.NativeArray;
 
 		if (j != 0)
 		{ //rehashing is required
+#if !no_map_cache
 			//resetting cache
 			cachedKey = null;
 			cachedIndex = -1;
+#end
 
 			j = -1;
 			var nBuckets = nBuckets, _keys = _keys, vals = vals, hashes = hashes;
@@ -263,16 +271,20 @@ import cs.NativeArray;
 	public function get( key : K ) : Null<V>
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
+#end
 
 			return vals[idx];
 		}
@@ -283,16 +295,20 @@ import cs.NativeArray;
 	private function getDefault( key : K, def : V ) : V
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
+#end
 
 			return vals[idx];
 		}
@@ -303,16 +319,20 @@ import cs.NativeArray;
 	public function exists( key : K ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return true;
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
+#end
 
 			return true;
 		}
@@ -323,7 +343,9 @@ import cs.NativeArray;
 	public function remove( key : K ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (! (cachedKey == key && ( (idx = cachedIndex) != -1 )))
+#end
 		{
 			idx = lookup(key);
 		}
@@ -332,8 +354,10 @@ import cs.NativeArray;
 		{
 			return false;
 		} else {
+#if !no_map_cache
 			if (cachedKey == key)
 				cachedIndex = -1;
+#end
 
 			hashes[idx] = FLAG_DEL;
 			_keys[idx] = null;
@@ -366,8 +390,10 @@ import cs.NativeArray;
 			},
 			next: function() {
 				var ret = _keys[i];
+#if !no_map_cache
 				cachedIndex = i;
 				cachedKey = ret;
+#end
 
 				i = i + 1;
 				return ret;

+ 26 - 4
std/cs/_std/haxe/ds/StringMap.hx

@@ -46,8 +46,10 @@ import cs.NativeArray;
 	private var nOccupied:Int;
 	private var upperBound:Int;
 
+#if !no_map_cache
 	private var cachedKey:String;
 	private var cachedIndex:Int;
+#end
 
 #if DEBUG_HASHTBL
 	private var totalProbes:Int;
@@ -58,7 +60,9 @@ import cs.NativeArray;
 
 	public function new() : Void
 	{
+#if !no_map_cache
 		cachedIndex = -1;
+#end
 	}
 
 	public function set( key : String, value : T ) : Void
@@ -122,8 +126,10 @@ import cs.NativeArray;
 			vals[x] = value;
 		}
 
+#if !no_map_cache
 		cachedIndex = x;
 		cachedKey = key;
+#end
 	}
 
 	@:final private function lookup( key : String ) : Int
@@ -189,8 +195,10 @@ import cs.NativeArray;
 		if (j != 0)
 		{ //rehashing is required
 			//resetting cache
+#if !no_map_cache
 			cachedKey = null;
 			cachedIndex = -1;
+#end
 
 			j = -1;
 			var nBuckets = nBuckets, _keys = _keys, vals = vals, hashes = hashes;
@@ -263,17 +271,19 @@ import cs.NativeArray;
 	public function get( key : String ) : Null<T>
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
-
+#end
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
-
+#end
 			return vals[idx];
 		}
 
@@ -283,16 +293,20 @@ import cs.NativeArray;
 	private function getDefault( key : String, def : T ) : T
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
+#end
 
 			return vals[idx];
 		}
@@ -303,17 +317,20 @@ import cs.NativeArray;
 	public function exists( key : String ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return true;
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
-
+#end
 			return true;
 		}
 
@@ -323,7 +340,9 @@ import cs.NativeArray;
 	public function remove( key : String ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (! (cachedKey == key && ( (idx = cachedIndex) != -1 )))
+#end
 		{
 			idx = lookup(key);
 		}
@@ -332,9 +351,10 @@ import cs.NativeArray;
 		{
 			return false;
 		} else {
+#if !no_map_cache
 			if (cachedKey == key)
 				cachedIndex = -1;
-
+#end
 			hashes[idx] = FLAG_DEL;
 			_keys[idx] = null;
 			vals[idx] = null;
@@ -471,8 +491,10 @@ private class StringMapKeyIterator<T> {
 
 	public function next():String {
 		var ret = m._keys[i];
+#if !no_map_cache
 		m.cachedIndex = i;
 		m.cachedKey = ret;
+#end
 		i++;
 		return ret;
 	}