Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Replacement PR for https://github.com/nim-lang/Nim/pull/23779 that" #23810

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
[//]: # "Additions:"

- Added `newStringUninit` to system, which creates a new string of length `len` like `newString` but with uninitialized content.
- Added `setLenUninit` to system, which doesn't initialize
- Added `setLenUninit` to system, which doesn't initalize
slots when enlarging a sequence.
- Added `hasDefaultValue` to `std/typetraits` to check if a type has a valid default value.
- Added Viewport API for the JavaScript targets in the `dom` module.
Expand All @@ -39,12 +39,9 @@ slots when enlarging a sequence.
objects the cyclic collector did free. If the number is zero that is a strong indicator that you can use `--mm:arc`
instead of `--mm:orc`.
- A `$` template is provided for `Path` in `std/paths`.
- `std/hashes.hash(x:string)` changed to produce a 64-bit string `Hash` (based
on Google's Farm Hash) which is also often faster than the present one. Define
`nimStringHash2` to get the old values back. `--jsbigint=off` mode always only
produces the old values. This may impact your automated tests if they depend
on hash order in some obvious or indirect way. Using `sorted` or `OrderedTable`
is often an easy workaround.
- `nimPreviewHashFarm` has been added to `lib/pure/hashes.nim` to default to a
64-bit string `Hash` (based upon Google's Farm Hash) which is also faster than
the present one. At present, this is incompatible with `--jsbigint=off` mode.

[//]: # "Deprecations:"

Expand Down
68 changes: 26 additions & 42 deletions lib/pure/hashes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -518,19 +518,6 @@ 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.
##
Expand All @@ -539,13 +526,13 @@ proc hash*(x: string): Hash =
## * `hashIgnoreCase <#hashIgnoreCase,string>`_
runnableExamples:
doAssert hash("abracadabra") != hash("AbracadabrA")
maybeFailJS_Number()
when not sHash2:

when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
result = cast[Hash](hashFarm(toOpenArrayByte(x, 0, x.high)))
else:
#when nimvm:
# result = hashVmImpl(x, 0, high(x))
when true:
when nimvm:
result = hashVmImpl(x, 0, high(x))
else:
result = murmurHash(toOpenArrayByte(x, 0, high(x)))

proc hash*(x: cstring): Hash =
Expand All @@ -555,22 +542,21 @@ proc hash*(x: cstring): Hash =
doAssert hash(cstring"AbracadabrA") == hash("AbracadabrA")
doAssert hash(cstring"abracadabra") != hash(cstring"AbracadabrA")

maybeFailJS_Number()
when not sHash2:
when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
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:
# result = hashVmImpl(x, 0, high(x))
when true:
when nimvm:
hashVmImpl(x, 0, high(x))
else:
when not defined(js):
result = murmurHash(toOpenArrayByte(x, 0, x.high))
murmurHash(toOpenArrayByte(x, 0, x.high))
else:
let xx = $x
result = murmurHash(toOpenArrayByte(xx, 0, high(xx)))
murmurHash(toOpenArrayByte(xx, 0, high(xx)))

proc hash*(sBuf: string, sPos, ePos: int): Hash =
## Efficient hashing of a string buffer, from starting
Expand All @@ -581,8 +567,7 @@ proc hash*(sBuf: string, sPos, ePos: int): Hash =
var a = "abracadabra"
doAssert hash(a, 0, 3) == hash(a, 7, 10)

maybeFailJS_Number()
when not sHash2:
when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
result = cast[Hash](hashFarm(toOpenArrayByte(sBuf, sPos, ePos)))
else:
murmurHash(toOpenArrayByte(sBuf, sPos, ePos))
Expand Down Expand Up @@ -720,17 +705,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 not sHash2:
when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
result = cast[Hash](hashFarm(x))
else:
result = murmurHash(x)
elif A is char:
when not sHash2:
when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
result = cast[Hash](hashFarm(toOpenArrayByte(x, 0, x.high)))
else:
#when nimvm:
# result = hashVmImplChar(x, 0, x.high)
when true:
when nimvm:
result = hashVmImplChar(x, 0, x.high)
else:
result = murmurHash(toOpenArrayByte(x, 0, x.high))
else:
result = 0
Expand All @@ -747,23 +732,22 @@ 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:
maybeFailJS_Number()
when not sHash2:
when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
result = cast[Hash](hashFarm(toOpenArray(aBuf, sPos, ePos)))
else:
#when nimvm:
# result = hashVmImplByte(aBuf, sPos, ePos)
when true:
when nimvm:
result = hashVmImplByte(aBuf, sPos, ePos)
else:
result = murmurHash(toOpenArray(aBuf, sPos, ePos))
elif A is char:
maybeFailJS_Number()
when not sHash2:
when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
result = cast[Hash](hashFarm(toOpenArrayByte(aBuf, sPos, ePos)))
else:
#when nimvm:
# result = hashVmImplChar(aBuf, sPos, ePos)
when true:
when nimvm:
result = hashVmImplChar(aBuf, sPos, ePos)
else:
result = murmurHash(toOpenArrayByte(aBuf, sPos, ePos))
else:
for i in sPos .. ePos:
Expand Down
2 changes: 1 addition & 1 deletion tests/int/tints.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "; --backend:js --jsbigint64:off -d:nimStringHash2; --backend:js --jsbigint64:on"
matrix: "; --backend:js --jsbigint64:off; --backend:js --jsbigint64:on"
output: '''
0 0
0 0
Expand Down
2 changes: 1 addition & 1 deletion tests/js/ttypedarray.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "--jsbigint64:off -d:nimStringHash2; --jsbigint64:on"
matrix: "--jsbigint64:off; --jsbigint64:on"
"""

import std/private/jsutils
Expand Down
21 changes: 14 additions & 7 deletions tests/parallel/tsendtwice.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
discard """
output: '''ob2 @[]
ob @[]
ob3 @[]
3
ob2 @[]
ob @[]
ob3 @[]
'''
matrix: "--mm:refc"
"""

# bug #4776

import tables, algorithm
import tables

type
Base* = ref object of RootObj
Expand All @@ -27,21 +35,20 @@ globalTable.add("ob", d)
globalTable.add("ob2", d)
globalTable.add("ob3", d)

proc `<`(x, y: seq[int]): bool = x.len < y.len
proc kvs(t: TableRef[string, Base]): seq[(string, seq[int])] =
for k, v in t.pairs: result.add (k, v.someSeq)
result.sort

proc testThread(channel: ptr TableChannel) {.thread.} =
globalTable = channel[].recv()
for k, v in pairs globaltable:
echo k, " ", v.someSeq
var myObj: Base
deepCopy(myObj, globalTable["ob"])
myObj.someSeq = newSeq[int](100)
let table = channel[].recv() # same table
echo table.len
for k, v in mpairs table:
echo k, " ", v.someSeq
assert(table.contains("ob")) # fails!
assert(table.contains("ob2")) # fails!
assert(table.contains("ob3")) # fails!
assert table.kvs == globalTable.kvs # Last to see above spot checks first

var channel: TableChannel

Expand Down
17 changes: 5 additions & 12 deletions tests/stdlib/thashes.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "--mm:refc; --mm:orc; --backend:cpp; --backend:js --jsbigint64:on; --backend:c -d:nimStringHash2; --backend:cpp -d:nimStringHash2; --backend:js -d:nimStringHash2"
matrix: "--mm:refc; --mm:orc; --backend:cpp; --backend:js --jsbigint64:on; --backend:js --jsbigint64:off; --backend:c -d:nimPreviewHashFarm; --backend:cpp -d:nimPreviewHashFarm; --backend:js -d:nimPreviewHashFarm"
"""

import std/hashes
Expand Down Expand Up @@ -46,18 +46,10 @@ block hashes:
else:
doAssert hashWangYi1(456) == -6421749900419628582

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)

block empty:
const emptyStrHash = # Hash=int=4B on js even w/--jsbigint64:on => cast[Hash]
when sHash2: 0 else: cast[Hash](-7286425919675154353i64)
when defined nimPreviewHashFarm: cast[Hash](-7286425919675154353i64)
else: 0
var
a = ""
b = newSeq[char]()
Expand Down Expand Up @@ -104,7 +96,8 @@ block largeSize: # longer than 4 characters
proc main() =
doAssert hash(0.0) == hash(0)
# bug #16061
when not sHash2: # Hash=int=4B on js even w/--jsbigint64:on => cast[Hash]
when defined nimPreviewHashFarm: # Default switched -> `not nimStringHash2`
# Hash=int=4B on js even w/--jsbigint64:on => cast[Hash]
doAssert hash(cstring"abracadabra") == cast[Hash](-1119910118870047694i64)
else:
doAssert hash(cstring"abracadabra") == 97309975
Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/tjson.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "; --backend:cpp; --backend:js --jsbigint64:off -d:nimStringHash2; --backend:js --jsbigint64:on"
matrix: "; --backend:cpp; --backend:js --jsbigint64:off; --backend:js --jsbigint64:on"
"""


Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/trandom.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
discard """
joinable: false # to avoid messing with global rand state
matrix: "--mm:refc; --mm:orc; --backend:js --jsbigint64:off -d:nimStringHash2; --backend:js --jsbigint64:on"
matrix: "--mm:refc; --mm:orc; --backend:js --jsbigint64:off; --backend:js --jsbigint64:on"
"""
import std/[assertions, formatfloat]
import std/[random, math, stats, sets, tables]
Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/tstrutils.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "--mm:refc; --mm:orc; --backend:cpp; --backend:js --jsbigint64:off -d:nimStringHash2; --backend:js --jsbigint64:on"
matrix: "--mm:refc; --mm:orc; --backend:cpp; --backend:js --jsbigint64:off; --backend:js --jsbigint64:on"
"""

import std/strutils
Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/ttimes.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "--mm:refc; --mm:orc; --backend:js --jsbigint64:on; --backend:js --jsbigint64:off -d:nimStringHash2"
matrix: "--mm:refc; --mm:orc; --backend:js --jsbigint64:on; --backend:js --jsbigint64:off"
"""

import times, strutils, unittest
Expand Down
2 changes: 1 addition & 1 deletion tests/system/tdollars.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "--mm:refc; --mm:orc; --backend:cpp; --backend:js --jsbigint64:off -d:nimStringHash2; --backend:js --jsbigint64:on"
matrix: "--mm:refc; --mm:orc; --backend:cpp; --backend:js --jsbigint64:off; --backend:js --jsbigint64:on"
"""

#[
Expand Down
Loading