Less C/Go border crossing to get database changes

Speeds up the query benchmark by about 5% (Go 1.2.1, Linux x64).
This commit is contained in:
Lars Buitinck 2015-03-23 22:17:00 +01:00
parent 308067797b
commit 92b704c1a6
1 changed files with 22 additions and 18 deletions

View File

@ -47,14 +47,23 @@ _sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
static long static int
_sqlite3_last_insert_rowid(sqlite3* db) { _sqlite3_exec(sqlite3* db, const char* pcmd, long* rowid, long* changes)
return (long) sqlite3_last_insert_rowid(db); {
int rv = sqlite3_exec(db, pcmd, 0, 0, 0);
*rowid = (long) sqlite3_last_insert_rowid(db);
*changes = (long) sqlite3_changes(db);
return rv;
} }
static long static int
_sqlite3_changes(sqlite3* db) { _sqlite3_step(sqlite3_stmt* stmt, long* rowid, long* changes)
return (long) sqlite3_changes(db); {
int rv = sqlite3_step(stmt);
sqlite3* db = sqlite3_db_handle(stmt);
*rowid = (long) sqlite3_last_insert_rowid(db);
*changes = (long) sqlite3_changes(db);
return rv;
} }
*/ */
@ -230,14 +239,13 @@ func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, erro
func (c *SQLiteConn) exec(cmd string) (driver.Result, error) { func (c *SQLiteConn) exec(cmd string) (driver.Result, error) {
pcmd := C.CString(cmd) pcmd := C.CString(cmd)
defer C.free(unsafe.Pointer(pcmd)) defer C.free(unsafe.Pointer(pcmd))
rv := C.sqlite3_exec(c.db, pcmd, nil, nil, nil)
var rowid, changes C.long
rv := C._sqlite3_exec(c.db, pcmd, &rowid, &changes)
if rv != C.SQLITE_OK { if rv != C.SQLITE_OK {
return nil, c.lastError() return nil, c.lastError()
} }
return &SQLiteResult{ return &SQLiteResult{int64(rowid), int64(changes)}, nil
int64(C._sqlite3_last_insert_rowid(c.db)),
int64(C._sqlite3_changes(c.db)),
}, nil
} }
// Begin transaction. // Begin transaction.
@ -465,18 +473,14 @@ func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
C.sqlite3_reset(s.s) C.sqlite3_reset(s.s)
return nil, err return nil, err
} }
rv := C.sqlite3_step(s.s) var rowid, changes C.long
rv := C._sqlite3_step(s.s, &rowid, &changes)
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE { if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
err := s.c.lastError() err := s.c.lastError()
C.sqlite3_reset(s.s) C.sqlite3_reset(s.s)
return nil, err return nil, err
} }
return &SQLiteResult{int64(rowid), int64(changes)}, nil
res := &SQLiteResult{
int64(C._sqlite3_last_insert_rowid(s.c.db)),
int64(C._sqlite3_changes(s.c.db)),
}
return res, nil
} }
// Close the rows. // Close the rows.