mirror of https://github.com/status-im/NimYAML.git
Fixed a GC problem; improved bench & tojson
This commit is contained in:
parent
3942e80d9b
commit
b9f569a3cf
|
@ -156,20 +156,17 @@ block:
|
|||
|
||||
block:
|
||||
multibench(cYaml1k, 100):
|
||||
var events = parser.parse(json1k)
|
||||
let res = constructJson(events)
|
||||
let res = loadToJson(json1k)
|
||||
assert res[0].kind == JObject
|
||||
|
||||
block:
|
||||
multibench(cYaml10k, 100):
|
||||
var events = parser.parse(json10k)
|
||||
let res = constructJson(events)
|
||||
let res = loadToJson(json10k)
|
||||
assert res[0].kind == JObject
|
||||
|
||||
block:
|
||||
multibench(cYaml100k, 100):
|
||||
var events = parser.parse(json100k)
|
||||
let res = constructJson(events)
|
||||
let res = loadToJson(json100k)
|
||||
assert res[0].kind == JObject
|
||||
|
||||
block:
|
||||
|
|
|
@ -153,10 +153,10 @@ block:
|
|||
let res = loadDOM(yaml100k)
|
||||
assert res.root.kind == yMapping
|
||||
|
||||
#block:
|
||||
# multibench(cYaml1m, 2):
|
||||
# let res = loadDOM(yaml1m)
|
||||
# assert res.root.kind == yMapping
|
||||
block:
|
||||
multibench(cYaml1m, 2):
|
||||
let res = loadDOM(yaml1m)
|
||||
assert res.root.kind == yMapping
|
||||
|
||||
block:
|
||||
multibench(cLibYaml1k, 100):
|
||||
|
|
|
@ -1151,7 +1151,9 @@ proc newYamlLexer*(source: string, startAt: int = 0): YamlLexer
|
|||
let sSource = safeAlloc[StringSource]()
|
||||
sSource[] = StringSource(pos: startAt, lineStart: startAt, line: 1)
|
||||
sSource[].src = source
|
||||
GC_ref(sSource[].src)
|
||||
new(result, proc(x: ref YamlLexerObj) {.nimcall.} =
|
||||
GC_unref(cast[ptr StringSource](x.source)[].src)
|
||||
dealloc(x.source)
|
||||
)
|
||||
result[] = YamlLexerObj(buf: "", source: sSource, inFlow: false,
|
||||
|
|
|
@ -176,7 +176,8 @@ proc constructJson*(s: var YamlStream): seq[JsonNode]
|
|||
else:
|
||||
internalError("Unexpected node kind: " & $levels[levels.high].node.kind)
|
||||
|
||||
proc loadToJson*(s: Stream): seq[JsonNode] {.raises: [YamlParserError].} =
|
||||
proc loadToJson*(s: Stream): seq[JsonNode]
|
||||
{.raises: [YamlParserError, YamlConstructionError, IOError].} =
|
||||
## Uses `YamlParser <#YamlParser>`_ and
|
||||
## `constructJson <#constructJson>`_ to construct an in-memory JSON tree
|
||||
## from a YAML character stream.
|
||||
|
@ -185,10 +186,6 @@ proc loadToJson*(s: Stream): seq[JsonNode] {.raises: [YamlParserError].} =
|
|||
events = parser.parse(s)
|
||||
try:
|
||||
return constructJson(events)
|
||||
except YamlConstructionError:
|
||||
var e = (ref YamlConstructionError)(getCurrentException())
|
||||
discard events.getLastTokenContext(e.line, e.column, e.lineContent)
|
||||
raise e
|
||||
except YamlStreamError:
|
||||
let e = getCurrentException()
|
||||
if e.parent of IOError:
|
||||
|
@ -196,6 +193,18 @@ proc loadToJson*(s: Stream): seq[JsonNode] {.raises: [YamlParserError].} =
|
|||
elif e.parent of YamlParserError:
|
||||
raise (ref YamlParserError)(e.parent)
|
||||
else: internalError("Unexpected exception: " & e.parent.repr)
|
||||
except Exception:
|
||||
# compiler bug: https://github.com/nim-lang/Nim/issues/3772
|
||||
internalError("Reached code that should be unreachable")
|
||||
|
||||
proc loadToJson*(str: string): seq[JsonNode]
|
||||
{.raises: [YamlParserError, YamlConstructionError].} =
|
||||
## Uses `YamlParser <#YamlParser>`_ and
|
||||
## `constructJson <#constructJson>`_ to construct an in-memory JSON tree
|
||||
## from a YAML character stream.
|
||||
var
|
||||
parser = newYamlParser(initCoreTagLibrary())
|
||||
events = parser.parse(str)
|
||||
try: return constructJson(events)
|
||||
except YamlStreamError:
|
||||
let e = getCurrentException()
|
||||
if e.parent of YamlParserError:
|
||||
raise (ref YamlParserError)(e.parent)
|
||||
else: internalError("Unexpected exception: " & e.parent.repr)
|
Loading…
Reference in New Issue