Repository navigation
#89: faster Dir.glob, shared Regexp translations, tighter count/delete/squeeze loops - #175
Conversation
) Every matched entry used to be asked about again: DirectoryExists plus FileExists for a last segment, DirectoryExists before walking into it, a DirectoryInfo/LinkTarget check under "**", and "**/x" listed every directory twice. On the real file system (a PAL that does not override FileExists, DirectoryExists or GetFileSystemEntries) the listing now comes from FileSystemEnumerable and keeps readdir's answer of whether an entry is a directory (following a symbolic link, as DirectoryExists does). A listed last segment is reported without another check, a non-directory is not walked into, the symlink check runs for directories only, and the last listing is reused. Behaviour is unchanged: a dangling link is still reported (FileExists said true for one, and MRI lists it), "*/" and "**" still see links to directories, sorting is per directory as before. On Windows a reparse point keeps the old checks, since its Directory attribute says nothing about its target. A host PAL with its own file system is still asked through its own methods. Release net8.0, main-thread CPU time, a 20-directory tree: Dir.glob("*/*.rb") ~5.1 ms -> 0.65 ms (CRuby 0.50) Dir.glob("**/*.txt") ~19.4 ms -> 1.65 ms (CRuby 0.96) Dir.glob("**/*") ~23.3 ms -> 1.65 ms (CRuby 0.91) Dir.glob("d1/*") ~257 us -> 47 us (CRuby 26) Co-Authored-By: Claude Opus 5.5 <[email protected]>
…#89) Regexp.new and every evaluation of an interpolated literal (/#{x}/) translated the pattern and had .NET parse the result. A cache of the last 256 patterns, keyed on the translation's input, the options and the timeout, now hands out the same CLR Regex (immutable and thread-safe) together with its \G flag. Failed patterns are not cached, so a bad one still raises every time. Tier-up stays per instance: each Regexp counts its own matches, and the one that compiles its regex puts the compiled one in the cache, so Regexps of that pattern made afterwards start out compiled. Release net8.0, main-thread CPU time: Regexp.new("foo(\\d+)bar[a-z]*") ~6.5-9.8 us -> 0.33 us (CRuby 4.1) /#{x}\d+(?:y|z)/ ~2.7-3.5 us -> 0.48 us (CRuby 3.3) "zabc12y" =~ /#{x}\d+(?:y|z)/ ~4.3-4.7 us -> 0.89 us (CRuby 4.0) Co-Authored-By: Claude Opus 5.5 <[email protected]>
The loops from #171 were inlined into InternalCount and friends, large methods, where RyuJIT kept the loop index on the stack: every character waited for a store and a reload (about 2 ns a character, 3x a standalone loop). They are now NoInlining methods, with the non-ASCII test out of line, and: - a set of one ASCII character ("l") is counted with the vectorized MemoryExtensions.Count; - when no selector can pick a non-ASCII character, those skip the test; - from 256 characters on, ASCII characters are looked up in a 128-byte table built once per set (short strings keep the mask test, so they pay nothing for it); - #delete and #squeeze store each character and advance past it unless it is dropped, instead of branching on every one. Release net8.0, main-thread CPU time, 1140 ASCII characters: count("lo") ~1.6 us -> 1.2 us (CRuby 1.2) count("l") ~1.7 us -> 0.45 us (CRuby 0.46) count("a-y") ~1.7 us -> 1.2 us (CRuby 1.3) delete("l") ~3.3 us -> 2.0-2.4 us (CRuby 2.0) delete("a-m") ~3.0 us -> 2.2-2.4 us (CRuby 2.1) squeeze ~3.3 us -> 2.3-2.4 us (CRuby 1.3) squeeze("l") ~3.6 us -> 3.3 us (CRuby 1.2) 11-character strings are unchanged (0.2-0.45 us). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Part of #89. Three of its unticked items that #171 did not cover, one commit each. Behaviour is unchanged; the expected outputs of the new tests are CRuby 4.0's.
#89 items this addresses
Dir.globis still ~5–8× slower than CRuby (Strings and regexps). The per-entry existence checks are gone without changing how broken symlinks are handled.Regexp.newand interpolated regex literals are ~4× and ~1.8× slower than CRuby. There is now a shared regex cache, and tier-up still works per instance.String#count,#deleteand#squeeze: the remaining per-character overhead.ljust/centerwith a 10000-character result measure the same as CRuby now (14 µs both), so nothing was changed there.All numbers are main-thread CPU time (
/proc/thread-self/stat), from Release net8.0 builds offork/master(8cc413b) and of this branch, run interleaved A B B A. Each figure is the best of 3 rounds of at least 300 ms.1.
Dir.glob(8abce10)Every matched entry used to be checked again:
DirectoryExists+FileExistsfor a last segment;DirectoryExistsbefore walking into it;DirectoryInfo.LinkTargetcheck under**.**/xalso listed every directory twice.When the PAL is the real file system (it does not override
FileExists,DirectoryExistsorGetFileSystemEntries), the listing now comes fromFileSystemEnumerable, and each entry keepsIsDirectory. That flag follows a symlink, asDirectoryExistsdoes.Broken symlinks: a dangling link is still reported.
File.Existsanswered true for one, and MRI lists it too.*/and*/*still go through links to directories, and**still does not.Windows: a reparse point keeps the old checks, because its Directory attribute says nothing about its target.
Host PALs: a PAL with its own file system is still asked through its own methods.
Dir_GlobVirtualPal1covers this with a PAL that lists an entry itsFileExistsdenies.Dir.glob("*/*.rb")(520 hits)Dir.glob("**/*.txt")(1000 hits)Dir.glob("**/*")Dir.glob("d1/*")2.
Regexp.new(70ed244)The cache holds the last 256 patterns. It is keyed on the translation's input, the options and the timeout, and it gives back the same CLR
Regex(immutable and thread-safe) together with its\Gflag. Failed patterns are not cached, so a bad one still raises every time.Tier-up is still per instance: each Regexp counts its own matches. The one that compiles puts its compiled regex in the cache, so Regexps of that pattern made afterwards start compiled (
Regex_SharedTranslation2).Regexp.new("foo(\\d+)bar[a-z]*")/#{x}\d+(?:y|z)/"zabc12y" =~ /#{x}\d+(?:y|z)/3.
String#count/#delete/#squeeze(ca897b1)The loops from #171 were inlined into large methods. There, RyuJIT kept the loop index on the stack, so each character waited for a store and a reload: about 2 ns per character, three times a standalone loop (checked with
DOTNET_JitDisasmandperf). The changes:NoInlining.MemoryExtensions.Count.#deleteand#squeezestore every character and advance past it unless it is dropped, instead of branching.count("lo")count("l")count("a-y")delete("l")delete("a-m")squeezesqueeze("l")11-character strings are unchanged (0.2–0.45 µs).
squeezewith a set is still ~2.8× CRuby. What remains there is mostly the call path and the result allocation, not the loop.Verification
Dir_GlobListing1Dir_GlobVirtualPal1Regex_SharedTranslation1,Regex_SharedTranslation2String_CountDeleteSqueeze3chroot_spec, which hangs as root on master too), core/regexp, core/matchdata, library/pathname.String.try_convert, andRegexp.compile/new/try_convertwith a bad#to_str/#to_regexp.File.socket?specs errored twice, from the two runs sharing a socket path...,base:andFNM_DOTMATCH.count/delete/squeeze/!calls, with multibyte, astral, binary and negated or multiple sets.Regexp.new/match/scan/gsub over options, named groups,\G, timeouts, errors and hot loops.Not done
dup/+/strip/hashon ~100 KB strings,StringIO#read) need a shared-buffer design change.$~.stringfor a String subclass / frozen source andEnumerable#zipwith an Enumerable argument were done in #89: $~.string after sub, huge shift widths, faster Enumerable#zip and String#count/delete/squeeze #171. Their boxes in Performance gaps found while optimizing 2016-era code (not yet addressed) #89 are still unticked.Found, not changed (already on master; CRuby 4.0.9 differs)
Dir["d1/*/../f1.rb"]also reportsd1/link/../f1.rbthrough a symlinked directory.Dir["d1/f1.rb/../f2.rb"]matches through a file.Dir["**/*"]lists a directory's matches before descending into its subdirectories. MRI interleaves them per entry (a/sub/z.rbbeforea/x.rb). WithFNM_DOTMATCH,"."comes last instead of first.Errno::ELOOP.s.count("^\0-\x7f", "あ")is 0 in MRI, which looks like an MRI quirk with a negated first set and a multibyte set. IronRuby counts the あ.🤖 Generated with Claude Code
Summary by CodeRabbit