Skip to content

Commit 734da9e

Browse files
narimiranAraq
authored andcommitted
fixes #11764, faster hashing of (u)int (#12407)
1 parent 5f5ac8c commit 734da9e

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

lib/pure/collections/sets.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,9 +1019,9 @@ when isMainModule and not defined(release):
10191019
# --> {1, 3, 5}
10201020

10211021
block toSeqAndString:
1022-
var a = toHashSet([2, 4, 5])
1022+
var a = toHashSet([2, 7, 5])
10231023
var b = initHashSet[int]()
1024-
for x in [2, 4, 5]: b.incl(x)
1024+
for x in [2, 7, 5]: b.incl(x)
10251025
assert($a == $b)
10261026
#echo a
10271027
#echo toHashSet(["no", "esc'aping", "is \" provided"])

lib/pure/hashes.nim

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,32 @@ proc hash*[T: proc](x: T): Hash {.inline.} =
112112
else:
113113
result = hash(pointer(x))
114114

115+
const
116+
prime = uint(11)
117+
115118
proc hash*(x: int): Hash {.inline.} =
116119
## Efficient hashing of integers.
117-
result = x
120+
result = cast[Hash](cast[uint](x) * prime)
118121

119122
proc hash*(x: int64): Hash {.inline.} =
120123
## Efficient hashing of `int64` integers.
121-
result = cast[int](x)
124+
result = cast[Hash](cast[uint](x) * prime)
122125

123126
proc hash*(x: uint): Hash {.inline.} =
124127
## Efficient hashing of unsigned integers.
125-
result = cast[int](x)
128+
result = cast[Hash](x * prime)
126129

127130
proc hash*(x: uint64): Hash {.inline.} =
128131
## Efficient hashing of `uint64` integers.
129-
result = cast[int](x)
132+
result = cast[Hash](cast[uint](x) * prime)
130133

131134
proc hash*(x: char): Hash {.inline.} =
132135
## Efficient hashing of characters.
133-
result = ord(x)
136+
result = cast[Hash](cast[uint](ord(x)) * prime)
134137

135138
proc hash*[T: Ordinal](x: T): Hash {.inline.} =
136139
## Efficient hashing of other ordinal types (e.g. enums).
137-
result = ord(x)
140+
result = cast[Hash](cast[uint](ord(x)) * prime)
138141

139142
proc hash*(x: float): Hash {.inline.} =
140143
## Efficient hashing of floats.

tests/collections/ttables.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ block tablesref:
233233
for y in 0..1:
234234
assert t[(x,y)] == $x & $y
235235
assert($t ==
236-
"{(x: 0, y: 1): \"01\", (x: 0, y: 0): \"00\", (x: 1, y: 0): \"10\", (x: 1, y: 1): \"11\"}")
236+
"{(x: 1, y: 1): \"11\", (x: 0, y: 0): \"00\", (x: 0, y: 1): \"01\", (x: 1, y: 0): \"10\"}")
237237

238238
block tableTest2:
239239
var t = newTable[string, float]()
@@ -340,7 +340,7 @@ block tablesref:
340340
block anonZipTest:
341341
let keys = @['a','b','c']
342342
let values = @[1, 2, 3]
343-
doAssert "{'a': 1, 'b': 2, 'c': 3}" == $ toTable zip(keys, values)
343+
doAssert "{'c': 3, 'a': 1, 'b': 2}" == $ toTable zip(keys, values)
344344

345345
block clearTableTest:
346346
var t = newTable[string, float]()

tests/collections/ttablesthreads.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ block tableTest1:
4848
for y in 0..1:
4949
assert t[(x,y)] == $x & $y
5050
assert($t ==
51-
"{(x: 0, y: 1): \"01\", (x: 0, y: 0): \"00\", (x: 1, y: 0): \"10\", (x: 1, y: 1): \"11\"}")
51+
"{(x: 1, y: 1): \"11\", (x: 0, y: 0): \"00\", (x: 0, y: 1): \"01\", (x: 1, y: 0): \"10\"}")
5252

5353
block tableTest2:
5454
var t = initTable[string, float]()

0 commit comments

Comments
 (0)