Skip to content

Commit

Permalink
0.3.0: faster arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed Nov 5, 2020
1 parent 090614c commit e729bab
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 127 deletions.
84 changes: 25 additions & 59 deletions docs/bench.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 20 additions & 55 deletions docs/packed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 13 additions & 8 deletions jason.nim
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,24 @@ macro jason*[I, T](a: array[I, T]): Json =
error "unexpected infix range:\n" & treeRepr(ranger)

# okay, let's do this thing
let js = bindSym"jason"
let amper = bindSym"&" # we need our Json concatenator
let js = ident"jason"
let s = genSym(nskVar, "jason")
var list = newStmtList()
list.add jasonify"["
# we'll make adding strings to our accumulating string easier...
template addString(x: typed): NimNode {.dirty.} =
# also cast the argument to a string just for correctness
list.add newCall(newDotExpr(s, bindSym"add"), newCall(ident"string", x))
# iterate over the array by index and add each item to the string
list.add newVarStmt(s, newLit"[")
for index in ranger[1].intVal .. ranger[2].intVal:
if index != 0:
list.add jasonify"," # comma between each element
addString newLit"," # comma between each element
# build and invoke the array access expression `a[i]`
let access = newTree(nnkBracketExpr, a, index.newLit)
list.add newCall(js, access).jasonify
list.add jasonify"]"
result = nestList(amper, list) # concatenate it all
result = jasonify result # merge literals, etc.
addString newCall(js, access)
addString newLit"]"
list.add s # final value of the stmtlist is the string itself
result = newCall(bindSym"Json", list)

when false:
macro jason[T](j: T{lit|`const`}): Json =
Expand Down
2 changes: 1 addition & 1 deletion jason.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "0.2.2"
version = "0.3.0"
author = "disruptek"
description = "compile-time json"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion tests/bench.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const

var cfg = newDefaultConfig()
cfg.brief = true
cfg.budget = 0.5
cfg.budget = 1.0

echo "running benchmark..."

Expand Down
1 change: 1 addition & 0 deletions tests/nim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
--hint[Cc]=off
--hint[Link]=off
--hint[XDeclaredButNotUsed]=off
--hint[ConvFromXtoItselfNotNeeded]=off
--path="$config/../"
4 changes: 1 addition & 3 deletions tests/packed.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import std/jsonutils

import jason
import criterion

Expand Down Expand Up @@ -35,7 +33,7 @@ const

var cfg = newDefaultConfig()
cfg.brief = true
cfg.budget = 0.5
cfg.budget = 1.0

echo "running benchmark..."

Expand Down

0 comments on commit e729bab

Please sign in to comment.