Skip to content

Commit a51e3a0

Browse files
getChanJefffrey
andauthored
minor : add crypto function benchmark (#19539)
## 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 1. add crypto function benchmark code 2. simplify inner digest function code 1. I checked for any performance changes using the benchmark code(1), but there was no changement. <!-- 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. --> ## 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. --> ## Are these changes tested? yes. <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 4. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? no <!-- 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 `api change` label. --> Co-authored-by: Jeffrey Vo <[email protected]>
1 parent a6fd5cc commit a51e3a0

File tree

3 files changed

+82
-14
lines changed

3 files changed

+82
-14
lines changed

datafusion/functions/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,11 @@ harness = false
275275
name = "ends_with"
276276
required-features = ["string_expressions"]
277277

278+
[[bench]]
279+
harness = false
280+
name = "crypto"
281+
required-features = ["crypto_expressions"]
282+
278283
[[bench]]
279284
harness = false
280285
name = "translate"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
extern crate criterion;
19+
20+
use arrow::datatypes::{DataType, Field};
21+
use arrow::util::bench_util::create_string_array_with_len;
22+
use criterion::{Criterion, criterion_group, criterion_main};
23+
use datafusion_common::config::ConfigOptions;
24+
use datafusion_expr::ScalarFunctionArgs;
25+
use datafusion_expr_common::columnar_value::ColumnarValue;
26+
use datafusion_functions::crypto;
27+
use std::hint::black_box;
28+
use std::sync::Arc;
29+
30+
fn criterion_benchmark(c: &mut Criterion) {
31+
let crypto = vec![
32+
crypto::md5(),
33+
crypto::sha224(),
34+
crypto::sha256(),
35+
crypto::sha384(),
36+
crypto::sha512(),
37+
];
38+
let config_options = Arc::new(ConfigOptions::default());
39+
40+
for func in crypto {
41+
let size = 1024;
42+
let arr_args = vec![ColumnarValue::Array(Arc::new(
43+
create_string_array_with_len::<i32>(size, 0.2, 32),
44+
))];
45+
c.bench_function(&format!("{}_array", func.name()), |b| {
46+
b.iter(|| {
47+
let args_cloned = arr_args.clone();
48+
black_box(func.invoke_with_args(ScalarFunctionArgs {
49+
args: args_cloned,
50+
arg_fields: vec![Field::new("a", DataType::Utf8, true).into()],
51+
number_rows: size,
52+
return_field: Field::new("f", DataType::Utf8, true).into(),
53+
config_options: Arc::clone(&config_options),
54+
}))
55+
})
56+
});
57+
58+
let scalar_args = vec![ColumnarValue::Scalar("test_string".into())];
59+
c.bench_function(&format!("{}_scalar", func.name()), |b| {
60+
b.iter(|| {
61+
let args_cloned = scalar_args.clone();
62+
black_box(func.invoke_with_args(ScalarFunctionArgs {
63+
args: args_cloned,
64+
arg_fields: vec![Field::new("a", DataType::Utf8, true).into()],
65+
number_rows: 1,
66+
return_field: Field::new("f", DataType::Utf8, true).into(),
67+
config_options: Arc::clone(&config_options),
68+
}))
69+
})
70+
});
71+
}
72+
}
73+
74+
criterion_group!(benches, criterion_benchmark);
75+
criterion_main!(benches);

datafusion/functions/src/crypto/basic.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,26 +173,14 @@ macro_rules! digest_to_array {
173173
($METHOD:ident, $INPUT:expr) => {{
174174
let binary_array: BinaryArray = $INPUT
175175
.iter()
176-
.map(|x| {
177-
x.map(|x| {
178-
let mut digest = $METHOD::default();
179-
digest.update(x);
180-
digest.finalize()
181-
})
182-
})
176+
.map(|x| x.map(|x| $METHOD::digest(x)))
183177
.collect();
184178
Arc::new(binary_array)
185179
}};
186180
}
187181

188182
macro_rules! digest_to_scalar {
189-
($METHOD: ident, $INPUT:expr) => {{
190-
ScalarValue::Binary($INPUT.as_ref().map(|v| {
191-
let mut digest = $METHOD::default();
192-
digest.update(v);
193-
digest.finalize().as_slice().to_vec()
194-
}))
195-
}};
183+
($METHOD: ident, $INPUT:expr) => {{ ScalarValue::Binary($INPUT.map(|v| $METHOD::digest(v).as_slice().to_vec())) }};
196184
}
197185

198186
impl DigestAlgorithm {

0 commit comments

Comments
 (0)