Initialize JObject fields with capacity 4 by default to save memory and improve performance

This commit is contained in:
def 2016-02-02 12:47:40 +01:00
commit 9aecdb587b

View file

@ -617,7 +617,7 @@ proc newJObject*(): JsonNode =
## Creates a new `JObject JsonNode` ## Creates a new `JObject JsonNode`
new(result) new(result)
result.kind = JObject result.kind = JObject
result.fields = initTable[string, JsonNode]() result.fields = initTable[string, JsonNode](4)
proc newJArray*(): JsonNode = proc newJArray*(): JsonNode =
## Creates a new `JArray JsonNode` ## Creates a new `JArray JsonNode`
@ -657,7 +657,7 @@ proc getBVal*(n: JsonNode, default: bool = false): bool =
else: return n.bval else: return n.bval
proc getFields*(n: JsonNode, proc getFields*(n: JsonNode,
default = initTable[string, JsonNode]()): default = initTable[string, JsonNode](4)):
Table[string, JsonNode] = Table[string, JsonNode] =
## Retrieves the key, value pairs of a `JObject JsonNode`. ## Retrieves the key, value pairs of a `JObject JsonNode`.
## ##
@ -700,7 +700,7 @@ proc `%`*(keyVals: openArray[tuple[key: string, val: JsonNode]]): JsonNode =
## Generic constructor for JSON data. Creates a new `JObject JsonNode` ## Generic constructor for JSON data. Creates a new `JObject JsonNode`
new(result) new(result)
result.kind = JObject result.kind = JObject
result.fields = initTable[string, JsonNode]() result.fields = initTable[string, JsonNode](4)
for key, val in items(keyVals): result.fields[key] = val for key, val in items(keyVals): result.fields[key] = val
proc `%`*(elements: openArray[JsonNode]): JsonNode = proc `%`*(elements: openArray[JsonNode]): JsonNode =