mirror of
https://github.com/hanzoai/csqlite.git
synced 2026-10-30 18:42:02 +00:00
Fork of github.com/mattn/go-sqlite3 v1.14.47, rebranded to package csqlite and vendored in-tree (SQLite amalgamation sqlite3-binding.c/.h + cgo driver + callback trampolines + optional-feature files) so the Hanzo stack depends on no external go-sqlite3 module. Registers the "sqlite3" database/sql driver name from this repo. SQLCipher at-rest encryption unchanged: link system libsqlcipher via -tags libsqlite3. Preserves upstream MIT license (LICENSE) with Hanzo-first NOTICE attribution. Verified: CGO_ENABLED=1 build+test (amalgamation) green; -tags libsqlite3 + libsqlcipher build+test green; CGO_ENABLED=0 build green; zero mattn in deps.
86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
// Copyright (C) 2019 Yasuhiro Matsumoto <[email protected]>.
|
|
//
|
|
// Use of this source code is governed by an MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build cgo
|
|
// +build cgo
|
|
|
|
package csqlite
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"unsafe"
|
|
)
|
|
|
|
func BenchmarkHandleLookupParallel(b *testing.B) {
|
|
d := SQLiteDriver{}
|
|
conn, err := d.Open(":memory:")
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
c := conn.(*SQLiteConn)
|
|
|
|
handle := newHandle(c, func() {})
|
|
|
|
benchmarkHandleLookupParallel(b, func() any {
|
|
return lookupHandle(handle)
|
|
})
|
|
}
|
|
|
|
func BenchmarkHandleLookupBeforeAfter(b *testing.B) {
|
|
value := handleVal{val: func() {}}
|
|
handle := unsafe.Pointer(&value)
|
|
|
|
before := mutexHandleTable{vals: map[unsafe.Pointer]handleVal{handle: value}}
|
|
after := atomicHandleTable{}
|
|
after.vals.Store(map[unsafe.Pointer]handleVal{handle: value})
|
|
|
|
b.Run("before_mutex", func(b *testing.B) {
|
|
benchmarkHandleLookupParallel(b, func() any {
|
|
return before.lookup(handle).val
|
|
})
|
|
})
|
|
b.Run("after_atomic", func(b *testing.B) {
|
|
benchmarkHandleLookupParallel(b, func() any {
|
|
return after.lookup(handle).val
|
|
})
|
|
})
|
|
}
|
|
|
|
func benchmarkHandleLookupParallel(b *testing.B, lookup func() any) {
|
|
b.Helper()
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
for pb.Next() {
|
|
if lookup() == nil {
|
|
b.Fatal("lookup returned nil")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
type mutexHandleTable struct {
|
|
mu sync.Mutex
|
|
vals map[unsafe.Pointer]handleVal
|
|
}
|
|
|
|
func (t *mutexHandleTable) lookup(handle unsafe.Pointer) handleVal {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
return t.vals[handle]
|
|
}
|
|
|
|
type atomicHandleTable struct {
|
|
vals atomic.Value
|
|
}
|
|
|
|
func (t *atomicHandleTable) lookup(handle unsafe.Pointer) handleVal {
|
|
m, _ := t.vals.Load().(map[unsafe.Pointer]handleVal)
|
|
return m[handle]
|
|
}
|