You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "viirya (via GitHub)" <gi...@apache.org> on 2023/12/22 20:59:20 UTC
[PR] Improve regexp_match performance by avoiding cloning Regex [arrow-rs]
viirya opened a new pull request, #5235:
URL: https://github.com/apache/arrow-rs/pull/5235
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #.
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
The `regexp_match` scalar expression in DataFusion is quite slow. As looking for the cause, it is found that the bad performance is due to cloning `Regex` per row. `Regex` has a `CachePool`. Cloning `Regex` will create a fresh `CachePool` and it is somehow expensive to re-creating cache space per row instead of reusing the cache.
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
This patch removes cloning on `Regex` in regexp kernels.
```
regexp time: [6.4153 ms 6.4331 ms 6.4519 ms]
change: [-97.721% -97.713% -97.705%] (p = 0.00 < 0.05)
Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
8 (8.00%) high mild
```
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!---
If there are any breaking changes to public APIs, please add the `breaking change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#discussion_r1435418803
##########
arrow-string/src/regexp.rs:
##########
@@ -216,15 +215,14 @@ pub fn regexp_match<OffsetSize: OffsetSizeTrait>(
(Some(value), Some(pattern)) => {
let existing_pattern = patterns.get(&pattern);
let re = match existing_pattern {
- Some(re) => re.clone(),
+ Some(re) => re,
None => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
- patterns.insert(pattern, re.clone());
- re
+ patterns.entry(pattern).or_insert(re)
Review Comment:
`clone`? You mean `let entry = patterns.entry(pattern.clone());`? It cannot be removed.
```
216 | (Some(value), Some(pattern)) => {
| ------- move occurs because `pattern` has type `String`, which does not implement the `Copy` trait
217 | let entry = patterns.entry(pattern);
| ------- value moved here
...
221 | let re = Regex::new(pattern.as_str()).map_err(|e| {
| ^^^^^^^ value borrowed here after move
|
help: consider cloning the value if the performance cost is acceptable
|
217 | let entry = patterns.entry(pattern.clone());
| ++++++++
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#issuecomment-1868340223
Thank you @alamb @Dandandan @tustvold for review
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#discussion_r1435376638
##########
arrow-string/src/regexp.rs:
##########
@@ -216,15 +215,14 @@ pub fn regexp_match<OffsetSize: OffsetSizeTrait>(
(Some(value), Some(pattern)) => {
let existing_pattern = patterns.get(&pattern);
let re = match existing_pattern {
- Some(re) => re.clone(),
+ Some(re) => re,
None => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
- patterns.insert(pattern, re.clone());
- re
+ patterns.entry(pattern).or_insert(re)
Review Comment:
Entry is an enumeration you can match on, you don't have to use or_insert_with and friends, they're just utilities like on Option
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#discussion_r1435361674
##########
arrow-string/src/regexp.rs:
##########
@@ -216,15 +215,14 @@ pub fn regexp_match<OffsetSize: OffsetSizeTrait>(
(Some(value), Some(pattern)) => {
let existing_pattern = patterns.get(&pattern);
let re = match existing_pattern {
- Some(re) => re.clone(),
+ Some(re) => re,
None => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
- patterns.insert(pattern, re.clone());
- re
+ patterns.entry(pattern).or_insert(re)
Review Comment:
Hmm, then we need to use `or_insert_with` like
```rust
let re = patterns.entry(pattern).or_insert_with(|| {
Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
}?)
});
```
But `or_insert_with` cannot propagate the error out of the function.
Tried with `Result<Regex, ArrowError>` as value type with the `patterns` HashMap, but then it doesn't work because `the `?` operator cannot be applied to type `&mut Result<regex::Regex, arrow_schema::ArrowError>` too.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan commented on PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#issuecomment-1868260148
Thank you @viirya !
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#discussion_r1435384693
##########
arrow-string/src/regexp.rs:
##########
@@ -216,15 +215,14 @@ pub fn regexp_match<OffsetSize: OffsetSizeTrait>(
(Some(value), Some(pattern)) => {
let existing_pattern = patterns.get(&pattern);
let re = match existing_pattern {
- Some(re) => re.clone(),
+ Some(re) => re,
None => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
- patterns.insert(pattern, re.clone());
- re
+ patterns.entry(pattern).or_insert(re)
Review Comment:
Oh, the branch to eliminate is the one inside `or_insert`? Do you mean something like:
```rust
let entry = patterns.entry(pattern.clone());
let re = match entry {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
entry.insert(re)
}
};
```
Hmm, it actually gets regression a little. 🤔
```
regexp time: [7.6891 ms 7.7210 ms 7.7511 ms]
change: [+14.792% +15.570% +16.321%] (p = 0.00 < 0.05)
Performance has regressed.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#issuecomment-1868071898
> As an aside it occurs to me we should probably have a version of this that takes `Datum`
Yea, I did a local version and tested it with DataFusion. It turns out the performance bottleneck is on cloning Regex mostly. But it is per query benchmark and I didn't run this kernel benchmark in it.
I will run this benchmark against the Datum version and probably submit it later.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#discussion_r1435345943
##########
arrow-string/src/regexp.rs:
##########
@@ -81,15 +81,15 @@ pub fn regexp_is_match_utf8<OffsetSize: OffsetSizeTrait>(
(Some(value), Some(pattern)) => {
let existing_pattern = patterns.get(&pattern);
let re = match existing_pattern {
- Some(re) => re.clone(),
+ Some(re) => re,
None => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
- patterns.insert(pattern, re.clone());
- re
+ patterns.insert(pattern.clone(), re);
Review Comment:
Perhaps you could use https://doc.rust-lang.org/std/collections/hash_map/enum.Entry.html#method.or_insert
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#discussion_r1435361674
##########
arrow-string/src/regexp.rs:
##########
@@ -216,15 +215,14 @@ pub fn regexp_match<OffsetSize: OffsetSizeTrait>(
(Some(value), Some(pattern)) => {
let existing_pattern = patterns.get(&pattern);
let re = match existing_pattern {
- Some(re) => re.clone(),
+ Some(re) => re,
None => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
- patterns.insert(pattern, re.clone());
- re
+ patterns.entry(pattern).or_insert(re)
Review Comment:
Hmm, then we need to use `or_insert_with` like
```rust
let re = patterns.entry(pattern).or_insert_with(|| {
Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
}?)
});
```
But `or_insert_with` cannot propagate the error out of the function.
Tried with `Result<Regex, ArrowError>` as value type with the `patterns` HashMap, but then it doesn't work because `the ? operator cannot be applied to type &mut Result<regex::Regex, arrow_schema::ArrowError>` too.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#discussion_r1435347431
##########
arrow-string/src/regexp.rs:
##########
@@ -216,15 +215,14 @@ pub fn regexp_match<OffsetSize: OffsetSizeTrait>(
(Some(value), Some(pattern)) => {
let existing_pattern = patterns.get(&pattern);
let re = match existing_pattern {
- Some(re) => re.clone(),
+ Some(re) => re,
None => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
- patterns.insert(pattern, re.clone());
- re
+ patterns.entry(pattern).or_insert(re)
Review Comment:
If you replaced the `get` above with this entry call, it might improve the performance further by eliminating a branch perhaps??
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#discussion_r1435361674
##########
arrow-string/src/regexp.rs:
##########
@@ -216,15 +215,14 @@ pub fn regexp_match<OffsetSize: OffsetSizeTrait>(
(Some(value), Some(pattern)) => {
let existing_pattern = patterns.get(&pattern);
let re = match existing_pattern {
- Some(re) => re.clone(),
+ Some(re) => re,
None => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
- patterns.insert(pattern, re.clone());
- re
+ patterns.entry(pattern).or_insert(re)
Review Comment:
Hmm, then we need to use `or_insert_with` like
```rust
let re = patterns.entry(pattern).or_insert_with(|| {
Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
}?)
});
```
But `or_insert_with` cannot propagate the error out of the function.
Tried with `Result<Regex, ArrowError>` as value type with the `patterns` HashMap, but then it doesn't work because `the ? operator cannot be applied to type &mut Result<regex::Regex, arrow_schema::ArrowError>` too.
So just keep it as is.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "Dandandan (via GitHub)" <gi...@apache.org>.
Dandandan merged PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#discussion_r1435376638
##########
arrow-string/src/regexp.rs:
##########
@@ -216,15 +215,14 @@ pub fn regexp_match<OffsetSize: OffsetSizeTrait>(
(Some(value), Some(pattern)) => {
let existing_pattern = patterns.get(&pattern);
let re = match existing_pattern {
- Some(re) => re.clone(),
+ Some(re) => re,
None => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
- patterns.insert(pattern, re.clone());
- re
+ patterns.entry(pattern).or_insert(re)
Review Comment:
Entry is an enumeration you can match on
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#discussion_r1435414837
##########
arrow-string/src/regexp.rs:
##########
@@ -216,15 +215,14 @@ pub fn regexp_match<OffsetSize: OffsetSizeTrait>(
(Some(value), Some(pattern)) => {
let existing_pattern = patterns.get(&pattern);
let re = match existing_pattern {
- Some(re) => re.clone(),
+ Some(re) => re,
None => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
- patterns.insert(pattern, re.clone());
- re
+ patterns.entry(pattern).or_insert(re)
Review Comment:
That is what I meant, perhaps the regression is because of the added clone (which i think can be removed)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve regexp kernels performance by avoiding cloning Regex [arrow-rs]
Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #5235:
URL: https://github.com/apache/arrow-rs/pull/5235#discussion_r1435542803
##########
arrow-string/src/regexp.rs:
##########
@@ -216,15 +215,14 @@ pub fn regexp_match<OffsetSize: OffsetSizeTrait>(
(Some(value), Some(pattern)) => {
let existing_pattern = patterns.get(&pattern);
let re = match existing_pattern {
- Some(re) => re.clone(),
+ Some(re) => re,
None => {
let re = Regex::new(pattern.as_str()).map_err(|e| {
ArrowError::ComputeError(format!(
"Regular expression did not compile: {e:?}"
))
})?;
- patterns.insert(pattern, re.clone());
- re
+ patterns.entry(pattern).or_insert(re)
Review Comment:
Oh because of the unfortunate way flags are handled... Yeah this kernel would probably do with redesigning 😅
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org