Skip to content
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

performance: remove unnecessary i18n locale reload #2723

Merged
merged 2 commits into from
Apr 4, 2023

Conversation

codez
Copy link
Contributor

@codez codez commented Mar 1, 2023

Summary

I18n is reloaded whenever load_path is set, so an additional reload is not needed and only increases load time (because all locale files are reloaded twice 😞).

See https://github.com/ruby-i18n/i18n/blob/b4cd00d73234e369d713eb2607c729765bb3ec06/lib/i18n/config.rb#L135

Other Information

The steps described in the original issue that introduced the reload now work as expected, even without the reload.

I18n is reloaded whenever load_path are set, so an additional reload
is not needed and only increases load time.
@thdaraujo
Copy link
Contributor

thanks for submitting this @codez!

I think your suggestion makes sense. But we had so many problems related to this in the past that I would like to have a test to make sure there's no regression.

@thdaraujo
Copy link
Contributor

thdaraujo commented Mar 25, 2023

I tested main and I tested your change and they both seem fine. But I couldn't really reproduce the original issue anymore, so not sure. Here's what I've tried:

# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem 'faker', :git => 'https://github.com/codez/faker.git', :branch => 'remove-unnecessary-i18n-reload', require: false
  # gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main', require: false

  gem "minitest"
end

require "minitest/autorun"
require "i18n"

class BugTest < Minitest::Test
  def test_faker
    refute I18n.backend.initialized?

    assert require 'faker'

    puts Faker::Name.name
    assert Faker::Name.name
  end

  def test_faker_forking
    refute I18n.backend.initialized?
    
    pid = fork do
      assert require 'faker'
      assert Faker::Name.name
    end
    
    _, status = Process.waitpid2(pid)
    assert status.success?, "Failed in forked process"
  end
end

Maybe someone has a better idea and is able to reproduce the original issue so we can know for sure.

@stefannibrasil
Copy link
Contributor

hi @codez Thanks for your contribution! Like @thdaraujo mentioned, it's hard to keep changing this without being sure if it will still work, or why it wasn't working in the first place.

Could you please provide more reproduction steps and automated tests to help with the reproduction as well? Thanks!

@codez
Copy link
Contributor Author

codez commented Mar 29, 2023

I added a test that loads faker in a subshell and asserts that the original issue now works.

Because the reloading is actually done in the i18n gem, this test also succeeds on the current main branch. The original issue is not reproducible with current versions of i18n.

Copy link
Contributor

@thdaraujo thdaraujo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding tests! Left a suggestion on how to handle the exit status.

test/test_i18n_reload.rb Outdated Show resolved Hide resolved
thdaraujo
thdaraujo previously approved these changes Apr 4, 2023
Copy link
Contributor

@thdaraujo thdaraujo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is not passing

@thdaraujo thdaraujo dismissed their stale review April 4, 2023 01:51

we'll need to fix the test to pass CI

@codez codez force-pushed the remove-unnecessary-i18n-reload branch from f566248 to bfb9ee4 Compare April 4, 2023 06:40
@codez
Copy link
Contributor Author

codez commented Apr 4, 2023

I adjusted the test, it should hopefully pass now.

Copy link
Contributor

@thdaraujo thdaraujo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for adding tests!

Copy link
Member

@marcelobarreto marcelobarreto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thdaraujo thdaraujo changed the title Remove unnecessary i18n reload performance: remove unnecessary i18n locale reload Apr 4, 2023
@thdaraujo thdaraujo merged commit 03c099a into faker-ruby:main Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants