Skip to content
Myr

Improve performance of string output (#3643)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Printing a string wrote each character with a separate call, one fwrite() per character, or one jv_string_append_buf() for tojson and tostring, which made output of string-heavy data slow. Scan ahead for the next byte that needs escaping and write the run of bytes before it with a single call, without changing the output. Printing strings is now significantly faster, most of all for long strings with few characters to escape, and the same applies to tojson and tostring. With --ascii-output, text that is almost all non-ASCII is about the same speed, since nearly every byte is escaped anyway.

authored by Michiel van Slobbe · committed by GitHub · parent a5323a3 · Browse files · 3 files+35 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+35 added−1 removed

  • tests:+23 added−0 removed
  • other:+12 added−1 removed

3 files+35 added−1 removed

modified:src/jv_print.c+12 added-1 removed
124 unmodified lines
125 const char* cstart;126 int c = 0;127 char buf[32];128 put_char('"', F, S, T);129 while ((i = jvp_utf8_next((cstart = i), end, &c))) {129 while (i < end) {130 // Write each run of bytes that need no escaping with a single call.131 for (cstart = i; cstart < end; cstart++) {132 unsigned char b = *cstart;133 if (b < 0x20 || b == '"' || b == '\\' || b == 0x7F || (ascii_only && b >= 0x80))134 break;135 }136 if (cstart > i)137 put_buf(i, cstart - i, F, S, T);138 if (cstart == end)139 break;140 i = jvp_utf8_next(cstart, end, &c);141 assert(c != -1);142 int unicode_escape = 0;143 if (0x20 <= c && c <= 0x7E) {144 // printable ASCII
287 unmodified lines
modified:tests/jq.test+9 added
98 unmodified lines
99[.[]|tojson|fromjson]100["foo", 1, ["a", 1, "b", 2, {"foo":"bar"}]]101["foo",1,["a",1,"b",2,{"foo":"bar"}]]102103# Put escapes on both sides of runs of bytes that need no escaping104map(tojson)105["abcdefg\"", "abcdefgh\\", "abcdefghi\u0001j", "abcdefghijklmno\u007f", "abcdefgh\té", "abcdefghijklmnop\n"]106["\"abcdefg\\\"\"","\"abcdefgh\\\\\"","\"abcdefghi\\u0001j\"","\"abcdefghijklmno\\u007f\"","\"abcdefgh\\té\"","\"abcdefghijklmnop\\n\""]107108[range(18) as $i | ("\"", "\\", "\u0001", "\u007f", "é", "😀") as $c | "abcdefghijklmnopq"[:$i] + $c + "abcdefghijklmnopq"[$i:] | (tojson | fromjson) == .] | all109null110true111112#113# Dictionary construction syntax114#115
2715 unmodified lines
modified:tests/utf8test+14 added
19 unmodified lines
20 exit 121 fi22done2324# --ascii-output escapes non-ASCII characters on both sides of runs of25# bytes that need no escaping26$VALGRIND $Q $JQ -cn '[range(18) as $i | ("é", "😀", "\u007f") as $c |27 "abcdefghijklmnopq"[:$i] + $c + "abcdefghijklmnopq"[$i:]]' > $d/input28$VALGRIND $Q $JQ -ac . $d/input > $d/out29if LC_ALL=C grep -q '[^ -~]' $d/out; then30 echo "--ascii-output printed a non-ASCII byte"31 exit 132fi33if ! $VALGRIND $Q $JQ -e --slurpfile a $d/input '. == $a[0]' $d/out > /dev/null; then34 echo "--ascii-output changed a string"35 exit 136fi3738exit 0