Skip to content

Commit bd93f74

Browse files
committed
Replace array with slice in writer implementation
1 parent d905aa7 commit bd93f74

File tree

1 file changed

+10
-34
lines changed

1 file changed

+10
-34
lines changed

kernel/writer_helper.go

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package kernel
22

3-
// Writer implementation that minimizes memory copies where possible
4-
// by using direct memory operations, though buffer growth requires copying
3+
// Writer implementation that minimizes memory copies
54

65
/*
76
#include "kernel/bitcoinkernel.h"
@@ -20,9 +19,8 @@ import (
2019
// writerCallbackData holds the growing buffer that collects written bytes
2120
// Uses a capacity-based growth strategy to reduce reallocations
2221
type writerCallbackData struct {
23-
buffer []byte // Pre-allocated buffer
24-
position int // Current write position
25-
err error
22+
buffer []byte // Pre-allocated buffer
23+
err error
2624
}
2725

2826
//export go_writer_callback_bridge
@@ -32,40 +30,18 @@ func go_writer_callback_bridge(bytes unsafe.Pointer, size C.size_t, userdata uns
3230
// Retrieve original Go callback data struct
3331
data := handle.Value().(*writerCallbackData)
3432

35-
bytesSize := int(size)
36-
requiredCapacity := data.position + bytesSize
37-
38-
// Grow buffer if needed (double capacity strategy)
39-
if requiredCapacity > len(data.buffer) {
40-
newCapacity := len(data.buffer) * 2
41-
if newCapacity < requiredCapacity {
42-
newCapacity = requiredCapacity
43-
}
44-
newBuffer := make([]byte, newCapacity)
45-
copy(newBuffer[:data.position], data.buffer[:data.position])
46-
data.buffer = newBuffer
47-
}
48-
49-
if bytesSize > 0 {
50-
// Get pointer to destination in Go buffer
51-
dstPtr := unsafe.Pointer(&data.buffer[data.position])
52-
// Use C's memmove to copy directly
53-
C.memmove(dstPtr, bytes, size)
54-
data.position += bytesSize
33+
if size > 0 {
34+
// Create a Go slice view of the C memory
35+
cBytes := unsafe.Slice((*byte)(bytes), int(size))
36+
data.buffer = append(data.buffer, cBytes...)
5537
}
56-
57-
return 0 // success
38+
return 0
5839
}
5940

6041
// writeToBytes is a helper function that uses a callback pattern to collect bytes
6142
// It takes a function that calls the C API with the writer callback
6243
func writeToBytes(writerFunc func(C.btck_WriteBytes, unsafe.Pointer) C.int) ([]byte, error) {
63-
// Pre-allocate buffer with reasonable initial capacity
64-
initialCapacity := 1024 // Start with 1KB, will grow as needed
65-
callbackData := &writerCallbackData{
66-
buffer: make([]byte, initialCapacity),
67-
position: 0,
68-
}
44+
callbackData := &writerCallbackData{}
6945

7046
// Create cgo handle for the callback data
7147
handle := cgo.NewHandle(callbackData)
@@ -82,5 +58,5 @@ func writeToBytes(writerFunc func(C.btck_WriteBytes, unsafe.Pointer) C.int) ([]b
8258
}
8359

8460
// Return exactly the bytes that were written (slice the buffer to actual size)
85-
return callbackData.buffer[:callbackData.position], nil
61+
return callbackData.buffer, nil
8662
}

0 commit comments

Comments
 (0)