-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Speedup require and first call #2719
Comments
I looked into why is spends 1400ms to get the first. It is because it takes so so much time parse yaml I created this script to check. # frozen_string_literal: true
def it_takes(string)
t = Time.now.to_f * 1000
r = yield
tt = Time.now.to_f * 1000
print "#{('%.4f' % (tt - t) ).to_s.rjust(20)}ms Done with #{string}\n"
r
end
it_takes "require psych" do
require 'psych'
require 'yaml'
end
Dir[File.join(__dir__, 'locales', '**/*.yml')]
.each do |file|
it_takes "file #{file} " do
YAML.unsafe_load_file(file, symbolize_names: true, freeze: true)
end
end
it_takes "all " do
Dir[File.join(__dir__, 'locales', '**/*.yml')]
.each do |file|
YAML.unsafe_load_file(file, symbolize_names: true, freeze: true)
end
end results
Knowing this I'm not sure it is worth spending any time speeding up require by 100ms(of a total of 300ms) if we will spend 1400ms parsing yaml. The only way I see this becoming faster is to remove i18n and lazy load those yaml files we need. For that, to work we need to restructure the yaml files so we know whay they contain without parsing it I can spend some time working on this, but I want to know if it is a chance that it will be merged, and how you would like to have it. |
You could also argue that it is better not to pollute I18n with fakers translations |
Same issue here.
|
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
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)
The text was updated successfully, but these errors were encountered: