Skip to content

Commit a0a16d2

Browse files
Merge pull request #351 from JAicewizard/improve_appender_speed
Improve appender speed
2 parents 1ae7ce4 + c00220c commit a0a16d2

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

appender_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -936,3 +936,46 @@ func appendNestedData[T require.TestingT](t T, a *Appender, rowsToAppend []neste
936936
}
937937
require.NoError(t, a.Flush())
938938
}
939+
940+
var types = map[reflect.Type]string{
941+
reflect.TypeFor[int8](): "TINYINT",
942+
}
943+
944+
func benchmarkAppenderSingle[T any](v T) func(*testing.B) {
945+
return func(b *testing.B) {
946+
if _, ok := types[reflect.TypeFor[T]()]; !ok {
947+
b.Fatal("Type not defined in table:", reflect.TypeFor[T]())
948+
}
949+
tableSQL := fmt.Sprintf(createSingleTableSQL, types[reflect.TypeFor[T]()])
950+
c, con, a := prepareAppender(b, tableSQL)
951+
const rowsToAppend = 2048
952+
953+
var vec [rowsToAppend]T = [rowsToAppend]T{}
954+
for i := 0; i < 2048; i++ {
955+
vec[i] = v
956+
}
957+
958+
b.ResetTimer()
959+
for n := 0; n < b.N; n++ {
960+
for i := 0; i < rowsToAppend; i++ {
961+
// require took up the majority of the time
962+
err := a.AppendRow(v)
963+
if err != nil {
964+
b.Error(err)
965+
}
966+
}
967+
}
968+
b.StopTimer()
969+
cleanupAppender(b, c, con, a)
970+
}
971+
}
972+
973+
func BenchmarkAppenderSingle(b *testing.B) {
974+
b.Run("int8", benchmarkAppenderSingle[int8](0))
975+
}
976+
977+
const createSingleTableSQL = `
978+
CREATE TABLE test (
979+
nested_int_list %s,
980+
)
981+
`

data_chunk.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package duckdb
66
import "C"
77

88
import (
9+
"sync"
910
"unsafe"
1011
)
1112

@@ -21,10 +22,7 @@ type DataChunk struct {
2122
size int
2223
}
2324

24-
// GetDataChunkCapacity returns the capacity of a data chunk.
25-
func GetDataChunkCapacity() int {
26-
return int(C.duckdb_vector_size())
27-
}
25+
var GetDataChunkCapacity = sync.OnceValue[int](func() int {return int(C.duckdb_vector_size())})
2826

2927
// GetSize returns the internal size of the data chunk.
3028
func (chunk *DataChunk) GetSize() int {

0 commit comments

Comments
 (0)