Skip to content

Commit

Permalink
Merge pull request #15 from ozontech/revert-14-reduce-gc-objects
Browse files Browse the repository at this point in the history
Revert "Reduce GC objects count"
  • Loading branch information
vadimalekseev authored Jan 31, 2024
2 parents 69ada09 + 1d3ae3f commit b45de07
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions insane.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ type decoder struct {
id int
buf []byte
root Root
nodePool []Node
nodePool []*Node
nodeCount int
}

Expand Down Expand Up @@ -214,9 +214,9 @@ func (d *decoder) decode(json string, shouldReset bool) (*Node, error) {
nodePoolLen := len(nodePool)
nodes := d.nodeCount

root := &nodePool[nodes]
root := nodePool[nodes]
root.parent = nil
curNode := &nodePool[nodes]
curNode := nodePool[nodes]
topNode := root.parent

c := byte('i') // i means insane
Expand All @@ -243,14 +243,14 @@ decodeObject:
}

if c == '}' {
curNode.next = &nodePool[nodes]
curNode.next = nodePool[nodes]
curNode = curNode.next
nodes++

curNode.bits = hellBitEnd
curNode.parent = topNode

topNode.next = &nodePool[nodes]
topNode.next = nodePool[nodes]
topNode = topNode.parent

goto pop
Expand Down Expand Up @@ -314,7 +314,7 @@ decodeObject:
return nil, insaneErr(ErrExpectedObjectFieldSeparator, json, o)
}

curNode.next = &nodePool[nodes]
curNode.next = nodePool[nodes]
curNode = curNode.next
nodes++

Expand Down Expand Up @@ -363,14 +363,14 @@ decodeArray:
}

if c == ']' {
curNode.next = &nodePool[nodes]
curNode.next = nodePool[nodes]
curNode = curNode.next
nodes++

curNode.bits = hellBitArrayEnd
curNode.parent = topNode

topNode.next = &nodePool[nodes]
topNode.next = nodePool[nodes]
topNode = topNode.parent

goto pop
Expand All @@ -390,7 +390,7 @@ decodeArray:
}
}

topNode.nodes = append(topNode.nodes, &nodePool[nodes])
topNode.nodes = append(topNode.nodes, nodePool[nodes])
decode:
// skip wc
c = json[o]
Expand All @@ -411,7 +411,7 @@ decode:
return nil, insaneErr(ErrExpectedObjectField, json, o)
}

curNode.next = &nodePool[nodes]
curNode.next = nodePool[nodes]
curNode = curNode.next
nodes++

Expand All @@ -429,7 +429,7 @@ decode:
if o == l {
return nil, insaneErr(ErrExpectedValue, json, o)
}
curNode.next = &nodePool[nodes]
curNode.next = nodePool[nodes]
curNode = curNode.next
nodes++

Expand Down Expand Up @@ -465,7 +465,7 @@ decode:
}
}

curNode.next = &nodePool[nodes]
curNode.next = nodePool[nodes]
curNode = curNode.next
nodes++

Expand All @@ -480,7 +480,7 @@ decode:
}
o += 3

curNode.next = &nodePool[nodes]
curNode.next = nodePool[nodes]
curNode = curNode.next
nodes++

Expand All @@ -493,7 +493,7 @@ decode:
}
o += 4

curNode.next = &nodePool[nodes]
curNode.next = nodePool[nodes]
curNode = curNode.next
nodes++

Expand All @@ -506,7 +506,7 @@ decode:
}
o += 3

curNode.next = &nodePool[nodes]
curNode.next = nodePool[nodes]
curNode = curNode.next
nodes++

Expand All @@ -521,7 +521,7 @@ decode:
return nil, insaneErr(ErrExpectedValue, json, o)
}

curNode.next = &nodePool[nodes]
curNode.next = nodePool[nodes]
curNode = curNode.next
nodes++

Expand Down Expand Up @@ -809,13 +809,13 @@ getArray:
}

func (d *decoder) getNode() *Node {
last := d.nodeCount
node := d.nodePool[d.nodeCount]
d.nodeCount++
if d.nodeCount > len(d.nodePool)-16 {
d.expandPool()
}

return &d.nodePool[last]
return node
}

func (n *Node) DigStrict(path ...string) (*StrictNode, error) {
Expand Down Expand Up @@ -1803,12 +1803,18 @@ func (n *Node) getIndex() int {
// ******************** //

func (d *decoder) initPool() {
d.nodePool = make([]Node, StartNodePoolSize)
d.nodePool = make([]*Node, StartNodePoolSize, StartNodePoolSize)
for i := 0; i < StartNodePoolSize; i++ {
d.nodePool[i] = &Node{}
}
}

func (d *decoder) expandPool() []Node {
func (d *decoder) expandPool() []*Node {
c := cap(d.nodePool)
d.nodePool = append(d.nodePool, make([]Node, c)...)
for i := 0; i < c; i++ {
d.nodePool = append(d.nodePool, &Node{})
}

return d.nodePool
}

Expand Down

0 comments on commit b45de07

Please sign in to comment.