Skip to content
Myr

Improve performance of repeating strings (#3272)

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

This commit improves the performance of repeating strings, by copying the result string instead of the string being repeated. Also it adds an error message when the result string is too long.

authored by itchyny · committed by GitHub · parent 5df471f · Browse files · 4 files+41 added−13 removed
This commit

State

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

Diff stats

4 files+41 added−13 removed

  • tests:+12 added−0 removed
  • other:+29 added−13 removed

4 files+41 added−13 removed

modified:src/builtin.c+2 added-13 removed
341 unmodified lines
342 if (ak == JV_KIND_NUMBER) {343 str = b;344 num = a;345 }346 jv res;346 double d = jv_number_value(num);348 if (d < 0 || isnan(d)) {349 res = jv_null();350 } else {351 int n = d;352 size_t alen = jv_string_length_bytes(jv_copy(str));353 res = jv_string_empty(alen * n);354 for (; n > 0; n--) {355 res = jv_string_append_buf(res, jv_string_value(str), alen);356 }357 }358 jv_free(str);347 jv_free(num);360 return res;348 return jv_string_repeat(str,349 d < 0 || isnan(d) ? -1 : d > INT_MAX ? INT_MAX : (int)d);350 } else if (ak == JV_KIND_OBJECT && bk == JV_KIND_OBJECT) {351 return jv_object_merge_recursive(a, b);352 } else {353 return type_error2(a, b, "cannot be multiplied");
1711 unmodified lines
modified:src/jv.c+26 added
1322 unmodified lines
1323 jv_free(k);1324 return a;1325}13261327jv jv_string_repeat(jv j, int n) {1328 assert(JVP_HAS_KIND(j, JV_KIND_STRING));1329 if (n < 0) {1330 jv_free(j);1331 return jv_null();1332 }1333 int len = jv_string_length_bytes(jv_copy(j));1334 int64_t res_len = (int64_t)len * n;1335 if (res_len >= INT_MAX) {1336 jv_free(j);1337 return jv_invalid_with_msg(jv_string("Repeat string result too long"));1338 }1339 if (res_len == 0) {1340 jv_free(j);1341 return jv_string("");1342 }1343 jv res = jv_string_empty(res_len);1344 res = jvp_string_append(res, jv_string_value(j), len);1345 for (int curr = len, grow; curr < res_len; curr += grow) {1346 grow = MIN(res_len - curr, curr);1347 res = jvp_string_append(res, jv_string_value(res), grow);1348 }1349 jv_free(j);1350 return res;1351}13521353jv jv_string_split(jv j, jv sep) {1354 assert(JVP_HAS_KIND(j, JV_KIND_STRING));1355 assert(JVP_HAS_KIND(sep, JV_KIND_STRING));1356 const char *jstr = jv_string_value(j);
681 unmodified lines
modified:src/jv.h+1 added
134 unmodified lines
135jv jv_string_fmt(const char*, ...) JV_PRINTF_LIKE(1, 2);136jv jv_string_append_codepoint(jv a, uint32_t c);137jv jv_string_append_buf(jv a, const char* buf, int len);138jv jv_string_append_str(jv a, const char* str);139jv jv_string_repeat(jv j, int n);140jv jv_string_split(jv j, jv sep);141jv jv_string_explode(jv j);142jv jv_string_implode(jv j);143
157 unmodified lines
modified:tests/jq.test+12 added
1458 unmodified lines
1459[. * (nan,-nan)]1460"abc"1461[null,null]14621463. * 100000 | [.[:10],.[-10:]]1464"abc"1465["abcabcabca","cabcabcabc"]14661467. * 10000000001468""1469""14701471try (. * 1000000000) catch .1472"abc"1473"Repeat string result too long"14741475[.[] / ","]1476["a, bc, def, ghij, jklmn, a,b, c,d, e,f", "a,b,c,d, e,f,g,h"]1477[["a"," bc"," def"," ghij"," jklmn"," a","b"," c","d"," e","f"],["a","b","c","d"," e","f","g","h"]]1478
837 unmodified lines