Skip to content

Apache DataFusion SQL Query Engine

ARROW-2420: [Rust] Fix major memory bug and add benches

Andy Grove9d587712c0b5
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index c3120cfdc5..4d2476b0c5 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -36,4 +36,15 @@ path = "src/lib.rs"
[dependencies]
bytes = "0.4"
libc = "0.2"
-serde_json = "1.0.13"
\ No newline at end of file
+serde_json = "1.0.13"
+
+[dev-dependencies]
+criterion = "0.2"
+
+[[bench]]
+name = "array_from_vec"
+harness = false
+
+[[bench]]
+name = "array_from_builder"
+harness = false
\ No newline at end of file
diff --git a/rust/benches/array_from_builder.rs b/rust/benches/array_from_builder.rs
new file mode 100644
index 0000000000..3d020030e5
--- /dev/null
+++ b/rust/benches/array_from_builder.rs
@@ -0,0 +1,49 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#[macro_use]
+extern crate criterion;
+
+use criterion::Criterion;
+
+extern crate arrow;
+
+use arrow::array::*;
+use arrow::builder::*;
+
+fn array_from_builder(n: usize) {
+ let mut v: Builder<i32> = Builder::with_capacity(n);
+ for i in 0..n {
+ v.push(i as i32);
+ }
+ Array::from(v.finish());
+}
+
+fn criterion_benchmark(c: &mut Criterion) {
+ c.bench_function("array_from_builder 128", |b| {
+ b.iter(|| array_from_builder(128))
+ });
+ c.bench_function("array_from_builder 256", |b| {
+ b.iter(|| array_from_builder(256))
+ });
+ c.bench_function("array_from_builder 512", |b| {
+ b.iter(|| array_from_builder(512))
+ });
+}
+
+criterion_group!(benches, criterion_benchmark);
+criterion_main!(benches);
diff --git a/rust/benches/array_from_vec.rs b/rust/benches/array_from_vec.rs
new file mode 100644
index 0000000000..0feb0de0ba
--- /dev/null
+++ b/rust/benches/array_from_vec.rs
@@ -0,0 +1,42 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#[macro_use]
+extern crate criterion;
+
+use criterion::Criterion;
+
+extern crate arrow;
+
+use arrow::array::*;
+
+fn array_from_vec(n: usize) {
+ let mut v: Vec<i32> = Vec::with_capacity(n);
+ for i in 0..n {
+ v.push(i as i32);
+ }
+ Array::from(v);
+}
+
+fn criterion_benchmark(c: &mut Criterion) {
+ c.bench_function("array_from_vec 128", |b| b.iter(|| array_from_vec(128)));
+ c.bench_function("array_from_vec 256", |b| b.iter(|| array_from_vec(256)));
+ c.bench_function("array_from_vec 512", |b| b.iter(|| array_from_vec(512)));
+}
+
+criterion_group!(benches, criterion_benchmark);
+criterion_main!(benches);
diff --git a/rust/src/buffer.rs b/rust/src/buffer.rs
index 1f2ec6c8da..1cf004fb1b 100644
--- a/rust/src/buffer.rs
+++ b/rust/src/buffer.rs
@@ -74,7 +74,10 @@ impl<T> Buffer<T> {
impl<T> Drop for Buffer<T> {
fn drop(&mut self) {
- mem::drop(self.data)
+ unsafe {
+ let p = mem::transmute::<*const T, *mut libc::c_void>(self.data);
+ libc::free(p);
+ }
}
}
diff --git a/rust/src/builder.rs b/rust/src/builder.rs
index 58e5a128b1..832b2a4a81 100644
--- a/rust/src/builder.rs
+++ b/rust/src/builder.rs
@@ -99,7 +99,10 @@ impl<T> Builder<T> {
impl<T> Drop for Builder<T> {
fn drop(&mut self) {
if !self.data.is_null() {
- mem::drop(self.data)
+ unsafe {
+ let p = mem::transmute::<*const T, *mut libc::c_void>(self.data);
+ libc::free(p);
+ }
}
}
}