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

fix: speed up Lang.pm initialization, save 2 seconds x 100 tests #9197

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 42 additions & 13 deletions lib/ProductOpener/Lang.pm
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ sub lang_in_other_lc ($target_lc, $stringid) {

$log->info("initialize", {data_root => $data_root}) if $log->is_info();

# Load stored %Lang from Lang.sto
# Load stored %Lang from Lang.sto and Lang_tags.sto

my $path = "$data_root/data/Lang.${server_domain}.sto";
if (-e $path) {
Expand All @@ -275,27 +275,49 @@ if (-e $path) {
}

$log->info("Loaded languages", {langs => (scalar @Langs)}) if $log->is_info();
sleep(1) if $log->is_info();
}
else {
$log->warn("Language translation file does not exist, \%Lang will be empty. Run scripts/build_lang.pm to fix this.",
{path => $path})
if $log->is_warn();
}

# Tags types to path components in URLS: in ascii, lowercase, unaccented,
# transliterated (in Roman characters)
#
# Note: a lot of plurals are currently missing below, commented-out are
# the singulars that need to be changed to plurals
my ($tag_type_singular_ref, $tag_type_plural_ref)
= ProductOpener::I18N::split_tags(ProductOpener::I18N::read_po_files("$data_root/po/tags/"));
%tag_type_singular = %{$tag_type_singular_ref};
%tag_type_plural = %{$tag_type_plural_ref};
$path = "$data_root/data/Lang_tags.${server_domain}.sto";
Copy link
Member

Choose a reason for hiding this comment

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

This is more cache data, could you move it to the cache folder.

if (-e $path) {

$log->info("Loading tag types <=> singular and plural translated paths", {path => $path}) if $log->is_info();
my $tag_type_data_ref = retrieve($path);
$log->info("Loaded tag types <=> singular and plural translated paths", {path => $path}) if $log->is_info();

%tag_type_singular = %{$tag_type_data_ref->{tag_type_singular}};
%tag_type_plural = %{$tag_type_data_ref->{tag_type_plural}};
%tag_type_from_singular = %{$tag_type_data_ref->{tag_type_from_singular}};
%tag_type_from_plural = %{$tag_type_data_ref->{tag_type_from_plural}};
}
else {
$log->warn("Language translation file for tags does not exist. Run scripts/build_lang.pm to fix this.",
{path => $path})
if $log->is_warn();
}

# Taxonomies that can have debug, prev, and next versions
# (older feature to generate tags using multiple versions of a taxonomy, currently not used)
my @debug_taxonomies = ("categories", "labels", "additives");

{
# Build hashes to map a translated tag type (e.g. "catégorie") in singular or plural to the tag type (e.g. "categories")

sub build_lang_tags() {

# Tags types to path components in URLS: in ascii, lowercase, unaccented,
# transliterated (in Roman characters)
#
# Note: a lot of plurals are currently missing below, commented-out are
# the singulars that need to be changed to plurals
my ($tag_type_singular_ref, $tag_type_plural_ref)
= ProductOpener::I18N::split_tags(ProductOpener::I18N::read_po_files("$data_root/po/tags/"));
%tag_type_singular = %{$tag_type_singular_ref};
%tag_type_plural = %{$tag_type_plural_ref};

foreach my $l (@Langs) {

foreach my $taxonomy (@debug_taxonomies) {
Expand Down Expand Up @@ -355,7 +377,14 @@ my @debug_taxonomies = ("categories", "labels", "additives");
}

}

return (
{
tag_type_singular => \%tag_type_singular,
tag_type_plural => \%tag_type_plural,
tag_type_from_singular => \%tag_type_from_singular,
tag_type_from_plural => \%tag_type_from_plural
}
);
}

# initialize languages values:
Expand Down
2 changes: 2 additions & 0 deletions scripts/build_lang.pl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
# Tags.pm builds the %Languages hash of languages from the languages taxonomy

ProductOpener::Lang::build_lang(\%Languages);
my $tags_ref = ProductOpener::Lang::build_lang_tags();

# use $server_domain in part of the name so that we have different files
# when 2 instances of Product Opener share the same $data_root
Expand All @@ -48,6 +49,7 @@
mkdir("$data_root/data", 0755) or die("Could not create target directory $data_root/data : $!\n");
}
store("$data_root/data/Lang.${server_domain}.sto", \%Lang);
Copy link
Member

Choose a reason for hiding this comment

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

here also it would be better to have it in a cache folder.

store("$data_root/data/Lang_tags.${server_domain}.sto", $tags_ref);

# Generate JSON files for JavaScript I18N
ProductOpener::Lang::build_json();
Expand Down
Loading