From 002bd8b5a0901579273f48f17913d34f60f9e5a6 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 28 Jul 2021 21:02:23 +0200 Subject: [PATCH 1/2] Reduce table allocations in TSV output format table.concat({...}, "\n") creates a table for each line, which becomes immediate garbage. Handy when you don't know how many arguments will be used, but here the number of arguments is known. Suboptimal to allocate more memory when this thing is usually run when a lot of memory is already used. --- dump.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dump.lua b/dump.lua index 9ac864a..a06c4cb 100644 --- a/dump.lua +++ b/dump.lua @@ -131,8 +131,10 @@ local function dump_state(file, options) local TSV = {}; function TSV.header() print("id\ttype\tkey_id\tval_id\tkey_json\tval_json"); end - function TSV.vertex(...) print(table.concat({...}, "\t")); end - function TSV.edge(...) print(table.concat({...}, "\t")); end + function TSV.vertex(id, type, key_id, val_id, key_json, val_json) + print(id.."\t"..type.."\t"..key_id.."\t"..val_id.."\t"..key_json.."\t"..val_json); + end + TSV.edge = TSV.vertex; local CSV = {}; local function tsv2csv(s) return s:gsub("\"", "\"\""):gsub("[^\t]+", "\"%1\""):gsub("\t", ","); end From 935447d7dd341d86118d172e0e73573fe429df15 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 28 Jul 2021 21:06:34 +0200 Subject: [PATCH 2/2] Rewrite CSV output formatter Previously simple values like '1' were escaped into '"""1"""' which seems wrong. Also gets rid of the table allocation for each line, see previous commit for some words on that. --- dump.lua | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dump.lua b/dump.lua index a06c4cb..d9363ae 100644 --- a/dump.lua +++ b/dump.lua @@ -137,10 +137,12 @@ local function dump_state(file, options) TSV.edge = TSV.vertex; local CSV = {}; - local function tsv2csv(s) return s:gsub("\"", "\"\""):gsub("[^\t]+", "\"%1\""):gsub("\t", ","); end - function CSV.header() print(tsv2csv("id\ttype\tkey_id\tval_id\tkey_json\tval_json")); end - function CSV.vertex(...) print(tsv2csv(table.concat({...}, "\t"))); end - function CSV.edge(...) print(tsv2csv(table.concat({...}, "\t"))); end + local function esc(s) if s:find("[,\"]") then return "\"" .. s:gsub("\"", "\"\"") .. "\""; else return s end end + function CSV.header() print("id,type,key_id,val_id,key_json,val_json"); end + function CSV.vertex(id, type, key_id, val_id, key_json, val_json) + print(esc(id)..","..esc(type)..","..esc(key_id)..","..esc(val_id)..","..esc(key_json)..","..esc(val_json)); + end + CSV.edge = CSV.vertex; local format = options and options.format == "TSV" and TSV or CSV; local header = options and options.header and format.header or function() end;