diff options
author | Antonis Geralis <43617260+planetis-m@users.noreply.github.com> | 2021-07-26 16:22:42 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-26 15:22:42 +0200 |
commit | d4c3a09286d1cbb534ed5dbd21d99a18670b3427 (patch) | |
tree | 2144aef0e357b019eb570c104676f37b2ef51dfa | |
parent | 158d7c7a7a8977f5937311b98bc381399f31db8d (diff) | |
download | Nim-d4c3a09286d1cbb534ed5dbd21d99a18670b3427.tar.gz |
optimize for the non-throwing case (#18587)
-rw-r--r-- | lib/pure/collections/tables.nim | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/lib/pure/collections/tables.nim b/lib/pure/collections/tables.nim index b845f57cc..403345755 100644 --- a/lib/pure/collections/tables.nim +++ b/lib/pure/collections/tables.nim @@ -227,6 +227,12 @@ template dataLen(t): untyped = len(t.data) 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 = ## retrieves the value at `t[key]`. The value can be modified. ## 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) if index >= 0: result = t.data[index].val else: - when compiles($key): - raise newException(KeyError, "key not found: " & $key) - else: - raise newException(KeyError, "key not found") + raiseKeyError(key) proc enlarge[A, B](t: var Table[A, B]) = var n: KeyValuePairSeq[A, B] |