-
Notifications
You must be signed in to change notification settings - Fork 28
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
Regex bad performance #61
Comments
Hi, sure. I will take a look shortly. |
Yeah, the regular expressions are heavy and the general advise is to avoid recompiling them as much as possible. Thus, I can't see any other way but to solve it with something like a cache with the precompiled expressions. So, essentially, the library can only provide the option to accept the regex cache and use it. struct JsPathRegexCache {
cache: Arc<Mutex<HashMap<String, Regex>>>,
}
pub struct JsonPathConfig {
pub with_regex_cache: bool,
pub regex_cache: JsPathRegexCache,
}
fn example(global_cache:JsPathRegexCache) {
let json = Box::new(json!({}));
let cfg = JsonPathConfig::with_cache(global_cache)
let _v = (cfg, json).path("$.[?(@.author ~= '.*(?i)d\\(Rees\\)')]")
.expect("the path is correct");
} Obviously, it will improve the performance only in the long run. If the regexes are always different it will not help. I scribbled an example of how it may be in this branch In the given benchmark the duration gets boosted 5 times to roughly 25 µs on my laptop. Let me now, if this approach works and I will implement it shortly. |
Yes, is working fine. It would have been nice if serde_json::Value had a Regex field, I think in that case we would just added a "regex" rule and parse it directly, but this method works too |
Yeah, great. I will implement it in a week or two.
Well, it is beyond this repo unfortunately but even if not to me it would be not clear how it would solve the problem assuming:
But maybe, I just missed something. |
So, the story: The cache should be mutable(assuming refcell) and capable of working in multithreading (assuming Arc) |
Hey. I'm using this library at work and I have noticed some slowness when using regex, looks like creating a Regex is somehow slow. I have tested the theory using a global map for storing the compiled regex and it improved the speed ~50x. The implementation is not what we want, we should create the regex when creating the jsonpath and use it directly. I'm not sure how we can do that.
Here is the changes I made for testing:
https://github.com/catalinstochita/jsonpath-rust
Let me know if you have an idea of how we can do that. Thank you @besok
The text was updated successfully, but these errors were encountered: