Skip to content

Compile translations as one module per locale - #802

Open
pmargreff wants to merge 1 commit into
bitwalker:mainfrom
pmargreff:split-gettext-by-locale
Open

pmargreff wants to merge 1 commit into
bitwalker:mainfrom
pmargreff:split-gettext-by-locale

Conversation

@pmargreff

@pmargreff pmargreff commented Nov 21, 2026 •

Copy link
Copy Markdown

Summary of changes

Timex.Gettext compiles all 280 PO files (35 locales × 8 domains) into one module. Elixir compiles a module in a single process, so lib/l10n/gettext.ex takes nearly all of the time timex takes to compile, and the other files finish long before it. In a profiled build, it was 5.6 s of the package's 5.7 s. When Mix compiles an application's dependencies, every package that depends on timex waits for it too.

This PR passes split_module_by: [:locale] to use Gettext.Backend. Gettext then generates one internal module per locale (Timex.Gettext.T_de, ...) and compiles them in parallel. Timex.Gettext keeps the same functions, and each of its clauses calls into the module for that locale.

  • Gettext merges the application's config over the use options, so an application can still go back to a single module with config :timex, Timex.Gettext, split_module_by: [].

Results

mix deps.compile timex --force in a project that depends on timex by path, with one partition, median of 5 rounds after a warm-up, with the variants interleaved, on a 24-core Intel Core Ultra 9 285HX, Linux, Elixir 1.20.5, OTP 29.1.2, Gettext 1.0.2:

main This PR
Wall time 5.83 s (5.79–6.05) 1.36 s (1.35–1.39)
CPU time 12.5 s 13.8 s
Timex.Gettext* modules 1 (1.0 MB) 36 (1.3 MB)

I also tried split_module_by: [:locale, :domain], which gives 281 modules (1.8 MB): 1.48 s (1.43–1.52) and 14.5 s of CPU time. It was slower than splitting by locale in all rounds, so this PR splits by locale only.

Verifying change

Run from a project that depends on timex, once on each build, then compare the two files:

# mix run --no-start --no-compile gettext_dump.exs PRIV_DIR OUT
[priv, out] = System.argv()
backend = Timex.Gettext

rows =
  for po <- Path.wildcard(Path.join(priv, "*/LC_MESSAGES/*.po")) |> Enum.sort(),
      locale = po |> Path.dirname() |> Path.dirname() |> Path.basename(),
      domain = Path.basename(po, ".po"),
      %{} = msg <- Expo.PO.parse_file!(po).messages do
    id = IO.iodata_to_binary(msg.msgid)

    case msg do
      %Expo.Message.Plural{} ->
        idp = IO.iodata_to_binary(msg.msgid_plural)

        for n <- [0, 1, 2, 5, 21] do
          inspect({locale, domain, id, n,
                   backend.lngettext(locale, domain, nil, id, idp, n, %{count: n})})
        end

      _ ->
        inspect({locale, domain, id, backend.lgettext(locale, domain, nil, id, %{})})
    end
  end

File.write!(out, rows |> List.flatten() |> Enum.join("\n"))

Checklist

  • New functions have typespecs, changed functions were updated (no functions added or changed)
  • Same for documentation, including moduledocs (the generated modules are @moduledoc false)
  • Tests were added or updated to cover changes (see "Tests" above)
  • Commits were squashed into a single coherent commit
  • Notes added to CHANGELOG file which describe changes at a high-level

AI disclosure

I used Claude Code (Claude Opus 5.5) to find the slow module, write the change and run the measurements. I reviewed and validated the code and the results myself.

Split the Gettext backend by locale so each locale compiles as a separate
module in parallel. All translations previously compiled into one module,
which dominated package compilation and delayed dependent applications.

Retain the backend API and translation lookups. Applications can restore a
single module by configuring split_module_by for Timex.Gettext.
@josevalim

Copy link
Copy Markdown

It may be worth checking out Gettext main. It has several optimizations there which may alleviate the need for this.

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.

2 participants