From d388e1cb5c2555c74fcaca7437797c368d130e04 Mon Sep 17 00:00:00 2001 From: Maas-Maarten Zeeman Date: Sun, 23 Jan 2022 19:16:25 +0100 Subject: [PATCH 1/4] Use a smaller stack --- c_src/esqlite3_nif.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/c_src/esqlite3_nif.c b/c_src/esqlite3_nif.c index d34f5f0..433aeef 100644 --- a/c_src/esqlite3_nif.c +++ b/c_src/esqlite3_nif.c @@ -1021,8 +1021,15 @@ esqlite_start(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) } /* Start command processing thread */ - conn->opts = enif_thread_opts_create("esqldb_thread_opts"); + conn->opts = enif_thread_opts_create("esqlite_thread_opts"); + if(conn->opts == NULL) { + return make_error_tuple(env, "thread_opts_failed"); + } + + conn->opts->suggested_stack_size = 128; + if(enif_thread_create("esqlite_connection", &conn->tid, esqlite_connection_run, conn, conn->opts) != 0) { + enif_thread_opts_destroy(conn->opts); enif_release_resource(conn); return make_error_tuple(env, "thread_create_failed"); } From 1777b4424cc95be0e25f3dc45694aaf953b6baed Mon Sep 17 00:00:00 2001 From: Maas-Maarten Zeeman Date: Mon, 24 Jan 2022 10:44:45 +0100 Subject: [PATCH 2/4] Small refactor --- c_src/esqlite3_nif.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c_src/esqlite3_nif.c b/c_src/esqlite3_nif.c index 433aeef..cc92da5 100644 --- a/c_src/esqlite3_nif.c +++ b/c_src/esqlite3_nif.c @@ -202,7 +202,7 @@ command_create() { esqlite_command *cmd = (esqlite_command *) enif_alloc(sizeof(esqlite_command)); if(cmd == NULL) - return NULL; + return NULL; cmd->env = enif_alloc_env(); if(cmd->env == NULL) { @@ -304,11 +304,11 @@ update_callback(void *arg, int sqlite_operation_type, char const *sqlite_databas esqlite_connection *db = (esqlite_connection *)arg; esqlite_command *cmd = NULL; ERL_NIF_TERM type, table, rowid; - cmd = command_create(); if(db == NULL) return; + cmd = command_create(); if(!cmd) return; From 37b48af7e6eb38b91cdafc913d6641bffd30b519 Mon Sep 17 00:00:00 2001 From: Maas-Maarten Zeeman Date: Tue, 25 Jan 2022 18:13:06 +0100 Subject: [PATCH 3/4] Use a stack size of 3Mb, which is about right for complex queries --- c_src/esqlite3_nif.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/c_src/esqlite3_nif.c b/c_src/esqlite3_nif.c index cc92da5..d849ec0 100644 --- a/c_src/esqlite3_nif.c +++ b/c_src/esqlite3_nif.c @@ -1026,7 +1026,12 @@ esqlite_start(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) return make_error_tuple(env, "thread_opts_failed"); } - conn->opts->suggested_stack_size = 128; + /* Configure a fixed sized stack, windows uses a default of 1Mb, which + * can be too small for complex queries. Linux and MacOS uses 8Mb, which + * is a bit too large, since the largest query is about 1Mb in size. The + * stack size depends on that. A value of 3Mb is about right. + */ + conn->opts->suggested_stack_size = 3072; if(enif_thread_create("esqlite_connection", &conn->tid, esqlite_connection_run, conn, conn->opts) != 0) { enif_thread_opts_destroy(conn->opts); From 3147cd75ca5284c3341c4a31c71742cefe0593b4 Mon Sep 17 00:00:00 2001 From: Maas-Maarten Zeeman Date: Tue, 25 Jan 2022 21:40:34 +0100 Subject: [PATCH 4/4] Updated the comment regarding the stack size --- c_src/esqlite3_nif.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/c_src/esqlite3_nif.c b/c_src/esqlite3_nif.c index d849ec0..b120f36 100644 --- a/c_src/esqlite3_nif.c +++ b/c_src/esqlite3_nif.c @@ -1027,9 +1027,9 @@ esqlite_start(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) } /* Configure a fixed sized stack, windows uses a default of 1Mb, which - * can be too small for complex queries. Linux and MacOS uses 8Mb, which - * is a bit too large, since the largest query is about 1Mb in size. The - * stack size depends on that. A value of 3Mb is about right. + * can be too small for complex queries. Linux and MacOS uses a stack of about + * 8Mb, which is a bit too large, since the largest sqlite query is about 1Mb + * in size. The stack size depends on that. A value of 3Mb is about right. */ conn->opts->suggested_stack_size = 3072;