Skip to content

Commit 34d4d22

Browse files
committed
Load the RSS measurement code prior to the first benchmark iteration.
On macOS, RSS is measured via Fiddle and loading it the way ruby-bench does (force-activation) via Bundler, loads code that redefines methods like `==` and `hash`, leading to cache invalidation that adversely affects benchmark runs.
1 parent ad63bf8 commit 34d4d22

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

‎lib/harness.rb‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,25 +131,27 @@ def load_fiddle
131131
RUSAGE_INFO_V0 = 0
132132
RI_RESIDENT_SIZE_OFFSET = 16 + 6 * 8
133133

134-
# Memoized on a module, not on main: the Ractor harness freezes main.
134+
# On macOS, `get_rss` reads the RSS through Fiddle via `load_fiddle`. We load it when the harness
135+
# loads, rather than on the first RSS sample after a benchmark iteration. That's also before the
136+
# benchmark calls `use_gemfile`, so `load_fiddle` can activate the fiddle gem directly. After
137+
# `Bundler.setup`, it has to force-activate the gem through Bundler, which loads Bundler code that
138+
# can invalidate JIT-compiled code and adversely impact benchmark runs.
139+
#
140+
# Stored on a module, not on main: the Ractor harness freezes main.
135141
module ProcPidRusage
136-
@resolved = false
137-
138-
def self.fn
139-
return @fn if @resolved
140-
141-
@resolved = true
142-
@fn = begin
142+
if is_macos
143+
begin
143144
load_fiddle
144-
Fiddle::Function.new(
145+
@fn = Fiddle::Function.new(
145146
Fiddle.dlopen(nil)['proc_pid_rusage'],
146147
[Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_VOIDP],
147148
Fiddle::TYPE_INT,
148149
)
149150
rescue LoadError, Fiddle::DLError, NameError
150-
nil
151151
end
152152
end
153+
154+
def self.fn = @fn
153155
end
154156

155157
# The current RSS in bytes on macOS, read from libproc. Unlike ps this does not

0 commit comments

Comments
 (0)