Skip to content

Commit

Permalink
add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
planetis-m committed Apr 18, 2024
1 parent 5630626 commit 25a7e11
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
12 changes: 11 additions & 1 deletion bench/benchmark.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/[times, strutils, strformat, stats]
import std/json except `%*`
import jsonpak, jsonpak/[extra, patch, parser, jsonptr, mapper, builder]
import jsonpak, jsonpak/[extra, patch, parser, jsonptr, mapper, builder, sorted]

const
JsonData = readFile("test.json")
Expand Down Expand Up @@ -73,6 +73,13 @@ proc main() =
bench "move", tree:
move(t, JsonPtr"/records/500/city", JsonPtr"/records/0/location")

bench "sort", newEmptyTree().SortedJsonTree:
t = sorted(tree)

tree = sorted(tree).JsonTree
bench "hash", tree:
discard hash(t.SortedJsonTree)

# Benchmarks for std/json module
bench "stdlib - extract", JsonNode():
t = stdTree.copy()
Expand All @@ -83,6 +90,9 @@ proc main() =
bench "stdlib - toString", stdTree:
discard $t

bench "stdlib - hash", stdTree:
discard hash(t)

bench "stdlib - fromJson", stdTree:
discard t["records"][500].to(UserRecord)

Expand Down
1 change: 1 addition & 0 deletions src/jsonpak/private/rawops_sorted.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bitabs, jsonnode, jsontree, rawops, std/[importutils, algorithm, sequtils, hashes]
export rawExtract

proc rawSorted*(tree: JsonTree, n: NodePos): JsonTree =
privateAccess(JsonTree)
Expand Down
7 changes: 6 additions & 1 deletion src/jsonpak/sorted.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ proc `==`*(a, b: SortedJsonTree): bool {.inline.} =
privateAccess(JsonTree)
if JsonTree(a).nodes.len != JsonTree(b).nodes.len:
return false
rawTest(JsonTree(a), JsonTree(b), rootNodeId)
result = rawTest(JsonTree(a), JsonTree(b), rootNodeId)

proc copy*(tree: SortedJsonTree): SortedJsonTree =
## Returns a fresh copy of `tree`.
result = JsonTree().SortedJsonTree
rawExtract(JsonTree(result), JsonTree(tree), rootNodeId)

proc deduplicate*(tree: var SortedJsonTree) =
## Deduplicates keys in `tree` recursively. If duplicate keys are found,
Expand Down

0 comments on commit 25a7e11

Please sign in to comment.