Skip to content

Commit

Permalink
add parse
Browse files Browse the repository at this point in the history
  • Loading branch information
planetis-m committed Mar 29, 2024
1 parent b08a4e5 commit 3c3df5d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ for k, v in pairs(x, JsonPtr"/d", JsonTree): echo (k, v), " "
This section details the average time (in milliseconds) it takes to perform
various operations on a JSON document containing 1,000 entries.

| Library | Test | Replace | Remove | Add | Copy | Move |
|----------|--------|---------|--------|--------|--------|--------|
| jsonpak | 0.0035 | 0.0036 | 0.0124 | 0.0035 | 0.0120 | 0.0212 |
| std/json | 0.0005 | 0.0006 | 0.0009 | 0.0006 | 0.0007 | 0.0011 |
| Library | Parse | Test | Replace | Remove | Add | Copy | Move |
|----------|---------|--------|---------|--------|--------|--------|--------|
| jsonpak | 1.5065 | 0.0035 | 0.0036 | 0.0124 | 0.0035 | 0.0120 | 0.0212 |
| std/json | 1.7205 | 0.0005 | 0.0006 | 0.0009 | 0.0006 | 0.0007 | 0.0011 |

However, the standard library's representation occupies approximately 13.4MiB,
whereas ours only takes up 2.8MiB. Therefore, this library aims to optimize
Expand Down
6 changes: 6 additions & 0 deletions bench/benchmark.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ proc main() =
stdTree = json.parseJson(JsonData)
tree = parser.parseJson(JsonData)

bench "parse", JsonTree():
t = parser.parseJson(JsonData)

bench "test", tree:
discard test(t, JsonPtr"/records/500/age", %*30)

Expand All @@ -50,6 +53,9 @@ proc main() =
move(t, JsonPtr"/records/500/city", JsonPtr"/records/0/location")

# Benchmarks for std/json module
bench "stdlib - parse", JsonNode():
t = json.parseJson(JsonData)

bench "stdlib - test", stdTree:
discard t["records"][500]["age"] == %30

Expand Down
4 changes: 2 additions & 2 deletions src/jsonpak/private/jsontree.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ proc nextChild*(tree: JsonTree; pos: var int) {.inline.} =
else:
inc pos

proc toNode*(tree: var JsonTree; kind: uint32, str: string): Node {.inline.} =
proc toNodeAtom*(tree: var JsonTree; kind: uint32, str: string): Node {.inline.} =
toNode(kind, uint32 getOrIncl(tree.atoms, str))

type
Expand Down Expand Up @@ -104,7 +104,7 @@ proc storeAtom*(tree: var JsonTree; kind: uint32) {.inline.} =
tree.nodes.add Node(kind)

proc storeAtom*(tree: var JsonTree; kind: uint32; data: string) {.inline.} =
tree.nodes.add toNode(tree, kind, data)
tree.nodes.add toNodeAtom(tree, kind, data)

proc storeEmpty*(tree: var JsonTree; kind: uint32) {.inline.} =
tree.nodes.add toNode(kind, 1)
12 changes: 6 additions & 6 deletions src/jsonpak/private/rawops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ proc rawExtract*(result: var JsonTree, tree: JsonTree, n: NodePos) =
let n = NodePos(i+n.int) # careful
case n.kind
of opcodeInt, opcodeFloat, opcodeString:
result.nodes[i] = toNode(result, n.kind, n.str)
result.nodes[i] = toNodeAtom(result, n.kind, n.str)
else:
result.nodes[i] = tree.nodes[n.int]

Expand All @@ -51,7 +51,7 @@ proc rawAdd*(result: var JsonTree, tree: JsonTree, n: NodePos) =
let m = NodePos(i)
case m.kind
of opcodeInt, opcodeFloat, opcodeString:
result.nodes[i+n.int] = toNode(result, m.kind, m.str)
result.nodes[i+n.int] = toNodeAtom(result, m.kind, m.str)
else:
result.nodes[i+n.int] = tree.nodes[i]

Expand All @@ -62,7 +62,7 @@ proc rawAddKeyValuePair*(result: var JsonTree, src, dest: NodePos, key: string)
setLen(result.nodes, oldfull+L)
for i in countdown(oldfull-1, dest.int):
result.nodes[i+L] = result.nodes[i]
result.nodes[dest.int] = toNode(result, opcodeString, key)
result.nodes[dest.int] = toNodeAtom(result, opcodeString, key)
let src =
if src >= dest: NodePos(src.int+L) else: src
for i in 0..<L-1:
Expand All @@ -75,12 +75,12 @@ proc rawAddKeyValuePair*(result: var JsonTree, tree: JsonTree, n: NodePos, key:
setLen(result.nodes, oldfull+L)
for i in countdown(oldfull-1, n.int):
result.nodes[i+L] = result.nodes[i]
result.nodes[n.int] = toNode(result, opcodeString, key)
result.nodes[n.int] = toNodeAtom(result, opcodeString, key)
for i in 0..<L-1:
let m = NodePos(i)
case m.kind
of opcodeInt, opcodeFloat, opcodeString:
result.nodes[i+n.int+1] = toNode(result, m.kind, m.str)
result.nodes[i+n.int+1] = toNodeAtom(result, m.kind, m.str)
else:
result.nodes[i+n.int+1] = tree.nodes[i]

Expand Down Expand Up @@ -132,7 +132,7 @@ proc rawReplace*(result: var JsonTree, tree: JsonTree, n: NodePos) =
let m = NodePos(i)
case m.kind
of opcodeInt, opcodeFloat, opcodeString:
result.nodes[i+n.int] = toNode(result, m.kind, m.str)
result.nodes[i+n.int] = toNodeAtom(result, m.kind, m.str)
else:
result.nodes[i+n.int] = tree.nodes[i]

Expand Down

0 comments on commit 3c3df5d

Please sign in to comment.