Skip to content

Speedup require and first call #2719

Open
@stoivo

Description

@stoivo

Hi, I use faker and I noticed that some parts are slower then what I would have liked. I wonder if you have had some ideas for what you would like to do to make it faster to load and use.

I did a bit of testing locally. I an running on a bit older mac(MacBood Pro 2014). Below you can see how I tested some parts.

Most of the time is spent the first time we call Faker::Coin.flip 1332ms, I confident this is because we have many different yaml files which are loaded into I18n. I havn't tested but maybe it is faster if it one file instead of several?

The first think I tested what if I require less files then you require faker. I think we can save 30% (100ms in my case) of require time by using zeitwerk to load all the different extra files. Would you accept a merge requies switching to zeitwerk?

Script

t = Time.now.to_f * 1000
require_relative "lib/faker"
tt = Time.now.to_f * 1000
puts "Time: #{tt - t}ms Time of require faker"

10.times do
  t = Time.now.to_f * 1000
  fake = Faker::Coin.flip
  tt = Time.now.to_f * 1000
  puts "Time: #{tt - t}ms Faker::Coin.flip (#{fake})"
end

before:
Time: 214.383056640625ms Time of require faker
Time: 1423.72900390625ms Faker::Coin.flip (Heads)
Time: 0.072998046875ms Faker::Coin.flip (Tails)
Time: 0.08984375ms Faker::Coin.flip (Heads)
Time: 0.281005859375ms Faker::Coin.flip (Heads)
Time: 0.237060546875ms Faker::Coin.flip (Heads)
Time: 2.384765625ms Faker::Coin.flip (Heads)
Time: 0.08203125ms Faker::Coin.flip (Heads)
Time: 0.07177734375ms Faker::Coin.flip (Heads)
Time: 0.083984375ms Faker::Coin.flip (Tails)
Time: 0.066162109375ms Faker::Coin.flip (Heads)

replaced
Dir.glob(File.join(mydir, 'faker', '/**/*.rb')).sort.each { |file| require file }

with
require "/Users/simon/Downloads/faker/lib/faker/default/coin.rb"
require "/Users/simon/Downloads/faker/lib/faker/version.rb"

after:
Time: 310.723876953125ms Time of require faker
Time: 1332.678466796875ms Faker::Coin.flip (Tails)
Time: 0.044189453125ms Faker::Coin.flip (Heads)
Time: 0.08154296875ms Faker::Coin.flip (Heads)
Time: 0.064208984375ms Faker::Coin.flip (Heads)
Time: 0.06005859375ms Faker::Coin.flip (Tails)
Time: 0.100830078125ms Faker::Coin.flip (Tails)
Time: 0.09375ms Faker::Coin.flip (Tails)
Time: 0.075927734375ms Faker::Coin.flip (Tails)
Time: 0.066162109375ms Faker::Coin.flip (Heads)
Time: 0.06982421875ms Faker::Coin.flip (Heads)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions