quinedb icon indicating copy to clipboard operation
quinedb copied to clipboard

Use `printf %q` for simplicity and binary safety

Open Artoria2e5 opened this issue 7 years ago • 3 comments

Minor performance improvement from 11.331s to 10.937s.

Now it's able to hold arbitrary null-terminated bytes. (You may want to write some tests)

for ((i=0; i<512; i++)); do j+=($(head -c 16 /dev/urandom)); done
declare -p j > testdata
e=({a,A}{b,B}{c,C}{d,D}{e,E}{f,F}{g,G}{H,h}{i,I})
declare -p e >> testdata

for impl in ./quinedb_*; do
  TIMEFORMAT="$impl:"$'\nreal\t%R\nuser\t%U\nsys\t%S\n'
  time for ((i=0; i<32; i++)); do
    bash "$impl" set "${e[i]}" "${j[i]}" > qdb.out || break
    mv qdb.out "$impl"
  done
done

testdata.txt

./quinedb_+=:
real    11.331
user    0.578
sys     10.609
DB IS LOSSY

./quinedb_pf:
real    10.937
user    0.328
sys     10.703

An example of byte-safety:

/tmp$ bash ./quinedb_pf get abcdefghi >/dev/null
$'\3257\327\354I\206\374\364\306\\\253\216,\234\005\370'
/tmp$ bash ./quinedb_+= get abcdefghi >/dev/null
$'7I\\'

Warning on newlines and grey-area undocumented behavior:

printf %q only gives reusable strings, and there is no guarantee that the string will not contain newlines. The current (4.4) implementation does not produce newlines though:

/* bash-4.4/builtins/printf.def:575 */
if (p && *p == 0)        /* XXX - getstr never returns null */
  xp = savestring ("''");
else if (ansic_shouldquote (p))
  xp = ansic_quote (p, 0, (int *)0); // isprint('\n') == 0, so it always hits this branch
else
  xp = sh_backslash_quote (p, 0, 1); // even this branch has NL covered in lib/sh/shquote.c:bstab

Artoria2e5 avatar Sep 16 '16 22:09 Artoria2e5

Hmm, should kill b597667...

Artoria2e5 avatar Sep 16 '16 22:09 Artoria2e5

Would it be easy to post-process the results to replace newlines with \n?

gfredericks avatar Sep 20 '16 02:09 gfredericks

Would it be easy to post-process the results to replace newlines with \n?

Doing so correctly requires more understanding of the escapes by re-parsing it... Somehow worse than sticking to the current assumption that newlines always get escaped.

Artoria2e5 avatar Sep 20 '16 02:09 Artoria2e5