Skip to content

Commit

Permalink
Improve t! macro avoid string allocate for missing key.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Jun 19, 2023
1 parent 38e2dde commit 7bde139
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ toml = "0.5.8"

[dev-dependencies]
foo = {path = "examples/foo"}
criterion = "0.3"
criterion = "0.5"

[features]
default = ["rust-i18n-extract", "clap", "anyhow", "quote", "itertools"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ You can add [i18n-ally-custom-framework.yml](https://github.com/longbridgeapp/ru

Benchmark `t!` method, result on Apple M1:

```
t time: [110.94 ns 113.66 ns 117.06 ns]
```bash
t time: [100.91 ns 101.06 ns 101.24 ns]
t_with_args time: [495.56 ns 497.88 ns 500.64 ns]
```

Expand Down
8 changes: 5 additions & 3 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use criterion::{criterion_group, criterion_main, Criterion};

fn bench_t(c: &mut Criterion) {
c.bench_function("t", |b| b.iter(|| t!("hello")));
}

fn bench_t_with_args(c: &mut Criterion) {
c.bench_function("t_with_args", |b| {
b.iter(|| t!("a.very.nested.message", name = "Jason", msg = "Bla bla"))
});

c.bench_function("t_with_args (str)", |b| {
b.iter(|| t!("a.very.nested.message", "name" = "Jason", "msg" = "Bla bla"))
});
}

criterion_group!(benches, bench_t, bench_t_with_args);
criterion_group!(benches, bench_t);
criterion_main!(benches);
4 changes: 1 addition & 3 deletions crates/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ fn generate_code(

/// Get I18n text by locale and key
pub fn _rust_i18n_translate(locale: &str, key: &str) -> String {
let target_key = format!("{}.{}", locale, key);

if let Some(value) = _RUST_I18N_BACKEND.translate(locale, key) {
return value.to_string();
}
Expand All @@ -179,7 +177,7 @@ fn generate_code(
}
}

return target_key
return format!("{}.{}", locale, key);
}

pub fn _rust_i18n_available_locales() -> Vec<&'static str> {
Expand Down
23 changes: 12 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,43 +45,44 @@ macro_rules! t {
crate::_rust_i18n_translate(rust_i18n::locale().as_str(), $key)
};

// t!("foo", locale="en")
($key:expr, locale=$locale:expr) => {
// t!("foo", locale = "en")
($key:expr, locale = $locale:expr) => {
crate::_rust_i18n_translate($locale, $key)
};

// t!("foo", locale="en", a=1, b="Foo")
($key:expr, locale=$locale:expr, $($var_name:tt = $var_val:expr),+ $(,)?) => {
// t!("foo", locale = "en", a = 1, b = "Foo")
($key:expr, locale = $locale:expr, $($var_name:tt = $var_val:expr),+ $(,)?) => {
{
let mut message = crate::_rust_i18n_translate($locale, $key);

$(
let var = stringify!($var_name).trim_matches('"');
let mut holder = std::string::String::from("%{");
holder.push_str(var);
holder.push('}');
// Get the variable name as a string, and remove quotes surrounding the variable name
let var_name = stringify!($var_name).trim_matches('"');
// Make a holder string to replace the variable name with: %{var_name}
let holder = format!("%{{{var_name}}}");

message = message.replace(&holder, &format!("{}", $var_val));
)+
message
}
};

// t!("foo %{a} %{b}", a="bar", b="baz")
// t!("foo %{a} %{b}", a = "bar", b = "baz")
($key:expr, $($var_name:tt = $var_val:expr),+ $(,)?) => {
{
t!($key, locale = &rust_i18n::locale(), $($var_name = $var_val),*)
}
};

// t!("foo %{a} %{b}", locale = "en", "a" => "bar", "b" => "baz")
($key:expr, locale = $locale:expr, $($var_name:expr => $var_val:expr),+ $(,)?) => {
($key:expr, locale = $locale:expr, $($var_name:tt => $var_val:expr),+ $(,)?) => {
{
t!($key, locale = $locale, $($var_name = $var_val),*)
}
};

// t!("foo %{a} %{b}", "a" => "bar", "b" => "baz")
($key:expr, $($var_name:expr => $var_val:expr),+ $(,)?) => {
($key:expr, $($var_name:tt => $var_val:expr),+ $(,)?) => {
{
t!($key, locale = &rust_i18n::locale(), $($var_name = $var_val),*)
}
Expand Down

0 comments on commit 7bde139

Please sign in to comment.