-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
Replace rust-i18n with phf string maps and simple replaces #1428
Conversation
Failure of the build-docker job seems to be a result of the way Dockerfile is written. The "caching" somehow resulted in build.rs being skipped. |
@nc7s I pushed a commit which fixes the Dockerfile. I've not had time to review the rest of this yet, I will over the weekend. |
aside: I think this may be broken on edit: yes, broken:
bug: #1431 |
c7355d2
to
26f97ef
Compare
@nc7s i've rebased and pushed a WIP commit that:
LTM what you think. |
26f97ef
to
1dd6c6a
Compare
1dd6c6a
to
5d9ce7d
Compare
I used phf because it's more performant in this small key scenario than SipHash currently used by mod locale;
use locale::*;
use rand::distributions::{Distribution, Uniform};
fn main() {
let mut rng = rand::thread_rng();
let all_keys: Vec<_> = data().0.keys().collect();
let all_locales: Vec<_> = available_locales();
let keys: Vec<_> = Uniform::new(0, all_keys.len())
.sample_iter(&mut rng)
.take(100)
.map(|i| all_keys[i])
.collect();
let locales: Vec<_> = Uniform::new(0, all_locales.len())
.sample_iter(&mut rng)
.take(100)
.map(|i| all_locales[i])
.collect();
for locale in locales {
set_locale(Some(locale));
for key in &keys {
println!("{locale}:{key} = '{}'", t!(key));
}
}
} and the result is ("s" means std i.e.
phf is significantly faster by these numbers, but then again, probably negligible as it's on the level of nanoseconds per read. So, ultimately your call. The code structure is kinda new to me but OK :> Side note: I implemented "language-region to language fallback" by cutting the first two chars of |
@nc7s can aside: we may want a buld.rs anyway, just to have cargo::rerun-if-changed for
I'd like to swap out the adhoc string parsing for a more robust and compliant |
@nc7s I replaced |
@fujiapple852 but if it never changes then maybe OnceCell might be a better fit? |
I don't think so, phf is "a library to generate efficient lookup tables", "at compile time". I understand build.rs is more or less ugly, but the cost of the perfect hash values might actually hurt performance if computed at runtime.
That would ease accepting a build.rs I guess ;) Like said earlier, though, this ultimately boils down to a difference of tens of nanoseconds per read.
This smells like reinventing (some aspects of) the wheel.. though I too lean towards correctness on this matter. |
I don't have a reason to object to removing the lock in single thread environments, but then you take the responsibility to make sure either it does not become multi-threaded, or if it does, use locks again ;) |
@c-git the problem with using |
9e04e04
to
5310209
Compare
Co-authored-by: FujiApple <[email protected]>
5310209
to
91c0133
Compare
Closes #1416
Closes #1431
Alternative to #1426 for comparison. Strings are now
&'static str
asphf::Map
entries, placeholder substitution is simplystr::replace
, no complex logic. Executable size is down 1MB (!).Downsides are 1. build.rs, 2.
str::replace
is not enough for complex translations.Currently both the
$kt:ident = $kt:expr
and$kt:literal => $kv:expr
forms oft!
are still supported, but I doubt if they really differ.