Skip to content
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

Slow performance in a loop #13

Closed
closte opened this issue Feb 24, 2023 · 4 comments
Closed

Slow performance in a loop #13

closte opened this issue Feb 24, 2023 · 4 comments

Comments

@closte
Copy link

closte commented Feb 24, 2023

A few days ago I was testing the .NET C# integration of the UA Parser and I`m getting way faster execution time compared to your solution and Rust language.
Rust language is well known to be faster than C# in almost all cases.

Iterations: 1000
Rust (cargo run --release:): ~280ms for 1000 iterations
C#: ~130ms for 1000 iterations

Rust code looks like this:

let ua_parser = UserAgentParser::from_yaml(&yamlFile).expect("Parser creation failed");
let user_agent_string =  String::from("Mozilla/5.0 (X11; Linux x86_64; rv:2.0b8pre) Gecko/20101031 Firefox-4.0/4.0b8pre");
let mut user_agent = ua_parser.parse_user_agent(&user_agent_string);
for n in 1..1000{
        user_agent = ua_parser.parse_user_agent(&user_agent_string);
}

C# code looks like this:

 uaParser = Parser.FromYaml(fileContent, new ParserOptions{ UseCompiledRegex = true });
 int count = 1000;
         
for (int i = 0; i < count; i++){
     ClientInfo c = uaParser.Parse("Mozilla/5.0 (X11; Linux x86_64; rv:2.0b8pre) Gecko/20101031 Firefox-4.0/4.0b8pre");
}



I`m very newbie to Rust but maybe you can make all regex compiled/static like on the following URL or why the Rust implementation is almost double slower?

@hamiltop
Copy link
Contributor

hamiltop commented Jul 9, 2023

So all regexes are already compiled during initialization, so that's not the cause. The cause is likely unicode support in Rust, which makes regex much more complicated.

My fork/PR here #14 disables unicode support and provides a significant speedup as well as some memory savings. You can see an example of how to set up the parser without unicode support here: https://github.com/davidarmstronglewis/uap-rs/pull/14/files#diff-27d11095d02b2115142e677214a2e2791f4a6158c7e12c367338dfa8378ceed7

I'd love to see your comparison with this new code. The benchmark suite has a 3x+ speedup for the user_agent parsers, so hopefully that makes rust beat c# in your example.

@oceanlewis
Copy link
Owner

oceanlewis commented Jul 23, 2023

With #14 now merged (thank you @hamiltop) and version 0.6.1 released I would be curious where performance lands compared to your C# example.

Feel free to share your findings and if you're satisfied close this issue, @closte.

I can also imagine a silly optimization of memoizing user agent strings to avoid re-running the regex; but I'm not sure how much of a difference in performance and may not be worth the optimization.

@closte
Copy link
Author

closte commented Aug 2, 2023

I made a simple test and the same 1000 iterations ends is just +-30ms, faster than C# and way, way faster than the previous code.

Thanks
@hamiltop
@davidarmstronglewis

@oceanlewis
Copy link
Owner

Seriously, great work @hamiltop 🙌
Gonna close this one as it seems to be much more inline with performance expectations.
Thank you! ❤️

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

No branches or pull requests

3 participants