Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Add extend_from_iter into growable trait #773

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions benches/growable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ fn add_benchmark(c: &mut Criterion) {
})
});

c.bench_function("growable::dyn_primitive::non_null::non_null", |b| {
b.iter(|| {
let mut a: Box<dyn Growable> =
Box::new(GrowablePrimitive::new(vec![&i32_array], false, 1026 * 10));

let iter = values.clone().into_iter().map(|start| (0, start, 10));
a.extend_from_iter(Box::new(iter));
})
});

let i32_array = create_primitive_array::<i32>(1026 * 10, 0.0);
c.bench_function("growable::primitive::non_null::null", |b| {
b.iter(|| {
Expand Down
7 changes: 7 additions & 0 deletions src/array/growable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ pub trait Growable<'a> {
/// Extends this [`Growable`] with null elements, disregarding the bound arrays
fn extend_validity(&mut self, additional: usize);

/// Extends this [`Growable`] by iterator.
fn extend_from_iter(&mut self, iter: Box<dyn Iterator<Item = (usize, usize, usize)>>) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, I think that this won't work as intended because .extend will still be dynamically dispatched.

We do something like what you intend here: https://github.com/jorgecarleitao/arrow2/blob/main/src/compute/filter.rs#L87

Essentially, specialize the implementation based on the physical type, and use a generic one (https://github.com/jorgecarleitao/arrow2/blob/main/src/compute/filter.rs#L101) for all others.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://users.rust-lang.org/t/is-this-a-curiously-recurring-template-pattern/31004/6

As User @ExpHP said:

trait Trait {
    fn foo(&mut self);
    fn call_foo_twice(&mut self) {
        self.foo(); // <-- this is statically dispatched
        self.foo();
    }

    fn wrap(self) -> Wrapper<Self> { // <-- we can name Self
        Wrapper(self)
    }
}

so .extend is statically dispatched?

for (index, start, len) in iter {
self.extend(index, start, len);
}
}

/// Converts this [`Growable`] to an [`Arc<dyn Array>`], thereby finishing the mutation.
/// Self will be empty after such operation.
fn as_arc(&mut self) -> std::sync::Arc<dyn Array> {
Expand Down