summary refs log tree commit diff stats
path: root/lib/pure/hashes.nim
diff options
context:
space:
mode:
authorc-blake <c-blake@users.noreply.github.com>2024-07-07 10:51:42 +0000
committerGitHub <noreply@github.com>2024-07-07 12:51:42 +0200
commit4faa15f3ad6504c3ba808e63ecd729d3fc3cb78a (patch)
treef60e8f2524195c8605d6f3e8e6c478a5534f0ab9 /lib/pure/hashes.nim
parent1dcc364cd2e284f831735e0fe9b988b08c82ac4f (diff)
downloadNim-4faa15f3ad6504c3ba808e63ecd729d3fc3cb78a.tar.gz
Replacement PR for https://github.com/nim-lang/Nim/pull/23779 that (#23793)
makes new hash the default, with an opt-out (& js-no-big-int) define.
Also update changelog (& fix one typo).

Only really expect the chronos hash-order sensitive test to fail until
they merge that PR and tag a new release.
Diffstat (limited to 'lib/pure/hashes.nim')
-rw-r--r--lib/pure/hashes.nim68
1 files changed, 42 insertions, 26 deletions
diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim
index 9e16cea9e..14d027107 100644
--- a/lib/pure/hashes.nim
+++ b/lib/pure/hashes.nim
@@ -518,6 +518,19 @@ proc hashFarm(s: openArray[byte]): uint64 {.inline.} =
   swap z, x
   len16 len16(v[0],w[0],mul) + shiftMix(y)*k0 + z, len16(v[1],w[1],mul) + x, mul
 
+template jsNoInt64: untyped =
+  when defined js:
+    when compiles(compileOption("jsbigint64")):
+      when not compileOption("jsbigint64"): true
+      else: false
+    else: false
+  else: false
+const sHash2 = (when defined(nimStringHash2) or jsNoInt64(): true else: false)
+
+template maybeFailJS_Number =
+  when jsNoInt64() and not defined(nimStringHash2):
+    {.error: "Must use `-d:nimStringHash2` when using `--jsbigint64:off`".}
+
 proc hash*(x: string): Hash =
   ## Efficient hashing of strings.
   ##
@@ -526,13 +539,13 @@ proc hash*(x: string): Hash =
   ## * `hashIgnoreCase <#hashIgnoreCase,string>`_
   runnableExamples:
     doAssert hash("abracadabra") != hash("AbracadabrA")
-
-  when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
+  maybeFailJS_Number()
+  when not sHash2:
     result = cast[Hash](hashFarm(toOpenArrayByte(x, 0, x.high)))
   else:
-    when nimvm:
-      result = hashVmImpl(x, 0, high(x))
-    else:
+    #when nimvm:
+    #  result = hashVmImpl(x, 0, high(x))
+    when true:
       result = murmurHash(toOpenArrayByte(x, 0, high(x)))
 
 proc hash*(x: cstring): Hash =
@@ -542,21 +555,22 @@ proc hash*(x: cstring): Hash =
     doAssert hash(cstring"AbracadabrA") == hash("AbracadabrA")
     doAssert hash(cstring"abracadabra") != hash(cstring"AbracadabrA")
 
-  when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
+  maybeFailJS_Number()
+  when not sHash2:
     when defined js:
       let xx = $x
       result = cast[Hash](hashFarm(toOpenArrayByte(xx, 0, xx.high)))
     else:
       result = cast[Hash](hashFarm(toOpenArrayByte(x, 0, x.high)))
   else:
-    when nimvm:
-      hashVmImpl(x, 0, high(x))
-    else:
+    #when nimvm:
+    #  result = hashVmImpl(x, 0, high(x))
+    when true:
       when not defined(js):
-        murmurHash(toOpenArrayByte(x, 0, x.high))
+        result = murmurHash(toOpenArrayByte(x, 0, x.high))
       else:
         let xx = $x
-        murmurHash(toOpenArrayByte(xx, 0, high(xx)))
+        result = murmurHash(toOpenArrayByte(xx, 0, high(xx)))
 
 proc hash*(sBuf: string, sPos, ePos: int): Hash =
   ## Efficient hashing of a string buffer, from starting
@@ -567,7 +581,8 @@ proc hash*(sBuf: string, sPos, ePos: int): Hash =
     var a = "abracadabra"
     doAssert hash(a, 0, 3) == hash(a, 7, 10)
 
-  when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
+  maybeFailJS_Number()
+  when not sHash2:
     result = cast[Hash](hashFarm(toOpenArrayByte(sBuf, sPos, ePos)))
   else:
     murmurHash(toOpenArrayByte(sBuf, sPos, ePos))
@@ -705,17 +720,17 @@ proc hash*[A](x: openArray[A]): Hash =
   ## Efficient hashing of arrays and sequences.
   ## There must be a `hash` proc defined for the element type `A`.
   when A is byte:
-    when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
+    when not sHash2:
       result = cast[Hash](hashFarm(x))
     else:
       result = murmurHash(x)
   elif A is char:
-    when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
+    when not sHash2:
       result = cast[Hash](hashFarm(toOpenArrayByte(x, 0, x.high)))
     else:
-      when nimvm:
-        result = hashVmImplChar(x, 0, x.high)
-      else:
+      #when nimvm:
+      #  result = hashVmImplChar(x, 0, x.high)
+      when true:
         result = murmurHash(toOpenArrayByte(x, 0, x.high))
   else:
     result = 0
@@ -732,22 +747,23 @@ proc hash*[A](aBuf: openArray[A], sPos, ePos: int): Hash =
   runnableExamples:
     let a = [1, 2, 5, 1, 2, 6]
     doAssert hash(a, 0, 1) == hash(a, 3, 4)
-
   when A is byte:
-    when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
+    maybeFailJS_Number()
+    when not sHash2:
       result = cast[Hash](hashFarm(toOpenArray(aBuf, sPos, ePos)))
     else:
-      when nimvm:
-        result = hashVmImplByte(aBuf, sPos, ePos)
-      else:
+      #when nimvm:
+      #  result = hashVmImplByte(aBuf, sPos, ePos)
+      when true:
         result = murmurHash(toOpenArray(aBuf, sPos, ePos))
   elif A is char:
-    when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
+    maybeFailJS_Number()
+    when not sHash2:
       result = cast[Hash](hashFarm(toOpenArrayByte(aBuf, sPos, ePos)))
     else:
-      when nimvm:
-        result = hashVmImplChar(aBuf, sPos, ePos)
-      else:
+      #when nimvm:
+      #  result = hashVmImplChar(aBuf, sPos, ePos)
+      when true:
         result = murmurHash(toOpenArrayByte(aBuf, sPos, ePos))
   else:
     for i in sPos .. ePos: