Skip to content

Commit

Permalink
[backport] fix nim-lang#11764, faster hashing of (u)int
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Oct 10, 2019
1 parent 0a29c05 commit 0b51631
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/pure/hashes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,32 @@ proc hash*[T: proc](x: T): Hash {.inline.} =
else:
result = hash(pointer(x))

const
prime = 31

proc hash*(x: int): Hash {.inline.} =
## Efficient hashing of integers.
result = x
result = x * prime

proc hash*(x: int64): Hash {.inline.} =
## Efficient hashing of `int64` integers.
result = cast[int](x)
result = cast[int](x) * prime

proc hash*(x: uint): Hash {.inline.} =
## Efficient hashing of unsigned integers.
result = cast[int](x)
result = cast[int](x) * prime

proc hash*(x: uint64): Hash {.inline.} =
## Efficient hashing of `uint64` integers.
result = cast[int](x)
result = cast[int](x) * prime

proc hash*(x: char): Hash {.inline.} =
## Efficient hashing of characters.
result = ord(x)
result = ord(x) * prime

proc hash*[T: Ordinal](x: T): Hash {.inline.} =
## Efficient hashing of other ordinal types (e.g. enums).
result = ord(x)
result = ord(x) * prime

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

0 comments on commit 0b51631

Please sign in to comment.