Skip to content

Fix caching gaps and reduce Pathname overhead#161

Open
pharris2411 wants to merge 1 commit intorubyatscale:mainfrom
pharris2411:ph/fix-caching-performance
Open

Fix caching gaps and reduce Pathname overhead#161
pharris2411 wants to merge 1 commit intorubyatscale:mainfrom
pharris2411:ph/fix-caching-performance

Conversation

@pharris2411
Copy link

@pharris2411 pharris2411 commented Mar 4, 2026

Summary

  • Cache nil results for unowned files: TeamFinder.for_file previously skipped caching when RustCodeOwners.for_file returned nil, causing repeated calls to the Rust layer for every lookup of an unowned file. Now caches nil so subsequent lookups are instant.
  • Memoize Pathname.pwd: The working directory doesn't change during a process, but Pathname.pwd was called on every invocation, issuing a syscall each time. Now memoized in FilePathFinder.pwd.
  • Replace Pathname#relative_path_from with string prefix strip: For files under the project root (the common case), we use String#delete_prefix instead of Pathname.new(path).relative_path_from(pwd), avoiding costly Pathname allocations, cleanpath, and cleanpath_aggressive chains. Falls back to Pathname for paths outside the project root.

Context

Profiling CodeOwnership.for_file in a large Rails app (Gusto/zenpayroll) showed:

  • 59% of time spent in Pathname.pwd syscalls
  • Most remaining time in Pathname#relative_path_fromcleanpathFile.basename chains
  • Unowned files were never cached, hitting the Rust layer on every call

Results

This is testing with getting 100 instances of code ownership for an unowned job.

Before, this took about 104 ms

Screenshot 2026-03-04 at 4 38 33 PM

After, this takes 1.41 ms

Screenshot 2026-03-04 at 4 39 44 PM

Test plan

  • Updated existing spec to verify nil results from Rust are now cached
  • CI passes

🤖 Generated with Claude Code

Cache nil results in TeamFinder.for_file when RustCodeOwners returns nil,
preventing repeated uncached calls to the Rust layer for unowned files.
Memoize Pathname.pwd and use string prefix stripping instead of
Pathname#relative_path_from for the common case, eliminating costly
Pathname allocations and cleanpath operations.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

1 participant