Feature #13378

Eliminate 4 of 8 syscalls when requiring file by absolute path

Added by burke (Burke Libbey) almost 4 years ago. Updated over 3 years ago.

Target version:


Don't open file twice when specified by absolute path.

When invoking require '/a.rb' (i.e. via an absolute path), ruby generates this sequence of syscalls:

open    /a.rb
fstat64 /a.rb
close   /a.rb
open    /a.rb
fstat64 /a.rb
fstat64 /a.rb
read    /a.rb
close   /a.rb

It is apparent that the only inherently necessary members of this sequence are:

open    /a.rb
fstat64 /a.rb
read    /a.rb
close   /a.rb

(the fstat64 isn't obviously necessary, but it does serve a purpose and probably shouldn't be removed).

The first open/fstat64/close is used to check whether the file is loadable. This is important when scanning the $LOAD_PATH, since it is used to determine when a file has been found. However, when we've already unambiguously identified a file before invoking require, this serves no inherent purpose, since we can move whatever work is happening as a result of that fstat64 into the second open/close sequence.

This change bypasses the first open/fstat64/close in the case of an absolute path to require. It also removes one of the doubled-up fstat64 calls later in the sequence. As a result, the number of syscalls to require a file changes:

  • From 8 to 4 when specified by absolute path;
  • From 5+3n to 4+3n otherwise (where n is the number of $LOAD_PATH items scanned).

In future work, it would be possible to re-use the file descriptor opened while searching the $LOAD_PATH without the close/open sequence, but this would cause some ugly layering issues.

We intend to use this in conjunction with something like, which pre-resolves required features to absolute paths before calling require. This change reduces our total number of filesystem accesses by 13% during application boot.

Various notes and rationale at


0001-reduce-syscalls-on-require.patch (7.56 KB) 0001-reduce-syscalls-on-require.patch burke (Burke Libbey), 03/28/2017 07:31 PM
0001-reduce-syscalls-on-require-fixed.patch (6.94 KB) 0001-reduce-syscalls-on-require-fixed.patch burke (Burke Libbey), 03/28/2017 08:29 PM
0001-reduce-syscalls-on-require-v2.patch (6.44 KB) 0001-reduce-syscalls-on-require-v2.patch burke (Burke Libbey), 05/29/2017 06:41 PM

Also available in: Atom PDF