Commit 53fe993
committed
Optimize
Similar optimizations to the ones performed in rubyGH-15907.
- Skip the expensive multi-byte encoding handling for the common
encodings that are known to be safe.
- Use `CheckPath` to save on copying the argument and only scan it for
NULL bytes once.
- Create the return string with rb_enc_str_new instead of rb_str_subseq
as it's going to be a very small string anyway.
This could be optimized a little bit further by searching for both `.` and `dirsep`
in one pass,
```
compare-ruby: ruby 4.1.0dev (2026-01-19T03:51:30Z master 631bf19) +PRISM [arm64-darwin25]
built-ruby: ruby 4.1.0dev (2026-01-20T07:33:42Z master 6fb5043) +PRISM [arm64-darwin25]
```
| |compare-ruby|built-ruby|
|:----------|-----------:|---------:|
|long | 3.606M| 22.229M|
| | -| 6.17x|
|long_name | 2.254M| 13.416M|
| | -| 5.95x|
|short | 16.488M| 29.969M|
| | -| 1.82x|File.extname for common encodings1 parent 6fb5043 commit 53fe993
2 files changed
Lines changed: 40 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5126 | 5126 | | |
5127 | 5127 | | |
5128 | 5128 | | |
5129 | | - | |
5130 | | - | |
5131 | | - | |
5132 | | - | |
5133 | | - | |
5134 | | - | |
5135 | | - | |
5136 | | - | |
5137 | | - | |
5138 | | - | |
5139 | | - | |
5140 | | - | |
5141 | | - | |
5142 | | - | |
| 5129 | + | |
| 5130 | + | |
5143 | 5131 | | |
5144 | 5132 | | |
5145 | 5133 | | |
5146 | | - | |
| 5134 | + | |
5147 | 5135 | | |
5148 | 5136 | | |
5149 | 5137 | | |
| |||
5176 | 5164 | | |
5177 | 5165 | | |
5178 | 5166 | | |
5179 | | - | |
| 5167 | + | |
5180 | 5168 | | |
5181 | 5169 | | |
5182 | 5170 | | |
| |||
5191 | 5179 | | |
5192 | 5180 | | |
5193 | 5181 | | |
| 5182 | + | |
| 5183 | + | |
| 5184 | + | |
| 5185 | + | |
| 5186 | + | |
| 5187 | + | |
| 5188 | + | |
| 5189 | + | |
| 5190 | + | |
| 5191 | + | |
| 5192 | + | |
| 5193 | + | |
| 5194 | + | |
| 5195 | + | |
| 5196 | + | |
| 5197 | + | |
| 5198 | + | |
| 5199 | + | |
5194 | 5200 | | |
5195 | 5201 | | |
5196 | 5202 | | |
| |||
5220 | 5226 | | |
5221 | 5227 | | |
5222 | 5228 | | |
5223 | | - | |
5224 | | - | |
5225 | | - | |
| 5229 | + | |
| 5230 | + | |
| 5231 | + | |
5226 | 5232 | | |
5227 | | - | |
5228 | | - | |
5229 | | - | |
5230 | | - | |
5231 | | - | |
5232 | | - | |
5233 | | - | |
5234 | | - | |
| 5233 | + | |
| 5234 | + | |
| 5235 | + | |
| 5236 | + | |
| 5237 | + | |
| 5238 | + | |
| 5239 | + | |
| 5240 | + | |
| 5241 | + | |
5235 | 5242 | | |
5236 | 5243 | | |
5237 | 5244 | | |
| |||
0 commit comments