aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api/sqlite3-api-oo1.js
diff options
context:
space:
mode:
authorstephan <stephan@noemail.net>2024-04-04 22:53:09 +0000
committerstephan <stephan@noemail.net>2024-04-04 22:53:09 +0000
commit7fd9b53308d8bea8a8851fb71e6dbe0db4a73775 (patch)
tree2da4133f06160e5c4a36aed5bfdcb8bdd2b1fed5 /ext/wasm/api/sqlite3-api-oo1.js
parentf895173c5f38202677f323462a7f840825244fdf (diff)
downloadsqlite-7fd9b53308d8bea8a8851fb71e6dbe0db4a73775.tar.gz
sqlite-7fd9b53308d8bea8a8851fb71e6dbe0db4a73775.zip
Optimize sqlite3.oo1.DB.exec() for the rowMode='object' case to avoid converting the object property keys (column names) from native code to JS for each row. This speeds up large data sets considerably and addresses the report in [forum:3632183d2470617d|forum post 3632183d2470617d].
FossilOrigin-Name: 8b41ef8690001eb299f5b7182c28f5318333bff5b505e1d59d6e6f4556b1c759
Diffstat (limited to 'ext/wasm/api/sqlite3-api-oo1.js')
-rw-r--r--ext/wasm/api/sqlite3-api-oo1.js91
1 files changed, 56 insertions, 35 deletions
diff --git a/ext/wasm/api/sqlite3-api-oo1.js b/ext/wasm/api/sqlite3-api-oo1.js
index 160d59db5..425b52eec 100644
--- a/ext/wasm/api/sqlite3-api-oo1.js
+++ b/ext/wasm/api/sqlite3-api-oo1.js
@@ -433,40 +433,56 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
out.returnVal = ()=>opt.resultRows;
}
if(opt.callback || opt.resultRows){
- switch((undefined===opt.rowMode)
- ? 'array' : opt.rowMode) {
- case 'object': out.cbArg = (stmt)=>stmt.get(Object.create(null)); break;
- case 'array': out.cbArg = (stmt)=>stmt.get([]); break;
- case 'stmt':
- if(Array.isArray(opt.resultRows)){
- toss3("exec(): invalid rowMode for a resultRows array: must",
- "be one of 'array', 'object',",
- "a result column number, or column name reference.");
- }
- out.cbArg = (stmt)=>stmt;
+ switch((undefined===opt.rowMode) ? 'array' : opt.rowMode) {
+ case 'object':
+ out.cbArg = (stmt,cache)=>{
+ if( !cache.columnNames ) cache.columnNames = stmt.getColumnNames([]);
+ /* https://sqlite.org/forum/forumpost/3632183d2470617d:
+ conversion of rows to objects (key/val pairs) is
+ somewhat expensive for large data sets because of the
+ native-to-JS conversion of the column names. If we
+ instead cache the names and build objects from that
+ list of strings, it can run twice as fast. The
+ difference is not noticeable for small data sets but
+ becomes human-perceivable when enough rows are
+ involved. */
+ const row = stmt.get([]);
+ const rv = Object.create(null);
+ for( const i in cache.columnNames ) rv[cache.columnNames[i]] = row[i];
+ return rv;
+ };
+ break;
+ case 'array': out.cbArg = (stmt)=>stmt.get([]); break;
+ case 'stmt':
+ if(Array.isArray(opt.resultRows)){
+ toss3("exec(): invalid rowMode for a resultRows array: must",
+ "be one of 'array', 'object',",
+ "a result column number, or column name reference.");
+ }
+ out.cbArg = (stmt)=>stmt;
+ break;
+ default:
+ if(util.isInt32(opt.rowMode)){
+ out.cbArg = (stmt)=>stmt.get(opt.rowMode);
break;
- default:
- if(util.isInt32(opt.rowMode)){
- out.cbArg = (stmt)=>stmt.get(opt.rowMode);
- break;
- }else if('string'===typeof opt.rowMode
- && opt.rowMode.length>1
- && '$'===opt.rowMode[0]){
- /* "$X": fetch column named "X" (case-sensitive!). Prior
- to 2022-12-14 ":X" and "@X" were also permitted, but
- having so many options is unnecessary and likely to
- cause confusion. */
- const $colName = opt.rowMode.substr(1);
- out.cbArg = (stmt)=>{
- const rc = stmt.get(Object.create(null))[$colName];
- return (undefined===rc)
- ? toss3(capi.SQLITE_NOTFOUND,
- "exec(): unknown result column:",$colName)
- : rc;
- };
- break;
- }
- toss3("Invalid rowMode:",opt.rowMode);
+ }else if('string'===typeof opt.rowMode
+ && opt.rowMode.length>1
+ && '$'===opt.rowMode[0]){
+ /* "$X": fetch column named "X" (case-sensitive!). Prior
+ to 2022-12-14 ":X" and "@X" were also permitted, but
+ having so many options is unnecessary and likely to
+ cause confusion. */
+ const $colName = opt.rowMode.substr(1);
+ out.cbArg = (stmt)=>{
+ const rc = stmt.get(Object.create(null))[$colName];
+ return (undefined===rc)
+ ? toss3(capi.SQLITE_NOTFOUND,
+ "exec(): unknown result column:",$colName)
+ : rc;
+ };
+ break;
+ }
+ toss3("Invalid rowMode:",opt.rowMode);
}
}
return out;
@@ -884,10 +900,15 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
and names. */) ? 0 : 1;
evalFirstResult = false;
if(arg.cbArg || resultRows){
+ const cbArgCache = Object.create(null)
+ /* 2nd arg for arg.cbArg, used by (at least) row-to-object
+ converter */;
for(; stmt.step(); stmt._lockedByExec = false){
- if(0===gotColNames++) stmt.getColumnNames(opt.columnNames);
+ if(0===gotColNames++){
+ stmt.getColumnNames(cbArgCache.columnNames = (opt.columnNames || []));
+ }
stmt._lockedByExec = true;
- const row = arg.cbArg(stmt);
+ const row = arg.cbArg(stmt,cbArgCache);
if(resultRows) resultRows.push(row);
if(callback && false === callback.call(opt, row, stmt)){
break;