optimize for the non-throwing case (#18587)

This commit is contained in:
Antonis Geralis 2021-07-26 16:22:42 +03:00 • committed by GitHub
commit d4c3a09286
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -227,6 +227,12 @@ template dataLen(t): untyped = len(t.data)
include tableimpl include tableimpl
proc raiseKeyError[T](key: T) {.noinline, noreturn.} =
when compiles($key):
raise newException(KeyError, "key not found: " & $key)
else:
raise newException(KeyError, "key not found")
template get(t, key): untyped = template get(t, key): untyped =
## retrieves the value at `t[key]`. The value can be modified. ## retrieves the value at `t[key]`. The value can be modified.
## If `key` is not in `t`, the `KeyError` exception is raised. ## If `key` is not in `t`, the `KeyError` exception is raised.
@ -235,10 +241,7 @@ template get(t, key): untyped =
var index = rawGet(t, key, hc) var index = rawGet(t, key, hc)
if index >= 0: result = t.data[index].val if index >= 0: result = t.data[index].val
else: else:
when compiles($key): raiseKeyError(key)
raise newException(KeyError, "key not found: " & $key)
else:
raise newException(KeyError, "key not found")
proc enlarge[A, B](t: var Table[A, B]) = proc enlarge[A, B](t: var Table[A, B]) =
var n: KeyValuePairSeq[A, B] var n: KeyValuePairSeq[A, B]