From 3c3df5de767eac6f598ab90df19dec033946f11e Mon Sep 17 00:00:00 2001 From: Antonis Geralis Date: Fri, 29 Mar 2024 22:30:28 +0200 Subject: [PATCH] add parse --- README.md | 8 ++++---- bench/benchmark.nim | 6 ++++++ src/jsonpak/private/jsontree.nim | 4 ++-- src/jsonpak/private/rawops.nim | 12 ++++++------ 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index efc6370..5dfaf6a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bench/benchmark.nim b/bench/benchmark.nim index b86ee05..5ecfc4f 100644 --- a/bench/benchmark.nim +++ b/bench/benchmark.nim @@ -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) @@ -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 diff --git a/src/jsonpak/private/jsontree.nim b/src/jsonpak/private/jsontree.nim index 000fa44..612af33 100644 --- a/src/jsonpak/private/jsontree.nim +++ b/src/jsonpak/private/jsontree.nim @@ -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 @@ -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) diff --git a/src/jsonpak/private/rawops.nim b/src/jsonpak/private/rawops.nim index 632ccd1..a0eea5f 100644 --- a/src/jsonpak/private/rawops.nim +++ b/src/jsonpak/private/rawops.nim @@ -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] @@ -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] @@ -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..