Skip to content

Commit

Permalink
Merge pull request #351 from JAicewizard/improve_appender_speed
Browse files Browse the repository at this point in the history
Improve appender speed
  • Loading branch information
taniabogatsch authored Jan 24, 2025
2 parents 1ae7ce4 + c00220c commit a0a16d2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
43 changes: 43 additions & 0 deletions appender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,3 +936,46 @@ func appendNestedData[T require.TestingT](t T, a *Appender, rowsToAppend []neste
}
require.NoError(t, a.Flush())
}

var types = map[reflect.Type]string{
reflect.TypeFor[int8](): "TINYINT",
}

func benchmarkAppenderSingle[T any](v T) func(*testing.B) {
return func(b *testing.B) {
if _, ok := types[reflect.TypeFor[T]()]; !ok {
b.Fatal("Type not defined in table:", reflect.TypeFor[T]())
}
tableSQL := fmt.Sprintf(createSingleTableSQL, types[reflect.TypeFor[T]()])
c, con, a := prepareAppender(b, tableSQL)
const rowsToAppend = 2048

var vec [rowsToAppend]T = [rowsToAppend]T{}
for i := 0; i < 2048; i++ {
vec[i] = v
}

b.ResetTimer()
for n := 0; n < b.N; n++ {
for i := 0; i < rowsToAppend; i++ {
// require took up the majority of the time
err := a.AppendRow(v)
if err != nil {
b.Error(err)
}
}
}
b.StopTimer()
cleanupAppender(b, c, con, a)
}
}

func BenchmarkAppenderSingle(b *testing.B) {
b.Run("int8", benchmarkAppenderSingle[int8](0))
}

const createSingleTableSQL = `
CREATE TABLE test (
nested_int_list %s,
)
`
6 changes: 2 additions & 4 deletions data_chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package duckdb
import "C"

import (
"sync"
"unsafe"
)

Expand All @@ -21,10 +22,7 @@ type DataChunk struct {
size int
}

// GetDataChunkCapacity returns the capacity of a data chunk.
func GetDataChunkCapacity() int {
return int(C.duckdb_vector_size())
}
var GetDataChunkCapacity = sync.OnceValue[int](func() int {return int(C.duckdb_vector_size())})

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

0 comments on commit a0a16d2

Please sign in to comment.