From 756767aeaf3ed84d818ed0e129a07a247e5e3a12 Mon Sep 17 00:00:00 2001 From: Alex Taujenis Date: Fri, 19 Apr 2024 00:47:56 -0600 Subject: [PATCH 1/4] Cache translations to reduce file read and parse operations --- lib/faker.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/faker.rb b/lib/faker.rb index ad286e67f0..000ef27b76 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -44,6 +44,7 @@ class Base ULetters = Array('A'..'Z') LLetters = Array('a'..'z') Letters = ULetters + LLetters + @@translate_cache = {} class << self attr_reader :flexible_key @@ -161,8 +162,9 @@ def parse(key) # locale is specified def translate(*args, **opts) opts[:locale] ||= Faker::Config.locale + translate_key = args.to_s + opts.sort.join opts[:raise] = true - I18n.translate(*args, **opts) + @@translate_cache[translate_key] ||= I18n.translate(*args, **opts) rescue I18n::MissingTranslationData opts[:locale] = :en From 379b0b3ac1047009d921e2f03e4a1fbf8e252e73 Mon Sep 17 00:00:00 2001 From: Alex Taujenis Date: Fri, 19 Apr 2024 01:51:36 -0600 Subject: [PATCH 2/4] Disable rubocop Style/ClassVars --- lib/faker.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/faker.rb b/lib/faker.rb index 000ef27b76..3468ca2e8f 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +# rubocop:disable Style/ClassVars + mydir = __dir__ require 'psych' From 7233dfa59cbc5000c01276d60cc74131cd4a159e Mon Sep 17 00:00:00 2001 From: Alex Taujenis Date: Fri, 19 Apr 2024 01:54:18 -0600 Subject: [PATCH 3/4] Enable rubocop Style/ClassVars --- lib/faker.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/faker.rb b/lib/faker.rb index 3468ca2e8f..9b84ad1fb1 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -# rubocop:disable Style/ClassVars - mydir = __dir__ require 'psych' @@ -41,6 +39,7 @@ def random end end + # rubocop:disable Style/ClassVars class Base Numbers = Array(0..9) ULetters = Array('A'..'Z') @@ -272,6 +271,7 @@ def disable_enforce_available_locales end end end + # rubocop:enable Style/ClassVars end # require faker objects From 559859aa3c83d833a03729090d1c1341fa2b0fd6 Mon Sep 17 00:00:00 2001 From: Alex Taujenis Date: Wed, 8 May 2024 10:38:56 -0600 Subject: [PATCH 4/4] Convert the translate cache to a thread safe variable --- lib/faker.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/faker.rb b/lib/faker.rb index 9b84ad1fb1..85f5a698dc 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -45,7 +45,6 @@ class Base ULetters = Array('A'..'Z') LLetters = Array('a'..'z') Letters = ULetters + LLetters - @@translate_cache = {} class << self attr_reader :flexible_key @@ -159,13 +158,17 @@ def parse(key) parts.any? ? parts.join : numerify(fetched) end + def translate_cache + Thread.current[:faker_translate_cache] ||= {} + end + # Call I18n.translate with our configured locale if no # locale is specified def translate(*args, **opts) opts[:locale] ||= Faker::Config.locale translate_key = args.to_s + opts.sort.join opts[:raise] = true - @@translate_cache[translate_key] ||= I18n.translate(*args, **opts) + translate_cache[translate_key] ||= I18n.translate(*args, **opts) rescue I18n::MissingTranslationData opts[:locale] = :en