Skip to content

Parse each POT file once per merge - #455

Open
oliver-kriska wants to merge 2 commits into
elixir-gettext:mainfrom
oliver-kriska:parse-pot-once
Open

oliver-kriska wants to merge 2 commits into
elixir-gettext:mainfrom
oliver-kriska:parse-pot-once

Conversation

@oliver-kriska

@oliver-kriska oliver-kriska commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • mix gettext.merge DIR parsed every POT file once per locale. Now each POT file is parsed once and all locales reuse the parsed template.
  • On a production catalog (16 locales, 176 PO files), a merge with nothing to change and --no-fuzzy goes from 1.24 s to 0.76 s for the whole command at 8 schedulers, and its CPU time from 7.0 s to 3.2 s.
  • The PO files and the task output are byte-identical to main in every run below.

Changes

merge_messages_dir/3 builds the list of locales ([locale] for --locale, every locale directory otherwise) and calls one function. That function parses the POT files, runs the {locale, pot} stream from #453 and warns about PO files without a POT, so merge_dirs/5 and the two locale-dir helpers go away. merge_file, merge_or_create, merge_files and the internal Gettext.Merger.new_po_file/4 take the parsed POT instead of its path.

The POT files are parsed in parallel. With a plain Enum.map, --locale got slower on a catalog with several large domains (0.56 s → 1.09 s on the synthetic 8 × 8 catalog below), because those parses used to run inside the parallel tasks.

Most of the saving is garbage collection, not parsing. Each task used to parse a large POT while the parsed PO was already on its heap. With one scheduler, the no-change merge spends 1.78 s in GC on dirty CPU schedulers before and 0.45 s after (microstate accounting), and the number of collections drops from 11,736 to 5,551.

Measurements

Whole command, Mix startup included. Elixir 1.20.4 / OTP 29, same catalog and command in both columns (main measured at the commit of #453, before it was squashed), medians of 8 alternating runs (4 for the fuzzy row). The machine was shared with other jobs, so CPU time is the steadier number.

Case Schedulers main wall / CPU This PR wall / CPU
Production catalog, --no-fuzzy, nothing to change 4 1.64 s / 5.6 s 0.88 s / 2.5 s
same 8 1.24 s / 7.0 s 0.76 s / 3.2 s
same 14 1.33 s / 8.3 s 0.76 s / 4.2 s
Production catalog, --no-fuzzy, after a real extraction commit 8 1.10 s / 4.7 s 0.87 s / 3.1 s
Production catalog, fuzzy on, same commit 8 7.21 s / 35.9 s 5.77 s / 19.7 s
Production catalog, --locale, --no-fuzzy 8 0.57 s / 0.9 s 0.48 s / 0.8 s
Synthetic: 32 locales × 1 domain × 10,000 messages, --no-fuzzy 8 3.66 s / 25.7 s 2.18 s / 14.2 s
Synthetic: 8 locales × 8 domains × 4,000 messages, --no-fuzzy 8 1.95 s / 13.8 s 1.37 s / 8.9 s
Synthetic 8 × 8, --locale, --no-fuzzy 8 0.56 s / 2.6 s 0.56 s / 2.3 s

Peak memory at 8 schedulers: 335 → 280 MiB for the no-change merge and 299 → 308 MiB with fuzzy on, on the production catalog. It is higher on the synthetic catalogs (548 → 589 MiB and 303 → 363 MiB), because the parsed POT files now stay alive for the whole run.

Behavior checks

  • Output. PO files and the sorted task output ("Wrote" lines with their statistics, warnings) have the same hashes as on main in every run of every row.
  • Tests. mix test --warnings-as-errors passes on Elixir 1.20.4 / OTP 29 and on 1.16.3 / OTP 24. The only test change is the unit test of new_po_file/4, which now parses the POT first.
  • Two-file form. Same speed (3.40 s vs 3.41 s with fuzzy on). It now parses the reference file before the PO file, which only shows if both are broken.
  • A POT file with a syntax error. The command still fails with the same Expo.PO.SyntaxError, but now before any PO file is written and with one crash report. Before, there was one report per locale and PO files of other domains could already be rewritten. No test covers either behavior; I can add one if you want it pinned.

🤖 Generated with Claude Code

mix gettext.merge parsed every POT file once per locale. Parse the POT files once, in parallel, and pass the parsed template to the merge of each locale.

The --locale and the all-locales paths now share one function, and Gettext.Merger.new_po_file/4 takes the parsed POT instead of its path.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@whatyouhide

Copy link
Copy Markdown
Contributor

@oliver-kriska I've merged #453. Any work you need to do here before this becomes ready for review?

@oliver-kriska
oliver-kriska marked this pull request as ready for review December 3, 2026 09:50
@oliver-kriska

Copy link
Copy Markdown
Contributor Author

Rebased onto main and marked ready. Nothing else from my side.

pot_dir
|> Path.join("*.pot")
|> Path.wildcard()
|> Task.async_stream(&{&1, PO.parse_file!(&1)}, timeout: :infinity)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If locales is an empty list, we now still parse all the POT files. That's slightly inefficient but more importantly introduced a potential regression where a malformed POT file + locales == [] now raises an error (it did nothing before).

Can we keep previous behavior here? We can always fix this if we think the behavior is not correct, but I'd rather we do that in another PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Done in bd9b635: with an empty locales list no POT file is parsed, as on main. I added a test with a malformed POT file and no locale directories; it raised before this commit.

Without --locale and without locale directories nothing is merged, so
the POT files are not read, as before this branch. A malformed POT file
in that case does not raise.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

3 participants