From 5e7e314a72e76c849d9fb371b7a78912d9b4a89c Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Thu, 15 Jan 2026 12:13:54 -0700 Subject: [PATCH 1/3] Profile and benchmark with autoload Co-Authored-By: Thiago Araujo --- Gemfile | 3 +++ Gemfile.lock | 9 +++++++++ benchmark/load.rb | 10 ++++++++++ benchmark/load_faker.rb | 7 +++++++ lib/faker.rb | 4 ++++ lib/faker/blockchain/bitcoin.rb | 4 +++- lib/faker/blockchain/tezos.rb | 4 +++- lib/faker/default/crypto.rb | 4 +++- 8 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 benchmark/load.rb create mode 100644 benchmark/load_faker.rb diff --git a/Gemfile b/Gemfile index ea1f62b89f..dcf98b835e 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,9 @@ gem 'test-unit', '3.7.7' gem 'timecop', '0.9.10' gem 'yard', '0.9.38' +gem 'vernier' +gem 'profile-viewer' + group :benchmark do gem 'benchmark' gem 'benchmark-ips' diff --git a/Gemfile.lock b/Gemfile.lock index d55bb44515..b1bf0ffd7b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -27,6 +27,7 @@ GEM lint_roller (1.1.0) method_source (1.1.0) minitest (5.27.0) + optparse (0.8.1) parallel (1.27.0) parser (3.3.10.0) ast (~> 2.4.1) @@ -36,6 +37,9 @@ GEM prettyprint prettyprint (0.2.0) prism (1.7.0) + profile-viewer (0.0.5) + optparse + webrick pry (0.16.0) coderay (~> 1.1) method_source (~> 1.0) @@ -89,10 +93,13 @@ GEM unicode-display_width (3.2.0) unicode-emoji (~> 4.1) unicode-emoji (4.2.0) + vernier (1.9.0) + webrick (1.9.2) yard (0.9.38) PLATFORMS arm64-darwin-22 + arm64-darwin-24 x86_64-linux DEPENDENCIES @@ -101,6 +108,7 @@ DEPENDENCIES faker! irb minitest (= 5.27.0) + profile-viewer pry (= 0.16.0) rake (= 13.3.1) rdoc @@ -110,6 +118,7 @@ DEPENDENCIES simplecov (= 0.22.0) test-unit (= 3.7.7) timecop (= 0.9.10) + vernier yard (= 0.9.38) BUNDLED WITH diff --git a/benchmark/load.rb b/benchmark/load.rb new file mode 100644 index 0000000000..5db618c4fa --- /dev/null +++ b/benchmark/load.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require 'benchmark/ips' + +Benchmark.ips do |x| + x.report('require openssl') { system('ruby load_faker.rb') } + x.report('autoload openssl') { system('AUTOLOAD=1 ruby load_faker.rb') } + + x.compare!(order: :baseline) +end diff --git a/benchmark/load_faker.rb b/benchmark/load_faker.rb new file mode 100644 index 0000000000..343368faf9 --- /dev/null +++ b/benchmark/load_faker.rb @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby + +if defined?(Faker) + raise "fake is already defined..." +end + +load("/Users/stefannibrasil/projects/faker/lib/faker.rb") diff --git a/lib/faker.rb b/lib/faker.rb index 4c2a487a46..26df5da04c 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -5,6 +5,10 @@ require 'psych' require 'i18n' +if ENV['AUTOLOAD'] == '1' + autoload(:OpenSSL, 'openssl') +end + Dir.glob(File.join(mydir, 'helpers', '*.rb')).each { |file| require file } I18n.load_path += Dir[File.join(mydir, 'locales', '**/*.yml')] diff --git a/lib/faker/blockchain/bitcoin.rb b/lib/faker/blockchain/bitcoin.rb index 5d1a47376f..5a23b1418e 100644 --- a/lib/faker/blockchain/bitcoin.rb +++ b/lib/faker/blockchain/bitcoin.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true -require 'openssl' +if ENV['AUTOLOAD'] != '1' + require 'openssl' +end require 'securerandom' module Faker diff --git a/lib/faker/blockchain/tezos.rb b/lib/faker/blockchain/tezos.rb index 94747269bd..226ee86f4c 100644 --- a/lib/faker/blockchain/tezos.rb +++ b/lib/faker/blockchain/tezos.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true -require 'openssl' +if ENV['AUTOLOAD'] != '1' + require 'openssl' +end require 'securerandom' module Faker diff --git a/lib/faker/default/crypto.rb b/lib/faker/default/crypto.rb index 670f6354a6..36d8f1598f 100644 --- a/lib/faker/default/crypto.rb +++ b/lib/faker/default/crypto.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true -require 'openssl' +if ENV['AUTOLOAD'] != '1' + require 'openssl' +end module Faker class Crypto < Base From a832b6096bd949131f6170b228987f1f2083797a Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Thu, 15 Jan 2026 12:18:23 -0700 Subject: [PATCH 2/3] Remove scripts Co-Authored-By: Thiago Araujo --- Gemfile | 3 --- Gemfile.lock | 8 -------- benchmark/load.rb | 10 ---------- benchmark/load_faker.rb | 7 ------- 4 files changed, 28 deletions(-) delete mode 100644 benchmark/load.rb delete mode 100644 benchmark/load_faker.rb diff --git a/Gemfile b/Gemfile index dcf98b835e..ea1f62b89f 100644 --- a/Gemfile +++ b/Gemfile @@ -18,9 +18,6 @@ gem 'test-unit', '3.7.7' gem 'timecop', '0.9.10' gem 'yard', '0.9.38' -gem 'vernier' -gem 'profile-viewer' - group :benchmark do gem 'benchmark' gem 'benchmark-ips' diff --git a/Gemfile.lock b/Gemfile.lock index b1bf0ffd7b..972129aba7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -27,7 +27,6 @@ GEM lint_roller (1.1.0) method_source (1.1.0) minitest (5.27.0) - optparse (0.8.1) parallel (1.27.0) parser (3.3.10.0) ast (~> 2.4.1) @@ -37,9 +36,6 @@ GEM prettyprint prettyprint (0.2.0) prism (1.7.0) - profile-viewer (0.0.5) - optparse - webrick pry (0.16.0) coderay (~> 1.1) method_source (~> 1.0) @@ -93,8 +89,6 @@ GEM unicode-display_width (3.2.0) unicode-emoji (~> 4.1) unicode-emoji (4.2.0) - vernier (1.9.0) - webrick (1.9.2) yard (0.9.38) PLATFORMS @@ -108,7 +102,6 @@ DEPENDENCIES faker! irb minitest (= 5.27.0) - profile-viewer pry (= 0.16.0) rake (= 13.3.1) rdoc @@ -118,7 +111,6 @@ DEPENDENCIES simplecov (= 0.22.0) test-unit (= 3.7.7) timecop (= 0.9.10) - vernier yard (= 0.9.38) BUNDLED WITH diff --git a/benchmark/load.rb b/benchmark/load.rb deleted file mode 100644 index 5db618c4fa..0000000000 --- a/benchmark/load.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -require 'benchmark/ips' - -Benchmark.ips do |x| - x.report('require openssl') { system('ruby load_faker.rb') } - x.report('autoload openssl') { system('AUTOLOAD=1 ruby load_faker.rb') } - - x.compare!(order: :baseline) -end diff --git a/benchmark/load_faker.rb b/benchmark/load_faker.rb deleted file mode 100644 index 343368faf9..0000000000 --- a/benchmark/load_faker.rb +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env ruby - -if defined?(Faker) - raise "fake is already defined..." -end - -load("/Users/stefannibrasil/projects/faker/lib/faker.rb") From 09c0289ebd4aaccedd53b2978a633d82592db375 Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Thu, 15 Jan 2026 12:19:12 -0700 Subject: [PATCH 3/3] Autoload OpenSSL Profiling laoding faker showed that by autoloading OpenSSL, we could improve Faker's loading time by about 17%. A lot of the time is spent on set_default_paths. Given that OpenSSL is only used in a very limited way and only by 3 generators, this would speed up the library for most users. Co-Authored-By: Thiago Araujo --- Gemfile.lock | 1 - lib/faker.rb | 4 +--- lib/faker/blockchain/bitcoin.rb | 3 --- lib/faker/blockchain/tezos.rb | 3 --- lib/faker/default/crypto.rb | 4 ---- 5 files changed, 1 insertion(+), 14 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 972129aba7..d55bb44515 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -93,7 +93,6 @@ GEM PLATFORMS arm64-darwin-22 - arm64-darwin-24 x86_64-linux DEPENDENCIES diff --git a/lib/faker.rb b/lib/faker.rb index 26df5da04c..ce19d536bd 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -5,9 +5,7 @@ require 'psych' require 'i18n' -if ENV['AUTOLOAD'] == '1' - autoload(:OpenSSL, 'openssl') -end +autoload(:OpenSSL, 'openssl') Dir.glob(File.join(mydir, 'helpers', '*.rb')).each { |file| require file } diff --git a/lib/faker/blockchain/bitcoin.rb b/lib/faker/blockchain/bitcoin.rb index 5a23b1418e..badd2f2bd0 100644 --- a/lib/faker/blockchain/bitcoin.rb +++ b/lib/faker/blockchain/bitcoin.rb @@ -1,8 +1,5 @@ # frozen_string_literal: true -if ENV['AUTOLOAD'] != '1' - require 'openssl' -end require 'securerandom' module Faker diff --git a/lib/faker/blockchain/tezos.rb b/lib/faker/blockchain/tezos.rb index 226ee86f4c..94cf94449e 100644 --- a/lib/faker/blockchain/tezos.rb +++ b/lib/faker/blockchain/tezos.rb @@ -1,8 +1,5 @@ # frozen_string_literal: true -if ENV['AUTOLOAD'] != '1' - require 'openssl' -end require 'securerandom' module Faker diff --git a/lib/faker/default/crypto.rb b/lib/faker/default/crypto.rb index 36d8f1598f..84efe18aae 100644 --- a/lib/faker/default/crypto.rb +++ b/lib/faker/default/crypto.rb @@ -1,9 +1,5 @@ # frozen_string_literal: true -if ENV['AUTOLOAD'] != '1' - require 'openssl' -end - module Faker class Crypto < Base class << self