webhippie / gettext_i18n_rails_js

Extends gettext_i18n_rails making your .PO files available to client side javascript as JSON
https://github.com/webhippie/gettext_i18n_rails_js
MIT License
28 stars 35 forks source link

Autoload the parsers on first use #66

Closed jrafanie closed 6 months ago

jrafanie commented 6 months ago

Adding this gem to an application should probably load the gettext parsing utilities when it's being used at runtime. Currently, the gem entry lib file requires parser which eager loads these parsers and consumes time and memory when the developer or system may not need them.

before this PR

% for i in $(seq 1 10); do; ruby in_line_test.rb |grep MB; done
1.828125 MB
1.9375 MB
1.796875 MB
1.546875 MB
1.890625 MB
1.546875 MB
1.8125 MB
2.40625 MB
1.9375 MB
1.6875 MB

after this PR

% for i in $(seq 1 10); do; ruby in_line_test.rb |grep MB; done
0.90625 MB
0.53125 MB
0.984375 MB
1.1875 MB
0.46875 MB
0.390625 MB
0.453125 MB
0.96875 MB
0.546875 MB
0.5 MB

Using this script:

# frozen_string_literal: true

require "bundler/inline"

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

  gem "rails"
  # If you want to test against edge Rails replace the previous line with this:
  # gem "rails", github: "rails/rails", branch: "main"

  gem 'gettext_i18n_rails'
  gem 'gettext', '>=3.0.2', :require => false
  # gem 'ruby_parser', :require => false, :group => :development
  # gem "gettext_i18n_rails_js", "~> 2.0", :require => false, :git => "https://github.com/jrafanie/gettext_i18n_rails_js.git", :branch => "autoload_parsers_on_first_use"
  gem "gettext_i18n_rails_js", "~> 2.0", :require => false
  gem 'derailed_benchmarks', group: :development
end

require "active_support"
require "active_support/core_ext/object/blank"
require "minitest/autorun"

require 'get_process_mem'
mem    = GetProcessMem.new
before = mem.mb

require 'gettext_i18n_rails_js'

after  = mem.mb
puts "#{after - before} MB"
tboerger commented 6 months ago

Fails for ruby 2.7, I'm thinking about dropping all ruby versions which are already eol, like everything below 3.1.

tboerger commented 6 months ago

Or at least below 3.0

jrafanie commented 6 months ago

@tboerger It looks like https://github.com/webhippie/gettext_i18n_rails_js/pull/65 is also failing on 2.7 with rails 7 for the same io console reason:

bundle exec rspec
  shell: /usr/bin/bash -e {0}
Gem::LoadError: You have already activated io-console 0.5.6, but your Gemfile requires io-console 0.7.2. Since io-console is a default gem, you can either remove your dependency on it or try updating to a newer version of bundler that supports io-console as a default gem.
  /opt/hostedtoolcache/Ruby/2.7.8/x64/lib/ruby/gems/2.7.0/gems/bundler-1.17.[3](https://github.com/webhippie/gettext_i18n_rails_js/actions/runs/9038144306/job/24850487097?pr=66#step:4:3)/lib/bundler/runtime.rb:319:in `check_for_activated_spec!'
bundler: failed to load command: rspec (/home/runner/work/gettext_i18n_rails_js/gettext_i18n_rails_js/vendor/bundle/ruby/2.7.0/bin/rspec)
  /opt/hostedtoolcache/Ruby/2.7.8/x6[4](https://github.com/webhippie/gettext_i18n_rails_js/actions/runs/9038144306/job/24850487097?pr=66#step:4:5)/lib/ruby/gems/2.7.0/gems/bundler-1.17.3/lib/bundler/runtime.rb:31:in `block in setup'
  /home/runner/work/gettext_i18n_rails_js/gettext_i18n_rails_js/vendor/bundle/ruby/2.7.0/gems/forwardable-1.3.3/lib/forwardable.rb:240:in `each'
  /home/runner/work/gettext_i18n_rails_js/gettext_i18n_rails_js/vendor/bundle/ruby/2.7.0/gems/forwardable-1.3.3/lib/forwardable.rb:240:in `each'
  /opt/hostedtoolcache/Ruby/2.7.8/x[6](https://github.com/webhippie/gettext_i18n_rails_js/actions/runs/9038144306/job/24850487097?pr=66#step:4:7)4/lib/ruby/gems/2.7.0/gems/bundler-1.17.3/lib/bundler/runtime.rb:26:in `map'
  /opt/hostedtoolcache/Ruby/2.7.8/x64/lib/ruby/gems/2.7.0/gems/bundler-1.1[7](https://github.com/webhippie/gettext_i18n_rails_js/actions/runs/9038144306/job/24850487097?pr=66#step:4:8).3/lib/bundler/runtime.rb:26:in `setup'
  /opt/hostedtoolcache/Ruby/2.7.8/x64/lib/ruby/gems/2.7.0/gems/bundler-1.17.3/lib/bundler.rb:107:in `setup'
  /opt/hostedtoolcache/Ruby/2.7.[8](https://github.com/webhippie/gettext_i18n_rails_js/actions/runs/9038144306/job/24850487097?pr=66#step:4:9)/x64/lib/ruby/gems/2.7.0/gems/bundler-1.17.3/lib/bundler/setup.rb:20:in `<top (required)>'
  /opt/hostedtoolcache/Ruby/2.7.8/x64/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
  /opt/hostedtoolcache/Ruby/2.7.8/x64/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'

I'd hate to drop 2.7 since it's supported with rails 7.0 unless it's truly required.

I can't recreate it locally with ruby 2.7/bundler 1.17/rails 7.0, so I'm wondering if it's specific to the CI environment.

My first thought is loosening the bundler 1.17 requirement.

Any ideas?

jrafanie commented 6 months ago

It works for me locally. I'll close and reopen to try it again but it expect it to fail similarly to #65

% ruby -v
ruby 2.7.8p225 (2023-03-30 revision 1f4d455848) [arm64-darwin22]
% RAILS_VERSION="7.0.0" bundle _1.17.3_ --version
Bundler version 1.17.3
% RAILS_VERSION="7.0.0" bundle _1.17.3_ show rails
/Users/joerafaniello/.gem/ruby/2.7.8/gems/rails-7.0.8.1
% RAILS_VERSION="7.0.0" bundle _1.17.3_ exec rspec
...
% echo $?
0
jrafanie commented 6 months ago

Ok, I opened https://github.com/webhippie/gettext_i18n_rails_js/pull/67 to see if we can get it to pass with ruby 2.7, rails 6.1, and bundler 2.4 as the minimums for testing.

tboerger commented 6 months ago

I have published this change as v2.1.0 now, thanks for your effort.

jrafanie commented 6 months ago

I have published this change as v2.1.0 now, thanks for your effort.

Thank you! I opened #69 for a minor typo. Have a great day.