Skip to content

Commit a25916c

Browse files
committed
Improved finalization of databases and statements
1 parent 2b7bda6 commit a25916c

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
### 4.3.1 (2017-11-22)
2+
3+
* Improved finalization of databases and statements for better performance
4+
5+
16
### 4.3.0 (2017-10-10)
27

38
* Improved compatibility with MSVC

src/sqlite3_stubs.c

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ static inline void maybe_raise_user_exception(int rc)
148148

149149
/* Macros to access the wrapper structures stored in the custom blocks */
150150

151-
#define Sqlite3_val(x) (*((db_wrap **) (Data_custom_val(x))))
152-
#define Sqlite3_stmtw_val(x) (*((stmt_wrap **) (Data_custom_val(x))))
153-
#define Sqlite3_backup_val(x) (*((sqlite3_backup **) (Data_custom_val(x))))
151+
#define Sqlite3_val(x) (*((db_wrap **) Data_custom_val(x)))
152+
#define Sqlite3_stmtw_val(x) (*((stmt_wrap **) Data_custom_val(x)))
153+
#define Sqlite3_backup_val(x) (*((sqlite3_backup **) Data_custom_val(x)))
154154

155155

156156
/* Exceptions */
@@ -357,12 +357,22 @@ static inline void ref_count_finalize_dbw(db_wrap *dbw)
357357
}
358358
}
359359

360-
static inline void dbw_finalize_gc(value v_dbw)
360+
static inline void db_wrap_finalize_gc(value v_dbw)
361361
{
362362
db_wrap *dbw = Sqlite3_val(v_dbw);
363363
if (dbw->db) ref_count_finalize_dbw(dbw);
364364
}
365365

366+
static struct custom_operations db_wrap_ops = {
367+
"sqlite3_ocaml_db_wrap",
368+
db_wrap_finalize_gc,
369+
custom_compare_default,
370+
custom_hash_default,
371+
custom_serialize_default,
372+
custom_deserialize_default,
373+
custom_compare_ext_default
374+
};
375+
366376
#ifdef SQLITE_HAS_OPEN_V2
367377
static inline int get_open_flags(value v_mode, value v_mutex, value v_cache)
368378
{
@@ -445,10 +455,8 @@ CAMLprim value caml_sqlite3_open(
445455
"open returned neither a database nor an error");
446456
/* "open" succeded */
447457
{
448-
db_wrap *dbw;
449-
value v_res = caml_alloc_final(2, dbw_finalize_gc, 1, 100);
450-
Sqlite3_val(v_res) = NULL;
451-
dbw = caml_stat_alloc(sizeof(db_wrap));
458+
db_wrap *dbw = caml_stat_alloc(sizeof(db_wrap));
459+
value v_res = caml_alloc_custom(&db_wrap_ops, sizeof(db_wrap *), 1, 1000);
452460
dbw->db = db;
453461
dbw->rc = SQLITE_OK;
454462
dbw->ref_count = 1;
@@ -758,7 +766,7 @@ CAMLprim value caml_sqlite3_exec_not_null_no_headers(
758766

759767
/* Statements */
760768

761-
static inline void finalize_stmt_gc(value v_stmt)
769+
static inline void stmt_wrap_finalize_gc(value v_stmt)
762770
{
763771
stmt_wrap *stmtw = Sqlite3_stmtw_val(v_stmt);
764772
sqlite3_stmt *stmt = stmtw->stmt;
@@ -768,26 +776,21 @@ static inline void finalize_stmt_gc(value v_stmt)
768776
caml_stat_free(stmtw);
769777
}
770778

771-
CAMLprim value caml_sqlite3_stmt_finalize(value v_stmt)
772-
{
773-
stmt_wrap *stmtw = safe_get_stmtw("finalize", v_stmt);
774-
int rc = sqlite3_finalize(stmtw->stmt);
775-
stmtw->stmt = NULL;
776-
return Val_rc(rc);
777-
}
778-
779-
CAMLprim value caml_sqlite3_stmt_reset(value v_stmt)
780-
{
781-
sqlite3_stmt *stmt = safe_get_stmtw("reset", v_stmt)->stmt;
782-
return Val_rc(sqlite3_reset(stmt));
783-
}
779+
static struct custom_operations stmt_wrap_ops = {
780+
"sqlite3_ocaml_stmt_wrap",
781+
stmt_wrap_finalize_gc,
782+
custom_compare_default,
783+
custom_hash_default,
784+
custom_serialize_default,
785+
custom_deserialize_default,
786+
custom_compare_ext_default
787+
};
784788

785789
static inline value alloc_stmt(db_wrap *dbw)
786790
{
787-
value v_stmt = caml_alloc_final(2, finalize_stmt_gc, 1, 100);
788-
stmt_wrap *stmtw;
789-
Sqlite3_stmtw_val(v_stmt) = NULL;
790-
stmtw = caml_stat_alloc(sizeof(stmt_wrap));
791+
stmt_wrap *stmtw = caml_stat_alloc(sizeof(stmt_wrap));
792+
value v_stmt =
793+
caml_alloc_custom(&stmt_wrap_ops, sizeof(stmt_wrap *), 1, 1000);
791794
stmtw->db_wrap = dbw;
792795
dbw->ref_count++;
793796
stmtw->stmt = NULL;
@@ -811,6 +814,20 @@ static inline void prepare_it(
811814
if (!stmtw->stmt) raise_sqlite3_Error("No code compiled from %s", sql);
812815
}
813816

817+
CAMLprim value caml_sqlite3_stmt_finalize(value v_stmt)
818+
{
819+
stmt_wrap *stmtw = safe_get_stmtw("finalize", v_stmt);
820+
int rc = sqlite3_finalize(stmtw->stmt);
821+
stmtw->stmt = NULL;
822+
return Val_rc(rc);
823+
}
824+
825+
CAMLprim value caml_sqlite3_stmt_reset(value v_stmt)
826+
{
827+
sqlite3_stmt *stmt = safe_get_stmtw("reset", v_stmt)->stmt;
828+
return Val_rc(sqlite3_reset(stmt));
829+
}
830+
814831
CAMLprim value caml_sqlite3_prepare(value v_db, value v_sql)
815832
{
816833
CAMLparam2(v_db, v_sql);

0 commit comments

Comments
 (0)