From b154cfa98c01b17b8f4df82ebb7a5bc9ab7bf7bb Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 9 Apr 2026 18:50:53 +0200 Subject: [PATCH 1/2] Add a fastpath for `rb_str_normalize_ospath` This extra check is a hotspot for path operations on macOS. It was added in 9962aad7b0184e385b40c26c5a109bff7abbe43c 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. --- file.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/file.c b/file.c index 79d46b2de9670c..832e4b1cbbfb6c 100644 --- a/file.c +++ b/file.c @@ -380,9 +380,15 @@ rb_str_normalize_ospath(const char *ptr, long len) const char *p = ptr; const char *e = ptr + len; const char *p1 = p; - VALUE str = rb_str_buf_new(len); rb_encoding *enc = rb_utf8_encoding(); - rb_enc_associate(str, enc); + VALUE str = rb_utf8_str_new(ptr, len); + if (RB_LIKELY(rb_enc_str_coderange(str) == ENC_CODERANGE_7BIT)) { + return str; + } + else { + str = rb_str_buf_new(len); + rb_enc_associate(str, enc); + } while (p < e) { int l, c; From cfec60d4bec0d99e17ed56bea7055de7ec7674a1 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 21 Jan 2026 20:33:22 +0100 Subject: [PATCH 2/2] dir.c: cache and revalidate working directory `rb_dir_getwd_ospath()` is called quite frequently, but the overwhelming majority of the time, the current directory didn't change. We can also assume that most of the time, `PATH_MAX` is enough for `getcwd`, hence we can first attempt to use a small stack buffer rather than always allocate on the heap. This way we can keep the last `pwd` and revalidate it with no allocation. On macOS syscalls are fairly slow, so the gain isn't very large. macOS: ``` compare-ruby: ruby 4.1.0dev (2026-04-09T05:19:02Z master c091c186e4) +PRISM [arm64-darwin25] built-ruby: ruby 4.1.0dev (2026-04-09T06:37:20Z get-cwd-cache ea02126d79) +PRISM [arm64-darwin25] ``` | |compare-ruby|built-ruby| |:--------|-----------:|---------:| |Dir.pwd | 105.183k| 113.420k| | | -| 1.08x| ``` Linux (inside virtualized Docker) ``` compare-ruby: ruby 4.1.0dev (2026-04-07T08:26:25Z master fcd210086c) +PRISM [aarch64-linux] built-ruby: ruby 4.1.0dev (2026-04-09T06:38:09Z get-cwd-cache 6774af9ba7) +PRISM [aarch64-linux] ``` | |compare-ruby|built-ruby| |:--------|-----------:|---------:| |Dir.pwd | 4.157M| 5.541M| | | -| 1.33x| --- benchmark/dir_pwd.yml | 2 ++ dir.c | 32 +++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 benchmark/dir_pwd.yml diff --git a/benchmark/dir_pwd.yml b/benchmark/dir_pwd.yml new file mode 100644 index 00000000000000..c435d3ac5eb877 --- /dev/null +++ b/benchmark/dir_pwd.yml @@ -0,0 +1,2 @@ +benchmark: + pwd: Dir.pwd diff --git a/dir.c b/dir.c index d67de8cf06c830..d81ae28ee9e1b3 100644 --- a/dir.c +++ b/dir.c @@ -1585,6 +1585,8 @@ dir_chdir(VALUE dir) #endif } +static VALUE last_cwd; + #ifndef _WIN32 static VALUE getcwd_to_str(VALUE arg) @@ -1604,12 +1606,35 @@ getcwd_xfree(VALUE arg) return Qnil; } -VALUE -rb_dir_getwd_ospath(void) +static VALUE +rb_dir_getwd_ospath_slowpath(void) { char *path = ruby_getcwd(); return rb_ensure(getcwd_to_str, (VALUE)path, getcwd_xfree, (VALUE)path); } + +VALUE +rb_dir_getwd_ospath(void) +{ + char buf[PATH_MAX]; + char *path = getcwd(buf, PATH_MAX); + if (!path) { + return rb_dir_getwd_ospath_slowpath(); + } + + VALUE cached_cwd = RUBY_ATOMIC_VALUE_LOAD(last_cwd); + + if (!cached_cwd || strcmp(RSTRING_PTR(cached_cwd), path) != 0) { +#ifdef __APPLE__ + cached_cwd = rb_str_normalize_ospath(path, strlen(path)); +#else + cached_cwd = rb_str_new2(path); +#endif + rb_str_freeze(cached_cwd); + RUBY_ATOMIC_VALUE_SET(last_cwd, cached_cwd); + } + return cached_cwd; +} #endif VALUE @@ -1617,7 +1642,7 @@ rb_dir_getwd(void) { rb_encoding *fs = rb_filesystem_encoding(); int fsenc = rb_enc_to_index(fs); - VALUE cwd = rb_dir_getwd_ospath(); + VALUE cwd = rb_str_new_shared(rb_dir_getwd_ospath()); switch (fsenc) { case ENCINDEX_US_ASCII: @@ -4008,6 +4033,7 @@ Init_Dir(void) rb_gc_register_address(&chdir_lock.path); rb_gc_register_address(&chdir_lock.thread); + rb_gc_register_address(&last_cwd); rb_cDir = rb_define_class("Dir", rb_cObject);