@ 261102.1

261020.1 · 852c4d609611d1177de832010ff2de810ef026c8

Improve performance of UTF-8 validation (#3675)

jvp_utf8_next() had a fast path for ASCII, but it is not inlined into its callers, so every byte of a string cost a function call.

Move the end check and the ASCII case into a static inline function in jv_unicode.h and keep the rest out of line. This speeds up parsing of string-heavy input by up to 1.17x and string functions such as length, explode and @html, with the same output for every input.

reading declarations

Changes to src/jv_unicode.h. All files

src/jv_unicode.h

@@ -1,10 +1,27 @@ #ifndef JV_UNICODE_H #define JV_UNICODE_H +#include <assert.h>+ const char* jvp_utf8_backtrack(const char* start, const char* min, int *missing_bytes);-const char* jvp_utf8_next(const char* in, const char* end, int* codepoint);+const char* jvp_utf8_next_multibyte(const char* in, const char* end, int* codepoint); int jvp_utf8_is_valid(const char* in, const char* end); +// Decode one codepoint.  ASCII is handled here so that callers looping over+// a string do not make a call per byte; everything else goes to+// jvp_utf8_next_multibyte().+static inline const char* jvp_utf8_next(const char* in, const char* end, int* codepoint) {+  if (in >= end) {+    assert(in == end);+    return 0;+  }+  if (!(*in & 0x80)) {+    *codepoint = (unsigned char)*in;+    return in + 1;+  }+  return jvp_utf8_next_multibyte(in, end, codepoint);+}+ int jvp_utf8_decode_length(char startchar);  int jvp_utf8_encode_length(int codepoint);