Skip to content

Optimize File.basename#15919

Merged
byroot merged 1 commit intoruby:masterfrom
byroot:opt-basename
Jan 21, 2026
Merged

Optimize File.basename#15919
byroot merged 1 commit intoruby:masterfrom
byroot:opt-basename

Conversation

@byroot
Copy link
Copy Markdown
Member

@byroot byroot commented Jan 21, 2026

The actual algorithm is largely unchanged, just allowed to use singlebyte checks for common encodings.

It could certainly be optimized much further, as here again it often scans from the front of the string when we're interested in the back of it. But the algorithm as many Windows only corner cases so I'd rather ship a good improvement now and eventually come back to it later.

Most of improvement here is from the reduced setup cost (avodi double null checks, avoid duping the argument, etc), and skipping the multi-byte checks.

compare-ruby: ruby 4.1.0dev (2026-01-19T03:51:30Z master 631bf19b37) +PRISM [arm64-darwin25]
built-ruby: ruby 4.1.0dev (2026-01-21T08:21:05Z opt-basename 7eb11745b2) +PRISM [arm64-darwin25]
compare-ruby built-ruby
long 3.412M 18.158M
- 5.32x
long_name 1.981M 8.580M
- 4.33x
withext 3.200M 12.986M
- 4.06x

Comment thread internal/string.h
Comment thread internal/string.h Outdated
The actual algorithm is largely unchanged, just allowed to use
singlebyte checks for common encodings.

It could certainly be optimized much further, as here again it often
scans from the front of the string when we're interested in the back of
it. But the algorithm as many Windows only corner cases so I'd rather
ship a good improvement now and eventually come back to it later.

Most of improvement here is from the reduced setup cost (avodi double
null checks, avoid duping the argument, etc), and skipping the multi-byte
checks.

```
compare-ruby: ruby 4.1.0dev (2026-01-19T03:51:30Z master 631bf19) +PRISM [arm64-darwin25]
built-ruby: ruby 4.1.0dev (2026-01-21T08:21:05Z opt-basename 7eb1174) +PRISM [arm64-darwin25]
```

|           |compare-ruby|built-ruby|
|:----------|-----------:|---------:|
|long       |      3.412M|   18.158M|
|           |           -|     5.32x|
|long_name  |      1.981M|    8.580M|
|           |           -|     4.33x|
|withext    |      3.200M|   12.986M|
|           |           -|     4.06x|
@byroot byroot enabled auto-merge (rebase) January 21, 2026 10:04
@byroot byroot merged commit 519a4bd into ruby:master Jan 21, 2026
98 of 100 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants