Skip to content

Commit

Permalink
Merge pull request #18 from koykov/prealloc
Browse files Browse the repository at this point in the history
Implement preallocation method.
  • Loading branch information
koykov authored Feb 22, 2024
2 parents 518022b + 41c10bd commit 805e0cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ type Interface interface {
Marshal(io.Writer) error

ErrorOffset() int
Prealloc(uint)
Reset()
}
10 changes: 9 additions & 1 deletion vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ func (vec *Vector) getNode(depth int) (node *Node, idx int) {
node = &vec.nodes[n-1]
}
vec.nodeL++
node.vptr, node.depth = vec.ptr(), depth
node.depth = depth
node.vptr = vec.ptr()
node.key.vptr, node.val.vptr = node.vptr, node.vptr
idx = vec.Len() - 1
node.idx = idx
Expand All @@ -275,6 +276,13 @@ func (vec *Vector) ErrorOffset() int {
return vec.errOff
}

// Prealloc prepares space for further parse.
func (vec *Vector) Prealloc(size uint) {
if ul := uint(len(vec.nodes)); ul < size {
vec.nodes = append(vec.nodes, make([]Node, size-ul+1)...)
}
}

// Reset vector data.
func (vec *Vector) Reset() {
if vec.nodeL == 0 {
Expand Down

0 comments on commit 805e0cb

Please sign in to comment.