Skip to content

Commit

Permalink
add more optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ba0f3 committed Nov 3, 2020
1 parent a4ee065 commit 1029e4b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
15 changes: 7 additions & 8 deletions sam.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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 ","
Expand Down
2 changes: 1 addition & 1 deletion tests/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1029e4b

Please sign in to comment.