Skip to content

Commit

Permalink
Refactor, speed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Nov 2, 2024
1 parent 17f7840 commit 90d6ec3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
4 changes: 4 additions & 0 deletions vfs/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,8 @@ 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
)
2 changes: 0 additions & 2 deletions vfs/shm_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"github.com/ncruces/go-sqlite3/internal/util"
)

const _SHM_NLOCK = 8

type vfsShmFile struct {
*os.File
info os.FileInfo
Expand Down
19 changes: 11 additions & 8 deletions vfs/shm_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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[:])
}
6 changes: 0 additions & 6 deletions vfs/shm_ofd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 90d6ec3

Please sign in to comment.