From 0b51631be1fbf20851660043005bd4d9ceb92ec6 Mon Sep 17 00:00:00 2001 From: narimiran Date: Thu, 10 Oct 2019 15:31:56 +0200 Subject: [PATCH] [backport] fix #11764, faster hashing of (u)int --- lib/pure/hashes.nim | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/pure/hashes.nim b/lib/pure/hashes.nim index cb0250dbd388f..c4f929c3f9a8e 100644 --- a/lib/pure/hashes.nim +++ b/lib/pure/hashes.nim @@ -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.