Skip to content

Commit

Permalink
sqlite: disable shared cache by default
Browse files Browse the repository at this point in the history
The shared cache works well but is probably not the right default.
It has much stricter locking semantics than you get from SQLite
with a WAL database, reducing performance in some important cases.

This appears to be an implementation limit of the shared cache,
not something fundamental to its design. If it ever gets WAL-like
locking it should be turned on by default.
  • Loading branch information
crawshaw committed Sep 19, 2018
1 parent 54cdd36 commit c225a6c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
11 changes: 7 additions & 4 deletions blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ func TestBlob(t *testing.T) {
}

func TestConcurrentBlobSpins(t *testing.T) {
c, err := sqlite.OpenConn("file::memory:?mode=memory", 0)
flags := sqlite.SQLITE_OPEN_READWRITE | sqlite.SQLITE_OPEN_CREATE | sqlite.SQLITE_OPEN_URI | sqlite.SQLITE_OPEN_NOMUTEX | sqlite.SQLITE_OPEN_SHAREDCACHE
c, err := sqlite.OpenConn("file::memory:?mode=memory", flags)
if err != nil {
t.Fatal(err)
}
c2, err := sqlite.OpenConn("file::memory:?mode=memory", 0)
c2, err := sqlite.OpenConn("file::memory:?mode=memory", flags)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -169,8 +170,10 @@ func TestConcurrentBlobSpins(t *testing.T) {
// TestConcurrentBlobWrites looks for unexpected SQLITE_LOCKED errors
// when using the (default) shared cache.
func TestConcurrentBlobWrites(t *testing.T) {
flags := sqlite.SQLITE_OPEN_READWRITE | sqlite.SQLITE_OPEN_CREATE | sqlite.SQLITE_OPEN_URI | sqlite.SQLITE_OPEN_NOMUTEX | sqlite.SQLITE_OPEN_SHAREDCACHE

const numBlobs = 5
c, err := sqlite.OpenConn("file::memory:?mode=memory", 0)
c, err := sqlite.OpenConn("file::memory:?mode=memory", flags)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -207,7 +210,7 @@ func TestConcurrentBlobWrites(t *testing.T) {
b := make([]byte, 1024)
b[0] = byte(i)

c, err := sqlite.OpenConn("file::memory:?mode=memory", 0)
c, err := sqlite.OpenConn("file::memory:?mode=memory", flags)
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 0 additions & 7 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,10 @@ type Pool struct {
//
// SQLITE_OPEN_READWRITE
// SQLITE_OPEN_CREATE
// SQLITE_OPEN_SHAREDCACHE
// SQLITE_OPEN_WAL
// SQLITE_OPEN_URI
// SQLITE_OPEN_NOMUTEX
//
// The pool is always created with the shared cache enabled.
func Open(uri string, flags OpenFlags, poolSize int) (*Pool, error) {
if flags == 0 {
flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_WAL | SQLITE_OPEN_URI | SQLITE_OPEN_NOMUTEX
}
flags |= SQLITE_OPEN_SHAREDCACHE
if uri == ":memory:" {
return nil, strerror{msg: `sqlite: ":memory:" does not work with multiple connections, use "file::memory:?mode=memory"`}
}
Expand Down
3 changes: 2 additions & 1 deletion pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const poolSize = 20
// any error is t.Fatal.
func newMemPool(t *testing.T) *sqlite.Pool {
t.Helper()
dbpool, err := sqlite.Open("file::memory:?mode=memory", 0, poolSize)
flags := sqlite.SQLITE_OPEN_READWRITE | sqlite.SQLITE_OPEN_CREATE | sqlite.SQLITE_OPEN_URI | sqlite.SQLITE_OPEN_NOMUTEX | sqlite.SQLITE_OPEN_SHAREDCACHE
dbpool, err := sqlite.Open("file::memory:?mode=memory", flags, poolSize)
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ const (
//
// SQLITE_OPEN_READWRITE
// SQLITE_OPEN_CREATE
// SQLITE_OPEN_SHAREDCACHE
// SQLITE_OPEN_WAL
// SQLITE_OPEN_URI
// SQLITE_OPEN_NOMUTEX
Expand All @@ -116,7 +115,7 @@ func OpenConn(path string, flags OpenFlags) (*Conn, error) {
func openConn(path string, flags OpenFlags) (*Conn, error) {
sqliteInit.Do(sqliteInitFn)
if flags == 0 {
flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_SHAREDCACHE | SQLITE_OPEN_WAL | SQLITE_OPEN_URI | SQLITE_OPEN_NOMUTEX
flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_WAL | SQLITE_OPEN_URI | SQLITE_OPEN_NOMUTEX
}
conn := &Conn{
stmts: make(map[string]*Stmt),
Expand Down

0 comments on commit c225a6c

Please sign in to comment.