This can be reproduced with the following benchmark script:
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'benchmark-ips'
gem 'bootstrap', '~> 5.3'
gem 'sassc'
end
BOOTSTRAP = Gem::Specification.find_by_name('bootstrap').gem_dir + '/assets/stylesheets/_bootstrap.scss'
require 'benchmark/ips'
require 'sassc'
Benchmark.ips do |x|
x.time = 15
x.report("CXXFLAGS=#{RbConfig::CONFIG["CXXFLAGS"].inspect}") do
SassC::Engine.new(File.read(BOOTSTRAP), { style: :compressed, filename: BOOTSTRAP }).render
end
x.compare!
end
Begin with the default on ruby:3.3 docker container with gcc 12.2.0.
Please make sure to gem uninstall -a sassc
before the test to start clean.
ruby 3.3.3 (2024-06-12 revision f1c7b6f435) [aarch64-linux]
Warming up --------------------------------------
CXXFLAGS="" 1.000 i/100ms
Calculating -------------------------------------
CXXFLAGS="" 0.765 (± 0.0%) i/s - 12.000 in 15.684859s
Now edit $RUBYARCHDIR/rbconfig.rb
to manually patch the default CXXFLAGS as the following:
CONFIG["CXXFLAGS"] = "$(optflags)"
Note: This file is read-only by default, and may need to be force overwritten with depends on your editor.
Now we can run the same test again.
Please make sure to gem uninstall -a sassc
before the test to start clean.
You can see this is about 7 times faster with $optflags
on linux gcc.
ruby 3.3.3 (2024-06-12 revision f1c7b6f435) [aarch64-linux]
Warming up --------------------------------------
CXXFLAGS="-O3 -fno-fast-math"
1.000 i/100ms
Calculating -------------------------------------
CXXFLAGS="-O3 -fno-fast-math"
5.483 (± 0.0%) i/s - 83.000 in 15.139519s
I also tested on mac with clang 15 using homebrew ruby 3.3 out of box:
ruby 3.3.3 (2024-06-12 revision f1c7b6f435) [arm64-darwin23]
Warming up --------------------------------------
CXXFLAGS="-fdeclspec"
1.000 i/100ms
Calculating -------------------------------------
CXXFLAGS="-fdeclspec"
0.517 (± 0.0%) i/s - 8.000 in 15.475395s
It's more than 10 times faster with $optflags
with clang 15 on mac:.
ruby 3.3.3 (2024-06-12 revision f1c7b6f435) [arm64-darwin23]
Warming up --------------------------------------
CXXFLAGS="-fdeclspec -O3 -fno-fast-math"
1.000 i/100ms
Calculating -------------------------------------
CXXFLAGS="-fdeclspec -O3 -fno-fast-math"
5.601 (± 0.0%) i/s - 85.000 in 15.177061s