Commit f1fbcd91 by Orlando Vazquez

Some minor speedups

- allocate space for a step_request once per statement
- add some more speedtests
- process.nextTick the next step call to a statement to prevent a weird v8
  crash
parent 8771e301
...@@ -42,9 +42,6 @@ var count = 100000; ...@@ -42,9 +42,6 @@ var count = 100000;
var t0; var t0;
db.open(":memory:", function () { db.open(":memory:", function () {
puts(inspect(arguments));
puts("open cb");
db.query("CREATE TABLE t1 (alpha INTEGER)", function () { db.query("CREATE TABLE t1 (alpha INTEGER)", function () {
puts("create table callback" + inspect(arguments)); puts("create table callback" + inspect(arguments));
// writeTest(db, readTest); // writeTest(db, readTest);
......
var fs = require("fs"),
sys = require("sys"),
sqlite = require("./sqlite");
var sys = require("sys"),
assert = require("assert");
var puts = sys.puts;
var inspect = sys.inspect;
var sqlite = require("./sqlite");
var db = new sqlite.Database();
function readTest(db, callback) {
var t0 = new Date;
var rows = 0;
db.query("SELECT * FROM t1", function(row) {
if (!row) {
var d = ((new Date)-t0)/1000;
puts("**** " + rows + " rows in " + d + "s (" + (rows/d) + "/s)");
if (callback) callback(db);
}
else {
// assert.deepEqual(row, {alpha:1, beta: 'hello', pi: 3.141});
// puts("got a row" + inspect(arguments));
rows++;
}
});
}
function writeTest(db, i, callback) {
db.query("INSERT INTO t1 VALUES (1, 'hello', 3.141)", function (row) {
if (!i--) {
// end of results
var dt = ((new Date)-t0)/1000;
puts("**** " + count + " insertions in " + dt + "s (" + (count/dt) + "/s)");
if (callback) callback(db);
}
else {
writeTest(db, i--, callback);
}
});
}
var count = 100000;
var t0;
db.open(":memory:", function () {
puts(inspect(arguments));
puts("open cb");
db.query("CREATE TABLE t1 (alpha INTEGER, beta TEXT, pi FLOAT)", function () {
puts("create table callback" + inspect(arguments));
// writeTest(db, readTest);
t0 = new Date;
writeTest(db, count, readTest);
});
});
require 'sqlite3'
db = SQLite3::Database.new(":memory:")
db.execute("CREATE TABLE t1 (alpha INTEGER, beta TEXT, pi FLOAT)") do |row|
end
count = 100000;
t0 = Time.new;
1.upto(count) do
db.execute("INSERT INTO t1 VALUES (1, 'hello', 3.141)") do
end
end
d = Time.new - t0;
puts "Took #{d}s (#{count/d} inserts/s)";
t0 = Time.new;
db.execute("SELECT * FROM t1") do |row|
end
d = Time.new - t0;
puts "Took #{d} (#{count/d} rows/s)";
...@@ -98,7 +98,7 @@ function _doStep(db, statement, rowCallback) { ...@@ -98,7 +98,7 @@ function _doStep(db, statement, rowCallback) {
return; return;
} }
rowCallback(row); rowCallback(row);
_doStep(db, statement, rowCallback); process.nextTick(function () { _doStep(db, statement, rowCallback); });
}); });
} }
......
...@@ -464,11 +464,16 @@ protected: ...@@ -464,11 +464,16 @@ protected:
} }
protected: protected:
Statement(sqlite3_stmt* stmt, int first_rc = -1) : EventEmitter(), stmt_(stmt) { Statement(sqlite3_stmt* stmt, int first_rc = -1)
: EventEmitter(), stmt_(stmt), step_req(NULL) {
first_rc_ = first_rc; first_rc_ = first_rc;
} }
~Statement() { if (stmt_) { sqlite3_finalize(stmt_); } } ~Statement() {
if (stmt_) { sqlite3_finalize(stmt_); }
if (step_req) free(step_req);
}
sqlite3_stmt* stmt_; sqlite3_stmt* stmt_;
operator sqlite3_stmt* () const { return stmt_; } operator sqlite3_stmt* () const { return stmt_; }
...@@ -719,6 +724,8 @@ protected: ...@@ -719,6 +724,8 @@ protected:
Statement *sto = finalize_req->sto; Statement *sto = finalize_req->sto;
req->result = sqlite3_finalize(*sto); req->result = sqlite3_finalize(*sto);
sto->stmt_ = NULL; sto->stmt_ = NULL;
free(sto->step_req);
sto->step_req = NULL;
return 0; return 0;
} }
...@@ -742,6 +749,7 @@ protected: ...@@ -742,6 +749,7 @@ protected:
finalize_req->cb = Persistent<Function>::New(cb); finalize_req->cb = Persistent<Function>::New(cb);
finalize_req->sto = sto; finalize_req->sto = sto;
eio_custom(EIO_Finalize, EIO_PRI_DEFAULT, EIO_AfterFinalize, finalize_req); eio_custom(EIO_Finalize, EIO_PRI_DEFAULT, EIO_AfterFinalize, finalize_req);
ev_ref(EV_DEFAULT_UC); ev_ref(EV_DEFAULT_UC);
...@@ -771,7 +779,7 @@ protected: ...@@ -771,7 +779,7 @@ protected:
// if rc == 0 this will NULL // if rc == 0 this will NULL
char *error_msg; char *error_msg;
}; } *step_req;
static int EIO_AfterStep(eio_req *req) { static int EIO_AfterStep(eio_req *req) {
ev_unref(EV_DEFAULT_UC); ev_unref(EV_DEFAULT_UC);
...@@ -797,6 +805,10 @@ protected: ...@@ -797,6 +805,10 @@ protected:
Local<Object> row = Object::New(); Local<Object> row = Object::New();
for (int i = 0; i < step_req->column_count; i++) { for (int i = 0; i < step_req->column_count; i++) {
assert(step_req->column_data[i]);
assert(step_req->column_names[i]);
assert(step_req->column_types[i]);
switch(step_req->column_types[i]) { switch(step_req->column_types[i]) {
case SQLITE_INTEGER: case SQLITE_INTEGER:
row->Set(String::New(step_req->column_names[i]), row->Set(String::New(step_req->column_names[i]),
...@@ -832,15 +844,17 @@ protected: ...@@ -832,15 +844,17 @@ protected:
} }
step_req->cb.Dispose(); step_req->cb.Dispose();
if (step_req->column_count) { if (req->result == SQLITE_ROW && step_req->column_count) {
free(step_req->column_data); free((void**)step_req->column_data);
free(step_req->column_types); step_req->column_data = NULL;
free(step_req->column_names); free((int*)step_req->column_types);
step_req->column_types = NULL;
free((char**)step_req->column_names);
step_req->column_names = NULL;
} }
step_req->sto->Unref(); step_req->sto->Unref();
free(step_req);
return 0; return 0;
} }
...@@ -858,6 +872,8 @@ protected: ...@@ -858,6 +872,8 @@ protected:
rc = req->result = sqlite3_step(stmt); rc = req->result = sqlite3_step(stmt);
} }
assert(step_req);
if (rc == SQLITE_ROW) { if (rc == SQLITE_ROW) {
// would be nice to cache the column names and type data somewhere // would be nice to cache the column names and type data somewhere
if (step_req->column_count = sqlite3_column_count(stmt)) { if (step_req->column_count = sqlite3_column_count(stmt)) {
...@@ -867,7 +883,11 @@ protected: ...@@ -867,7 +883,11 @@ protected:
(void **) calloc(step_req->column_count, sizeof(void *)); (void **) calloc(step_req->column_count, sizeof(void *));
step_req->column_names = step_req->column_names =
(char **) calloc(step_req->column_count, sizeof(char *)); (char **) calloc(step_req->column_count, sizeof(char *));
} }
assert(step_req->column_types
&& step_req->column_data
&& step_req->column_names);
for (int i = 0; i < step_req->column_count; i++) { for (int i = 0; i < step_req->column_count; i++) {
int type = step_req->column_types[i] = sqlite3_column_type(stmt, i); int type = step_req->column_types[i] = sqlite3_column_type(stmt, i);
...@@ -903,11 +923,13 @@ protected: ...@@ -903,11 +923,13 @@ protected:
// unsupported type // unsupported type
} }
} }
assert(step_req->column_data[i]);
assert(step_req->column_names[i]);
assert(step_req->column_types[i]);
} }
} }
else if (rc == SQLITE_DONE) { else if (rc == SQLITE_DONE) {
// printf("no more results"); // nothing to do in this case
// no more results
} }
else { else {
step_req->error_msg = (char *) step_req->error_msg = (char *)
...@@ -923,9 +945,13 @@ protected: ...@@ -923,9 +945,13 @@ protected:
REQ_FUN_ARG(0, cb); REQ_FUN_ARG(0, cb);
Statement* sto = ObjectWrap::Unwrap<Statement>(args.This()); Statement* sto = ObjectWrap::Unwrap<Statement>(args.This());
struct step_request *step_req = sto->step_req;
struct step_request *step_req = (struct step_request *) if (!step_req) {
sto->step_req = step_req = (struct step_request *)
calloc(1, sizeof(struct step_request)); calloc(1, sizeof(struct step_request));
}
if (!step_req) { if (!step_req) {
V8::LowMemoryNotification(); V8::LowMemoryNotification();
......
...@@ -18,7 +18,7 @@ def configure(conf): ...@@ -18,7 +18,7 @@ def configure(conf):
def build(bld): def build(bld):
obj = bld.new_task_gen("cxx", "shlib", "node_addon") obj = bld.new_task_gen("cxx", "shlib", "node_addon")
obj.cxxflags = "-g" obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE"]
obj.target = "sqlite3_bindings" obj.target = "sqlite3_bindings"
obj.source = "sqlite3_bindings.cc" obj.source = "sqlite3_bindings.cc"
#obj.lib = "sqlite3" #obj.lib = "sqlite3"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment