Skip to content

Commit b154cfa

Browse files
committed
Add a fastpath for rb_str_normalize_ospath
This extra check is a hotspot for path operations on macOS. It was added in 9962aad because of a limitation of HFS+. But all the invalid characters are outside of ASCII range, and most paths are ASCII, so we can optimistically check the coderange instead. Most `rb_str_normalize_ospath` were first checking for ASCII range, but a few like `rb_dir_getwd_ospath` in `dir.c` or `ospath_new` in `file.c` didn't.
1 parent 36b0ae0 commit b154cfa

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

file.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,15 @@ rb_str_normalize_ospath(const char *ptr, long len)
380380
const char *p = ptr;
381381
const char *e = ptr + len;
382382
const char *p1 = p;
383-
VALUE str = rb_str_buf_new(len);
384383
rb_encoding *enc = rb_utf8_encoding();
385-
rb_enc_associate(str, enc);
384+
VALUE str = rb_utf8_str_new(ptr, len);
385+
if (RB_LIKELY(rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT)) {
386+
return str;
387+
}
388+
else {
389+
str = rb_str_buf_new(len);
390+
rb_enc_associate(str, enc);
391+
}
386392

387393
while (p < e) {
388394
int l, c;

0 commit comments

Comments
 (0)