From 1029e4b787ef46e391fe096d0f59c16816adc394 Mon Sep 17 00:00:00 2001 From: Huy Doan Date: Tue, 3 Nov 2020 22:45:32 +0700 Subject: [PATCH] add more optimizations --- sam.nim | 15 +++++++-------- tests/test.nim | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/sam.nim b/sam.nim index 676abbf..c0f4c07 100644 --- a/sam.nim +++ b/sam.nim @@ -99,7 +99,7 @@ proc loads(target: var any, m: Mapper, pos = 0) = assert tok.kind != JSMN_UNDEFINED when defined(verbose): echo "tok ", tok, " value: ", tok.getValue(m.json) - if tok.parent == pos: + if likely(tok.parent == pos): assert tok.kind == JSMN_STRING key = tok.getValue(m.json) for n, v in fieldPairs(target): @@ -130,7 +130,7 @@ proc loads(target: var any, m: Mapper, pos = 0) = tok = m.tokens[i] when defined(verbose): echo "array ", i, " ", tok.parent, " ", pos, " ", tok, " ", getValue(tok, m.json) - if tok.parent != pos: + if unlikely(tok.parent != pos): inc(i) continue loads(target[x], m, i) @@ -146,16 +146,15 @@ proc loads(target: var any, m: Mapper, pos = 0) = target = unescape(m.tokens[pos].getValue(m.json), "", "") elif target is bool: assert m.tokens[pos].kind == JSMN_PRIMITIVE - let value = m.tokens[pos].getValue(m.json) - target = value[0] == 't' + target = m.json[m.tokens[pos].start] == 't' elif target is SomeFloat: assert m.tokens[pos].kind == JSMN_PRIMITIVE target = parseFloat(m.tokens[pos].getValue(m.json)) elif target is char: assert m.tokens[pos].kind == JSMN_STRING - let value = m.tokens[pos].getValue(m.json) - if value.len > 0: - target = value[0] + assert m.tokens[pos].start < m.tokens[pos].stop + if likely(m.tokens[pos].start < m.tokens[pos].stop): + target = m.json[m.tokens[pos].start] elif target is enum: assert m.tokens[pos].kind == JSMN_STRING let value = m.tokens[pos].getValue(m.json) @@ -307,7 +306,7 @@ proc dumps*(t: auto, x: var string, namingConverter: NamingConverter = nil) = var first = true x.add "{" for n, v in fieldPairs(t): - if first: + if unlikely(first): first = false else: x.add "," diff --git a/tests/test.nim b/tests/test.nim index 0010315..9bac478 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -41,7 +41,7 @@ t1.done = wontfix t1.tags = ["test", "blah"] t1.categories = @["works", "urgent"] t1.user = u1 -t1.published = false +t1.published = true t1.points = [5,6,7,8,9] t1.watchers = @[u1, u2] t1.completed = true