optimized VDOM implementation for uibench

This commit is contained in:
Andreas Rumpf 2017-05-01 09:58:10 +02:00
commit d3e1d28c89

View file

@ -106,8 +106,9 @@ type
# even index: key, odd index: value; done this way for memory efficiency:
attrs: seq[cstring]
events*: seq[(EventKind, EventHandler)]
hash*: Hash
validHash*: bool
when false:
hash*: Hash
validHash*: bool
dom*: Node ## the attached real DOM node. Can be 'nil' if the virtual node
## is not part of the virtual DOM anymore.
@ -146,7 +147,11 @@ proc eq*(a, b: VNode): bool =
proc setAttr*(n: VNode; key: cstring; val: cstring = "") =
if n.attrs.isNil:
n.attrs = @[key, val]
when true:
let x = @[key, val]
shallowCopy n.attrs, x
else:
n.attrs = @[key, val]
else:
for i in countup(0, n.attrs.len-2, 2):
if n.attrs[i] == key:
@ -216,32 +221,33 @@ proc toString*(n: VNode; result: var string; indent: int) =
for i in 1..indent: result.add ' '
result.add "\L</" & $n.kind & ">"
proc calcHash*(n: VNode) =
if n.validHash: return
n.validHash = true
var h: Hash = ord n.kind
if n.id != nil:
h &= "id"
h &= n.id
if n.class != nil:
h &= "class"
h &= n.class
if n.key >= 0:
h &= "k"
h &= n.key
for k, v in attrs(n):
h &= " "
h &= k
h &= "="
h &= v
if n.kind == VNodeKind.text or n.text != nil:
h &= "t"
h &= n.text
else:
for child in items(n):
calcHash(child)
h &= child.hash
n.hash = h
when false:
proc calcHash*(n: VNode) =
if n.validHash: return
n.validHash = true
var h: Hash = ord n.kind
if n.id != nil:
h &= "id"
h &= n.id
if n.class != nil:
h &= "class"
h &= n.class
if n.key >= 0:
h &= "k"
h &= n.key
for k, v in attrs(n):
h &= " "
h &= k
h &= "="
h &= v
if n.kind == VNodeKind.text or n.text != nil:
h &= "t"
h &= n.text
else:
for child in items(n):
calcHash(child)
h &= child.hash
n.hash = h
proc `$`*(n: VNode): cstring =
var res = ""