From 7dfb697b8e62508236f7465503dd2b0e16188a1f Mon Sep 17 00:00:00 2001 From: Ian O'Dwyer <49337701+iodwyer@users.noreply.github.com> Date: Fri, 18 Sep 2026 10:23:34 +1000 Subject: [PATCH] Refactor savetables function to exit early for 0 rows --- code/wdb/writedown.q | 46 ++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/code/wdb/writedown.q b/code/wdb/writedown.q index d7ab15589..8dbe85b9c 100644 --- a/code/wdb/writedown.q +++ b/code/wdb/writedown.q @@ -26,26 +26,34 @@ tabsizes:([tablename:`symbol$()] rowcount:`long$(); bytes:`long$()); /- ke savetables:{[dir;pt;forcesave;tabname] /- check row count - /- forcesave will write flush the data to disk irrespective of counts - if[forcesave or maxrows[tabname] < arows: count value tabname; + arows:count value tabname; .lg.o[`rowcheck;"the ",(string tabname)," table consists of ", (string arows), " rows"]; - /- upsert data to partition - .lg.o[`save;"saving ",(string tabname)," data to partition ", string pt]; - .[ - upsert; - (` sv .Q.par[dir;pt;tabname],`;.Q.en[hdbsettings[`hdbdir];r:0!.save.manipulate[tabname;`. tabname]]); - {[e] .lg.e[`savetables;"Failed to save table to disk : ",e];'e} - ]; - /- make addition to tabsizes - .lg.o[`track;"appending table details to tabsizes"]; - .wdb.tabsizes+:([tablename:enlist tabname]rowcount:enlist arows;bytes:enlist -22!r); - /- empty the table - .lg.o[`delete;"deleting ",(string tabname)," data from in-memory table"]; - @[`.;tabname;0#]; - /- run a garbage collection (if enabled) - if[gc;.gc.run[]]; - :1b; - ]; 0b}; + + /- exit early if 0 row count, return 0b for no changes to table + if[0 = arows;:0b]; + + /- forcesave will write flush the data to disk irrespective of counts + if[forcesave or maxrows[tabname] < arows; + /- upsert data to partition + .lg.o[`save;"saving ",(string tabname)," data to partition ", string pt]; + .[ + upsert; + (` sv .Q.par[dir;pt;tabname],`;.Q.en[hdbsettings[`hdbdir];r:0!.save.manipulate[tabname;`. tabname]]); + {[e] .lg.e[`savetables;"Failed to save table to disk : ",e];'e} + ]; + /- make addition to tabsizes + .lg.o[`track;"appending table details to tabsizes"]; + .wdb.tabsizes+:([tablename:enlist tabname]rowcount:enlist arows;bytes:enlist -22!r); + /- empty the table + .lg.o[`delete;"deleting ",(string tabname)," data from in-memory table"]; + @[`.;tabname;0#]; + /- run a garbage collection (if enabled) + if[gc;.gc.run[]]; + :1b; + ]; + :0b + }; + \d . /-endofperiod function