Skip to content

Commit

Permalink
Added fast path in njs_chb_utf8_length() for ASCII input.
Browse files Browse the repository at this point in the history
  • Loading branch information
xeioex committed May 24, 2024
1 parent 6d624bb commit 1250d03
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/njs_chb.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ njs_chb_size(njs_chb_t *chain)
njs_inline int64_t
njs_chb_utf8_length(njs_chb_t *chain)
{
u_char *p, *p_end;
size_t size;
int64_t len, length;
njs_chb_node_t *n;

Expand All @@ -107,6 +109,23 @@ njs_chb_utf8_length(njs_chb_t *chain)

length = 0;

while (n != NULL) {
p = n->start;
size = njs_chb_node_size(n);
p_end = p + size;

while (p < p_end && *p < 0x80) {
p++;
}

if (p != p_end) {
break;
}

length += size;
n = n->next;
}

while (n != NULL) {
len = njs_utf8_length(n->start, njs_chb_node_size(n));
if (njs_slow_path(len < 0)) {
Expand Down
33 changes: 33 additions & 0 deletions src/test/njs_benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,21 @@ static njs_benchmark_test_t njs_test[] =
njs_str("undefined"),
1 },

{ "string create chb 'x'.repeat(256)",
njs_str("benchmark.string('chb', 'x'.repeat(256), 10000)"),
njs_str("undefined"),
1 },

{ "string create chb 'Д'.repeat(128)",
njs_str("benchmark.string('chb', 'Д'.repeat(128), 10000)"),
njs_str("undefined"),
1 },

{ "string create chb 'x'.repeat(128) + 'Д'.repeat(64)",
njs_str("benchmark.string('chb', 'x'.repeat(128) + 'Д'.repeat(64), 10000)"),
njs_str("undefined"),
1 },

{ "JSON.parse",
njs_str("JSON.parse('{\"a\":123, \"XXX\":[3,4,null]}').a"),
njs_str("123"),
Expand Down Expand Up @@ -696,6 +711,7 @@ njs_benchmark_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_index_t unused, njs_value_t *retval)
{
int64_t i, n;
njs_chb_t chain;
njs_str_t s, mode;
njs_value_t value;

Expand All @@ -718,6 +734,23 @@ njs_benchmark_string(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
njs_string_create(vm, &value, s.start, s.length);
}

} else if (memcmp(mode.start, "chb", 3) == 0) {

NJS_CHB_MP_INIT(&chain, vm);

njs_chb_append_literal(&chain, "abc");
njs_chb_append(&chain, s.start, s.length);
njs_chb_append_literal(&chain, "abc");
njs_chb_append(&chain, s.start, s.length);
njs_chb_append_literal(&chain, "abc");
njs_chb_append(&chain, s.start, s.length);

for (i = 0; i < n; i++) {
njs_string_create_chb(vm, &value, &chain);
}

njs_chb_destroy(&chain);

} else {
njs_type_error(vm, "unknown mode \"%V\"", &mode);
return NJS_ERROR;
Expand Down

0 comments on commit 1250d03

Please sign in to comment.