Skip to content

Commit f791780

Browse files
committed
mkmf: split try_link0 into separate compile and link steps
When `mkmf.rb` checks for functions via `have_func`, it compiles and links a test program in a single clang invocation. On macOS, passing a source file together with `-lruby.4.1-static` (28MB static archive) to clang in one command triggers a ~1.4s overhead per invocation. Splitting into two steps (compile `.c` to `.o`, then link `.o`) reduces this to ~0.14s — a 10x improvement per check. This dramatically speeds up extension configuration: - `ext/io/console`: 12.1s → 1.8s (6.7x faster) - `ext/openssl`: 8.3s → 3.0s (2.8x faster) - `ext/json`: 6.6s → 1.4s (4.7x faster) - `ext/strscan`: 5.0s → 0.6s (8.3x faster) Overall clean build: 52s → 44s (16% faster)
1 parent 661344b commit f791780

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

lib/mkmf.rb

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,6 @@ def link_config(ldflags, opt="", libpath=$DEFLIBPATH|$LIBPATH)
555555
conf
556556
end
557557

558-
def link_command(ldflags, *opts)
559-
conf = link_config(ldflags, *opts)
560-
RbConfig::expand(TRY_LINK.dup, conf)
561-
end
562-
563558
def cc_config(opt="")
564559
conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote, 'srcdir' => $srcdir.quote,
565560
'arch_hdrdir' => $arch_hdrdir.quote,
@@ -606,20 +601,24 @@ def with_werror(opt, opts = nil)
606601

607602
def try_link0(src, opt = "", ldflags: "", **opts, &b) # :nodoc:
608603
exe = CONFTEST+$EXEEXT
609-
cmd = link_command(ldflags, opt)
604+
conf = link_config(ldflags, opt)
605+
conf['src'] = "#{CONFTEST}.#{$OBJEXT}"
606+
ld = RbConfig::expand(TRY_LINK.dup, conf)
610607
if $universal
611608
require 'tmpdir'
612609
Dir.mktmpdir("mkmf_", oldtmpdir = ENV["TMPDIR"]) do |tmpdir|
613610
begin
614611
ENV["TMPDIR"] = tmpdir
615-
try_do(src, cmd, **opts, &b)
612+
try_do(src, cc_command(opt), **opts, &b)
616613
ensure
617614
ENV["TMPDIR"] = oldtmpdir
618615
end
619616
end
620617
else
621-
try_do(src, cmd, **opts, &b)
622-
end and File.executable?(exe) or return nil
618+
try_do(src, cc_command(opt), **opts, &b)
619+
end or return nil
620+
xsystem(ld, **opts) or return nil
621+
return nil unless File.executable?(exe)
623622
exe
624623
ensure
625624
MakeMakefile.rm_rf(*Dir["#{CONFTEST}*"]-[exe])
@@ -3037,11 +3036,6 @@ def cc_command(opt="")
30373036
conf)
30383037
end
30393038

3040-
def link_command(ldflags, *opts)
3041-
conf = link_config(ldflags, *opts)
3042-
RbConfig::expand(TRY_LINK_CXX.dup, conf)
3043-
end
3044-
30453039
# :startdoc:
30463040
end
30473041

0 commit comments

Comments
 (0)