Skip to content

Optimize FileSource::get_path with single-pass string construction#2

Draft
avalanche-staging[bot] wants to merge 1 commit into
mainfrom
codspeed-wizard-1778071670987
Draft

Optimize FileSource::get_path with single-pass string construction#2
avalanche-staging[bot] wants to merge 1 commit into
mainfrom
codspeed-wizard-1778071670987

Conversation

@avalanche-staging
Copy link
Copy Markdown

Summary

Optimizes FileSource::get_path in intl/l10n/rust/l10nregistry-rs/src/source/mod.rs by replacing a multi-allocation string construction pattern with a single-pass approach.

Before

fn get_path(&self, locale: &LanguageIdentifier, resource_id: &ResourceId) -> String {
    format!(
        "{}{}",
        self.pre_path.replace("{locale}", &locale.to_string()),
        resource_id.value,
    )
}

This performs 3 heap allocations:

  1. locale.to_string() — intermediate String
  2. str::replace(...) — allocates a new String with StrSearcher overhead
  3. format!(...) — final concatenation into yet another String

After

fn get_path(&self, locale: &LanguageIdentifier, resource_id: &ResourceId) -> String {
    use std::fmt::Write;
    const PLACEHOLDER: &str = "{locale}";
    let pre_path = &self.pre_path;

    let cap = pre_path.len() + resource_id.value.len() + 10;
    let mut result = String::with_capacity(cap);

    let mut last = 0;
    while let Some(pos) = pre_path[last..].find(PLACEHOLDER) {
        let abs_pos = last + pos;
        result.push_str(&pre_path[last..abs_pos]);
        write!(result, "{}", locale).unwrap();
        last = abs_pos + PLACEHOLDER.len();
    }
    result.push_str(&pre_path[last..]);
    result.push_str(&resource_id.value);
    result
}

This performs 1 allocation with 0 reallocs:

  • String::with_capacity pre-allocates the buffer
  • write!(result, "{}", locale) formats the locale directly into the buffer (no intermediate String)
  • str::find replaces StrSearcher with a simpler substring search

Benchmark Results (simulation mode)

Source benchmarks show 35–45% instruction count reduction, with zero regressions on solver, registry, or preferences benchmarks:

Benchmark Before (Ir) After (Ir) Change
browser/has_file 99,990 55,423 -44.6%
browser/fetch_file_sync 141,896 91,448 -35.6%
preferences/has_file 117,529 72,757 -38.1%
preferences/fetch_file_sync 187,290 120,258 -35.8%
simple/has_file 2,863 1,753 -38.7%
simple/fetch_file_sync 6,254 3,860 -38.3%

Other changes

  • rustc-hash: Updated from v1 to v2 in l10nregistry-rs/Cargo.toml to resolve type conflicts with transitive dependencies (fluent-bundle, fluent-fallback)
  • Benchmarking: Replaced criterion = "0.3" with codspeed-criterion-compat in dev-dependencies and added a CodSpeed CI workflow

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 6, 2026

(Automated Close) Please do not file pull requests here, see https://firefox-source-docs.mozilla.org/contributing/how_to_submit_a_patch.html

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.

0 participants