This is an automated email from the ASF dual-hosted git repository. nickva pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/couchdb-jiffy.git
commit efaaae1fd24d203d97e2c8edad5e03e20fe0c3c3 Author: Nick Vatamaniuc <[email protected]> AuthorDate: Wed Apr 1 01:50:06 2026 -0400 Use the restrict pointer qualifier when decoding numbers What's good for the goose is good for the gander. Use the same "trick" in `dec_number` as we did in `dec_string`. To make it work pull out d->p and d->i and then avoiding updating d->i on every digit. Only update it once at the end. On master ``` ===== With input Giphy ===== Name ips average deviation median 99th % jiffy 404.94 2.47 ms +/-54.98% 2.26 ms 4.72 ms ===== With input GitHub ===== Name ips average deviation median 99th % jiffy 1.20 K 0.83 ms +/-27.98% 0.82 ms 1.60 ms ===== With input GovTrack ===== Name ips average deviation median 99th % jiffy 7.64 130.82 ms +/-11.64% 134.32 ms 153.93 ms ===== With input Issue 90 ===== Name ips average deviation median 99th % jiffy 42.52 23.52 ms +/-32.15% 20.04 ms 49.11 ms ===== With input JSON Generator ===== Name ips average deviation median 99th % jiffy 323.84 3.09 ms +/-60.78% 2.81 ms 8.63 ms ===== With input JSON Generator (Pretty) ===== Name ips average deviation median 99th % jiffy 221.14 4.52 ms +/-80.60% 3.53 ms 18.46 ms ===== With input Pokedex ===== Name ips average deviation median 99th % jiffy 307.46 3.25 ms +/-59.47% 2.84 ms 13.28 ms ``` With PR ``` ===== With input Giphy ===== Name ips average deviation median 99th % jiffy 494.10 2.02 ms +/-4.45% 2.01 ms 2.37 ms ===== With input GitHub ===== Name ips average deviation median 99th % jiffy 1.54 K 0.65 ms +/-33.14% 0.68 ms 0.78 ms ===== With input GovTrack ===== Name ips average deviation median 99th % jiffy 8.38 119.34 ms +/-26.92% 110.64 ms 226.90 ms ===== With input Issue 90 ===== Name ips average deviation median 99th % jiffy 46.22 21.64 ms +/-4.33% 21.30 ms 26.07 ms ===== With input JSON Generator ===== Name ips average deviation median 99th % jiffy 408.25 2.45 ms +/-9.09% 2.48 ms 2.92 ms ===== With input JSON Generator (Pretty) ===== Name ips average deviation median 99th % jiffy 338.91 2.95 ms +/-73.30% 2.76 ms 5.37 ms ===== With input Pokedex ===== Name ips average deviation median 99th % jiffy 484.97 2.06 ms +/-3.21% 2.07 ms 2.24 ms ``` --- c_src/decoder.c | 70 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/c_src/decoder.c b/c_src/decoder.c index 0b440d9..1f67b41 100644 --- a/c_src/decoder.c +++ b/c_src/decoder.c @@ -381,23 +381,32 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) ERL_NIF_TERM num_type = d->atoms->atom_error; char state = nst_init; char nbuf[NUM_BUF_LEN]; - int st = d->i; int has_frac = 0; int has_exp = 0; double dval; long lval; - while(d->i < d->len) { + // Use the same trick as did for dec_string. The restrict qualifier hints + // to the compiler p won't alias any other pointers so it can optimize + // access to it. Also avoid writing back do d->i on every increment, + // instead increment a local variable (hopefully in a register) then update + // d->i once at the end. + const unsigned char* JIFFY_RESTRICT p = d->p; + const size_t len = d->len; + const size_t start = d->i; + size_t idx = start; + + while(idx < len) { switch(state) { case nst_init: - switch(d->p[d->i]) { + switch(p[idx]) { case '-': state = nst_sign; - d->i++; + idx++; break; case '0': state = nst_frac0; - d->i++; + idx++; break; case '1': case '2': @@ -409,18 +418,19 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) case '8': case '9': state = nst_mantissa; - d->i++; + idx++; break; default: + d->i = idx; return 0; } break; case nst_sign: - switch(d->p[d->i]) { + switch(p[idx]) { case '0': state = nst_frac0; - d->i++; + idx++; break; case '1': case '2': @@ -432,23 +442,24 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) case '8': case '9': state = nst_mantissa; - d->i++; + idx++; break; default: + d->i = idx; return 0; } break; case nst_mantissa: - switch(d->p[d->i]) { + switch(p[idx]) { case '.': state = nst_frac1; - d->i++; + idx++; break; case 'e': case 'E': state = nst_esign; - d->i++; + idx++; break; case '0': case '1': @@ -460,7 +471,7 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) case '7': case '8': case '9': - d->i++; + idx++; break; default: goto parse; @@ -468,15 +479,15 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) break; case nst_frac0: - switch(d->p[d->i]) { + switch(p[idx]) { case '.': state = nst_frac1; - d->i++; + idx++; break; case 'e': case 'E': state = nst_esign; - d->i++; + idx++; break; default: goto parse; @@ -485,7 +496,7 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) case nst_frac1: has_frac = 1; - switch(d->p[d->i]) { + switch(p[idx]) { case '0': case '1': case '2': @@ -497,7 +508,7 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) case '8': case '9': state = nst_frac; - d->i++; + idx++; break; default: goto parse; @@ -505,11 +516,11 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) break; case nst_frac: - switch(d->p[d->i]) { + switch(p[idx]) { case 'e': case 'E': state = nst_esign; - d->i++; + idx++; break; case '0': case '1': @@ -521,7 +532,7 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) case '7': case '8': case '9': - d->i++; + idx++; break; default: goto parse; @@ -530,7 +541,7 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) case nst_esign: has_exp = 1; - switch(d->p[d->i]) { + switch(p[idx]) { case '-': case '+': case '0': @@ -544,15 +555,16 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) case '8': case '9': state = nst_edigit; - d->i++; + idx++; break; default: + d->i = idx; return 0; } break; case nst_edigit: - switch(d->p[d->i]) { + switch(p[idx]) { case '0': case '1': case '2': @@ -563,7 +575,7 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) case '7': case '8': case '9': - d->i++; + idx++; break; default: goto parse; @@ -571,11 +583,13 @@ dec_number(Decoder* d, ERL_NIF_TERM* value) break; default: + d->i = idx; return 0; } } parse: + d->i = idx; switch(state) { case nst_init: @@ -589,9 +603,9 @@ parse: errno = 0; - if(d->i - st < NUM_BUF_LEN) { + if(d->i - start < NUM_BUF_LEN) { memset(nbuf, 0, NUM_BUF_LEN); - memcpy(nbuf, &(d->p[st]), d->i - st); + memcpy(nbuf, &p[start], d->i - start); if(has_frac || has_exp) { dval = strtod(nbuf, NULL); @@ -617,7 +631,7 @@ parse: } d->is_partial = 1; - *value = enif_make_sub_binary(d->env, d->arg, st, d->i - st); + *value = enif_make_sub_binary(d->env, d->arg, start, d->i - start); *value = enif_make_tuple2(d->env, num_type, *value); return 1; }
