From 14237397985b709a8d207e676db5a5fc9a05d989 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Sat, 2 Nov 2024 11:02:38 +0000 Subject: [PATCH] Refactor, speed. --- vfs/const.go | 5 +++++ vfs/shm_bsd.go | 4 +--- vfs/shm_copy.go | 21 ++++++++++++--------- vfs/shm_ofd.go | 6 ------ 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/vfs/const.go b/vfs/const.go index e80437be..a47aa807 100644 --- a/vfs/const.go +++ b/vfs/const.go @@ -234,4 +234,9 @@ const ( _SHM_LOCK _ShmFlag = 2 _SHM_SHARED _ShmFlag = 4 _SHM_EXCLUSIVE _ShmFlag = 8 + + _SHM_NLOCK = 8 + _SHM_BASE = 120 + _SHM_DMS = _SHM_BASE + _SHM_NLOCK + _SHM_CLOSE = _SHM_UNLOCK | _SHM_SHARED | _SHM_EXCLUSIVE ) diff --git a/vfs/shm_bsd.go b/vfs/shm_bsd.go index 079c8f42..4d2984eb 100644 --- a/vfs/shm_bsd.go +++ b/vfs/shm_bsd.go @@ -14,8 +14,6 @@ import ( "github.com/ncruces/go-sqlite3/internal/util" ) -const _SHM_NLOCK = 8 - type vfsShmFile struct { *os.File info os.FileInfo @@ -51,7 +49,7 @@ func (s *vfsShm) Close() error { defer vfsShmFilesMtx.Unlock() // Unlock everything. - s.shmLock(0, _SHM_NLOCK, _SHM_UNLOCK) + s.shmLock(0, _SHM_NLOCK, _SHM_CLOSE) // Decrease reference count. if s.vfsShmFile.refs > 0 { diff --git a/vfs/shm_copy.go b/vfs/shm_copy.go index 9f7f5860..a4132d0f 100644 --- a/vfs/shm_copy.go +++ b/vfs/shm_copy.go @@ -11,10 +11,7 @@ import ( "github.com/tetratelabs/wazero/api" ) -const ( - _SHM_NLOCK = 8 - _WALINDEX_PGSZ = 32768 -) +const _WALINDEX_PGSZ = 32768 type vfsShmBuffer struct { shared []byte // +checklocks:Mutex @@ -52,7 +49,7 @@ func (s *vfsShm) Close() error { defer vfsShmBuffersMtx.Unlock() // Unlock everything. - s.shmLock(0, _SHM_NLOCK, _SHM_UNLOCK) + s.shmLock(0, _SHM_NLOCK, _SHM_CLOSE) // Decrease reference count. if s.vfsShmBuffer.refs > 0 { @@ -132,9 +129,10 @@ func (s *vfsShm) shmLock(offset, n int32, flags _ShmFlag) _ErrorCode { s.Lock() defer s.Unlock() - if flags&_SHM_UNLOCK == 0 { + switch { + case flags&_SHM_LOCK != 0: s.shmAcquire() - } else { + case flags&_SHM_EXCLUSIVE != 0: s.shmRelease() } @@ -222,6 +220,7 @@ func (s *vfsShm) shmBarrier() { // // Finally, we have the WAL-index hash tables, // which are only modified holding the exclusive WAL_WRITE_LOCK. +// Also, aHash isn't modified unless aPgno changes. // // Since all the data is either redundant+checksummed, // 4 byte aligned, or modified under an exclusive lock, @@ -238,7 +237,7 @@ func (s *vfsShm) shmAcquire() { shared := shmPage(s.shared[i0:i1]) shadow := shmPage(s.shadow[i0:i1]) privat := shmPage(util.View(s.mod, p, _WALINDEX_PGSZ)) - if *shadow == *shared { + if shmPageEq(shadow, shared) { continue } for i, shared := range shared { @@ -259,7 +258,7 @@ func (s *vfsShm) shmRelease() { shared := shmPage(s.shared[i0:i1]) shadow := shmPage(s.shadow[i0:i1]) privat := shmPage(util.View(s.mod, p, _WALINDEX_PGSZ)) - if *shadow == *privat { + if shmPageEq(shadow, privat) { continue } for i, privat := range privat { @@ -275,3 +274,7 @@ func shmPage(s []byte) *[_WALINDEX_PGSZ / 4]uint32 { p := (*uint32)(unsafe.Pointer(unsafe.SliceData(s))) return (*[_WALINDEX_PGSZ / 4]uint32)(unsafe.Slice(p, _WALINDEX_PGSZ/4)) } + +func shmPageEq(p1, p2 *[_WALINDEX_PGSZ / 4]uint32) bool { + return *(*[_WALINDEX_PGSZ / 8]uint32)(p1[:]) == *(*[_WALINDEX_PGSZ / 8]uint32)(p2[:]) +} diff --git a/vfs/shm_ofd.go b/vfs/shm_ofd.go index bf047550..019fc7ab 100644 --- a/vfs/shm_ofd.go +++ b/vfs/shm_ofd.go @@ -15,12 +15,6 @@ import ( "github.com/ncruces/go-sqlite3/internal/util" ) -const ( - _SHM_NLOCK = 8 - _SHM_BASE = 120 - _SHM_DMS = _SHM_BASE + _SHM_NLOCK -) - type vfsShm struct { *os.File path string