summaryrefslogtreecommitdiff
path: root/lib/btrfs/btrfstree
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-25 16:41:52 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-12 16:16:53 -0700
commit61656c6d1ba2bb463c1a51b69c151eb2e495c20b (patch)
treeb13c4c1d7256e282c2b90ad7d3dbd554b6d5b436 /lib/btrfs/btrfstree
parent9ba37f3836965e5a28df48567c0b7666d959915d (diff)
btrfstree: Pre-allocate node.BodyLeaf/node.BodyInternal
Diffstat (limited to 'lib/btrfs/btrfstree')
-rw-r--r--lib/btrfs/btrfstree/types_node.go14
1 files changed, 7 insertions, 7 deletions
diff --git a/lib/btrfs/btrfstree/types_node.go b/lib/btrfs/btrfstree/types_node.go
index d9d7118..8f8ba0b 100644
--- a/lib/btrfs/btrfstree/types_node.go
+++ b/lib/btrfs/btrfstree/types_node.go
@@ -248,14 +248,13 @@ type KeyPointer struct {
func (node *Node) unmarshalInternal(bodyBuf []byte) (int, error) {
n := 0
- for i := uint32(0); i < node.Head.NumItems; i++ {
- var item KeyPointer
- _n, err := binstruct.Unmarshal(bodyBuf[n:], &item)
+ node.BodyInternal = make([]KeyPointer, node.Head.NumItems)
+ for i := range node.BodyInternal {
+ _n, err := binstruct.Unmarshal(bodyBuf[n:], &node.BodyInternal[i])
n += _n
if err != nil {
return n, fmt.Errorf("item %v: %w", i, err)
}
- node.BodyInternal = append(node.BodyInternal, item)
}
node.Padding = bodyBuf[n:]
return len(bodyBuf), nil
@@ -299,7 +298,8 @@ type ItemHeader struct {
func (node *Node) unmarshalLeaf(bodyBuf []byte) (int, error) {
head := 0
tail := len(bodyBuf)
- for i := uint32(0); i < node.Head.NumItems; i++ {
+ node.BodyLeaf = make([]Item, node.Head.NumItems)
+ for i := range node.BodyLeaf {
var itemHead ItemHeader
n, err := binstruct.Unmarshal(bodyBuf[head:], &itemHead)
head += n
@@ -324,11 +324,11 @@ func (node *Node) unmarshalLeaf(bodyBuf []byte) (int, error) {
tail = dataOff
dataBuf := bodyBuf[dataOff : dataOff+dataSize]
- node.BodyLeaf = append(node.BodyLeaf, Item{
+ node.BodyLeaf[i] = Item{
Key: itemHead.Key,
BodySize: itemHead.DataSize,
Body: btrfsitem.UnmarshalItem(itemHead.Key, node.ChecksumType, dataBuf),
- })
+ }
}
node.Padding = bodyBuf[head:tail]