Skip to content
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

Lazily materialize the null buffer builder for all array builders. #2127

Merged

Conversation

HaoYang670
Copy link
Contributor

Which issue does this PR close?

Closes #2125.

Rationale for this change

Please see the issue.

What changes are included in this PR?

  1. Define the NullBufferBuilder, which does the lazily materializing optimization.
  2. Let all array builders to use this new builder.
  3. rename bitmap_builder to null_buffer_builder

Are there any user-facing changes?

I guess there is no backward compatibility broken, but not 100% sure

Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
@github-actions github-actions bot added the arrow Changes to the arrow crate label Jul 22, 2022
Signed-off-by: remzi <13716567376yh@gmail.com>
Comment on lines +96 to +97
#[inline]
pub fn append_nulls(&mut self, n: usize) {
Copy link
Member

Choose a reason for hiding this comment

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

Add a simple doc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added!

@@ -53,6 +54,7 @@ pub use generic_binary_builder::GenericBinaryBuilder;
pub use generic_list_builder::GenericListBuilder;
pub use generic_string_builder::GenericStringBuilder;
pub use map_builder::MapBuilder;
pub use null_buffer_builder::NullBufferBuilder;
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to expose it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!

}

impl NullBufferBuilder {
pub fn new(capacity: usize) -> Self {
Copy link
Member

Choose a reason for hiding this comment

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

It's better to add simple doc for these functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!

viirya
viirya previously approved these changes Jul 22, 2022
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
@codecov-commenter
Copy link

codecov-commenter commented Jul 22, 2022

Codecov Report

Merging #2127 (49c963c) into master (ec3530d) will increase coverage by 0.01%.
The diff coverage is 96.26%.

@@            Coverage Diff             @@
##           master    #2127      +/-   ##
==========================================
+ Coverage   82.86%   82.87%   +0.01%     
==========================================
  Files         237      238       +1     
  Lines       61429    61475      +46     
==========================================
+ Hits        50902    50949      +47     
+ Misses      10527    10526       -1     
Impacted Files Coverage Δ
arrow/src/array/builder/boolean_builder.rs 84.26% <66.66%> (-2.47%) ⬇️
arrow/src/array/builder/struct_builder.rs 77.09% <85.71%> (-0.26%) ⬇️
arrow/src/array/builder/null_buffer_builder.rs 98.75% <98.75%> (ø)
...row/src/array/builder/fixed_size_binary_builder.rs 85.33% <100.00%> (ø)
arrow/src/array/builder/fixed_size_list_builder.rs 88.88% <100.00%> (ø)
arrow/src/array/builder/generic_binary_builder.rs 92.00% <100.00%> (ø)
arrow/src/array/builder/generic_list_builder.rs 95.06% <100.00%> (ø)
arrow/src/array/builder/map_builder.rs 81.63% <100.00%> (ø)
arrow/src/array/builder/primitive_builder.rs 95.26% <100.00%> (+1.36%) ⬆️
arrow/src/array/builder/union_builder.rs 91.66% <100.00%> (ø)
... and 2 more

Help us with your feedback. Take ten seconds to tell us how you rate us.

@tustvold
Copy link
Contributor

I will run the benchmarks shortly to verify this doesn't represent a performance regression, I sincerely doubt it, but stranger things have happened 😅

@tustvold
Copy link
Contributor

Sadly the benchmarks would suggest something is not quite happy with this change...

cargo bench --bench builder -- --baseline master
   Compiling arrow v18.0.0 (/data/raphael/arrow-rs/arrow)
    Finished bench [optimized] target(s) in 49.08s
     Running benches/builder.rs (/data/raphael/arrow-rs/target/release/deps/builder-b12e265674091bcf)
WARNING: HTML report generation will become a non-default optional feature in Criterion.rs 0.4.0.
This feature is being moved to cargo-criterion (https://github.com/bheisler/cargo-criterion) and will be optional in a future version of Criterion.rs. To silence this warning, either switch to cargo-criterion or enable the 'html_reports' feature in your Cargo.toml.

WARNING: In Criterion.rs 0.4.0, running criterion benchmarks outside of cargo-criterion will become a default optional feature.
The statistical analysis and reporting is being moved to cargo-criterion (https://github.com/bheisler/cargo-criterion) and will be optional in a future version of Criterion.rs. To silence this warning, either switch to cargo-criterion or enable the 'cargo_bench_support' feature in your Cargo.toml.

Gnuplot not found, using plotters backend
bench_primitive/bench_primitive                                                                            
                        time:   [689.88 us 690.61 us 691.51 us]
                        thrpt:  [5.6489 GiB/s 5.6562 GiB/s 5.6622 GiB/s]
                 change:
                        time:   [-0.2259% -0.0747% +0.0924%] (p = 0.36 > 0.05)
                        thrpt:  [-0.0923% +0.0748% +0.2264%]
                        No change in performance detected.

Benchmarking bench_primitive_nulls/bench_primitive_nulls: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 9.9s, enable flat sampling, or reduce sample count to 50.
bench_primitive_nulls/bench_primitive_nulls                                                                             
                        time:   [1.9564 ms 1.9569 ms 1.9575 ms]
                        change: [+81.473% +81.584% +81.697%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

bench_bool/bench_bool   time:   [759.97 us 762.42 us 765.04 us]                                  
                        thrpt:  [653.56 MiB/s 655.80 MiB/s 657.92 MiB/s]
                 change:
                        time:   [+111.83% +114.64% +117.24%] (p = 0.00 < 0.05)
                        thrpt:  [-53.968% -53.411% -52.792%]
                        Performance has regressed.
Found 4 outliers among 100 measurements (4.00%)
  4 (4.00%) high mild

bench_primitive/bench_string                                                                            
                        time:   [5.7475 ms 5.7512 ms 5.7550 ms]
                        thrpt:  [1.1030 GiB/s 1.1037 GiB/s 1.1044 GiB/s]
                 change:
                        time:   [+40.620% +40.811% +41.003%] (p = 0.00 < 0.05)
                        thrpt:  [-29.079% -28.983% -28.886%]
                        Performance has regressed.

@viirya viirya dismissed their stale review July 22, 2022 18:33

performance regression :(

@@ -53,6 +54,7 @@ pub use generic_binary_builder::GenericBinaryBuilder;
pub use generic_list_builder::GenericListBuilder;
pub use generic_string_builder::GenericStringBuilder;
pub use map_builder::MapBuilder;
pub(self) use null_buffer_builder::NullBufferBuilder;
Copy link
Contributor

Choose a reason for hiding this comment

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

How is pub(self) use different from just use

/// Builds the null buffer and resets the builder.
/// Returns `None` if the builder only contains `true`s.
pub fn finish(&mut self) -> Option<Buffer> {
let buf = self.bitmap_builder.as_mut().map(|b| b.finish());
Copy link
Contributor

Choose a reason for hiding this comment

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

Could use take here

@HaoYang670
Copy link
Contributor Author

Thank you @tustvold !. The benchmark result is really interesting. I can reproduce some of the result:
Tested on M1 Pro Mac:

remziy@remziy-mlt:Arrow-rs$ cargo bench --bench builder -- --baseline master     
   Compiling arrow v18.0.0 (/Users/remziy/FangcloudV2/personal_space.localized/working/Arrow-rs/arrow)
    Finished bench [optimized] target(s) in 39.38s
     Running benches/builder.rs (target/release/deps/builder-dd4589600d542fc3)
WARNING: HTML report generation will become a non-default optional feature in Criterion.rs 0.4.0.
This feature is being moved to cargo-criterion (https://github.com/bheisler/cargo-criterion) and will be optional in a future version of Criterion.rs. To silence this warning, either switch to cargo-criterion or enable the 'html_reports' feature in your Cargo.toml.

WARNING: In Criterion.rs 0.4.0, running criterion benchmarks outside of cargo-criterion will become a default optional feature.
The statistical analysis and reporting is being moved to cargo-criterion (https://github.com/bheisler/cargo-criterion) and will be optional in a future version of Criterion.rs. To silence this warning, either switch to cargo-criterion or enable the 'cargo_bench_support' feature in your Cargo.toml.

Gnuplot not found, using plotters backend
bench_primitive/bench_primitive                                                                            
                        time:   [642.48 us 646.76 us 651.53 us]
                        thrpt:  [5.9955 GiB/s 6.0397 GiB/s 6.0799 GiB/s]
                 change:
                        time:   [+1.3793% +3.3046% +5.3602%] (p = 0.00 < 0.05)
                        thrpt:  [-5.0875% -3.1989% -1.3605%]
                        Performance has regressed.
Found 15 outliers among 100 measurements (15.00%)
  10 (10.00%) high mild
  5 (5.00%) high severe

Benchmarking bench_primitive_nulls/bench_primitive_nulls: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.3s, enable flat sampling, or reduce sample count to 50.
bench_primitive_nulls/bench_primitive_nulls                                                                             
                        time:   [1.4423 ms 1.4447 ms 1.4480 ms]
                        change: [+4.0353% +4.8072% +5.8474%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 9 outliers among 100 measurements (9.00%)
  1 (1.00%) high mild
  8 (8.00%) high severe

bench_bool/bench_bool   time:   [658.62 us 687.66 us 722.13 us]                                  
                        thrpt:  [692.40 MiB/s 727.10 MiB/s 759.16 MiB/s]
                 change:
                        time:   [-0.8443% +1.7920% +4.6238%] (p = 0.23 > 0.05)
                        thrpt:  [-4.4195% -1.7604% +0.8515%]
                        No change in performance detected.
Found 30 outliers among 100 measurements (30.00%)
  20 (20.00%) low severe
  1 (1.00%) low mild
  9 (9.00%) high severe

bench_primitive/bench_string                                                                             
                        time:   [3.6955 ms 3.7110 ms 3.7281 ms]
                        thrpt:  [1.7027 GiB/s 1.7105 GiB/s 1.7177 GiB/s]
                 change:
                        time:   [+29.974% +30.513% +31.173%] (p = 0.00 < 0.05)
                        thrpt:  [-23.765% -23.379% -23.062%]
                        Performance has regressed.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) high mild
  1 (1.00%) high severe

The most obvious regression happened on bench_string which didn't use the lazy materialization previously. I will try to reproduce this result on my Intel desktop.

@HaoYang670
Copy link
Contributor Author

I can reproduce all the regressions on Intel Ubuntu:

(base) remziy@remzi-desktop:~/learning/Arrow$ cargo bench --bench builder -- --baseline master   
   Compiling arrow v18.0.0 (/home/remziy/learning/Arrow/arrow)
    Finished bench [optimized] target(s) in 37.52s
     Running benches/builder.rs (target/release/deps/builder-2cea1707400a3af5)
WARNING: HTML report generation will become a non-default optional feature in Criterion.rs 0.4.0.
This feature is being moved to cargo-criterion (https://github.com/bheisler/cargo-criterion) and will be optional in a future version of Criterion.rs. To silence this warning, either switch to cargo-criterion or enable the 'html_reports' feature in your Cargo.toml.

WARNING: In Criterion.rs 0.4.0, running criterion benchmarks outside of cargo-criterion will become a default optional feature.
The statistical analysis and reporting is being moved to cargo-criterion (https://github.com/bheisler/cargo-criterion) and will be optional in a future version of Criterion.rs. To silence this warning, either switch to cargo-criterion or enable the 'cargo_bench_support' feature in your Cargo.toml.

Gnuplot not found, using plotters backend
bench_primitive/bench_primitive                                                                            
                        time:   [196.37 us 196.46 us 196.59 us]
                        thrpt:  [19.870 GiB/s 19.883 GiB/s 19.893 GiB/s]
                 change:
                        time:   [-6.0712% -5.9337% -5.8025%] (p = 0.00 < 0.05)
                        thrpt:  [+6.1599% +6.3080% +6.4637%]
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  1 (1.00%) high mild
  8 (8.00%) high severe

Benchmarking bench_primitive_nulls/bench_primitive_nulls: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.3s, enable flat sampling, or reduce sample count to 50.
bench_primitive_nulls/bench_primitive_nulls                                                                             
                        time:   [1.4484 ms 1.4497 ms 1.4514 ms]
                        change: [+82.679% +83.105% +83.671%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 11 outliers among 100 measurements (11.00%)
  1 (1.00%) low mild
  2 (2.00%) high mild
  8 (8.00%) high severe

bench_bool/bench_bool   time:   [680.40 us 718.81 us 763.42 us]                                  
                        thrpt:  [654.95 MiB/s 695.59 MiB/s 734.86 MiB/s]
                 change:
                        time:   [+125.17% +134.83% +144.58%] (p = 0.00 < 0.05)
                        thrpt:  [-59.113% -57.417% -55.590%]
                        Performance has regressed.
Found 21 outliers among 100 measurements (21.00%)
  1 (1.00%) high mild
  20 (20.00%) high severe

bench_primitive/bench_string                                                                             
                        time:   [3.4884 ms 3.4953 ms 3.5028 ms]
                        thrpt:  [1.8122 GiB/s 1.8161 GiB/s 1.8197 GiB/s]
                 change:
                        time:   [+77.994% +78.687% +79.272%] (p = 0.00 < 0.05)
                        thrpt:  [-44.219% -44.036% -43.818%]
                        Performance has regressed.
Found 7 outliers among 100 measurements (7.00%)
  7 (7.00%) high mild

Will take some time to have a look

@HaoYang670 HaoYang670 marked this pull request as draft July 23, 2022 00:36
@HaoYang670
Copy link
Contributor Author

Mark the PR as draft until I solve the performance regression.

Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
Signed-off-by: remzi <13716567376yh@gmail.com>
@HaoYang670 HaoYang670 marked this pull request as ready for review July 23, 2022 12:47
@HaoYang670
Copy link
Contributor Author

After doing lots of bench tests today, I found that the performance regression of bench_bool is really weird. It is caused by value builder, but not the null buffer builder. I have tried to remove the impact of the null buffer builder:

    /// Appends a slice of type `T` into the builder
    #[inline]
    pub fn append_slice(&mut self, v: &[bool]) {
        //self.null_buffer_builder.append_n_true(v.len());
        self.values_builder.append_slice(v);
    }

and run cargo bench --bench builder -- --baseline master ,
but still got the weird result: (Tested on Intel Ubuntu)

bench_bool/bench_bool   time:   [560.89 us 561.72 us 562.61 us]                                  
                        thrpt:  [888.71 MiB/s 890.13 MiB/s 891.44 MiB/s]
                 change:
                        time:   [+116.82% +117.38% +117.92%] (p = 0.00 < 0.05)
                        thrpt:  [-54.113% -53.997% -53.879%]
                        Performance has regressed.
Found 4 outliers among 100 measurements (4.00%)
  1 (1.00%) low severe
  1 (1.00%) low mild
  2 (2.00%) high mild

I have also tried to inline(always) the BooleanBufferBuilder::append_slice but it had no effect.
cc @tustvold @viirya @alamb could you reproduce the above result? And any idea about it?

@HaoYang670
Copy link
Contributor Author

Latest benchmark result: (Tested on Intel Ununtu)

(base) remziy@remzi-desktop:~/learning/Arrow$ cargo bench --bench builder -- --baseline master   
   Compiling arrow v19.0.0 (/home/remziy/learning/Arrow/arrow)
    Finished bench [optimized] target(s) in 38.60s
     Running benches/builder.rs (target/release/deps/builder-40504c7a17ff8e19)
WARNING: HTML report generation will become a non-default optional feature in Criterion.rs 0.4.0.
This feature is being moved to cargo-criterion (https://github.com/bheisler/cargo-criterion) and will be optional in a future version of Criterion.rs. To silence this warning, either switch to cargo-criterion or enable the 'html_reports' feature in your Cargo.toml.

WARNING: In Criterion.rs 0.4.0, running criterion benchmarks outside of cargo-criterion will become a default optional feature.
The statistical analysis and reporting is being moved to cargo-criterion (https://github.com/bheisler/cargo-criterion) and will be optional in a future version of Criterion.rs. To silence this warning, either switch to cargo-criterion or enable the 'cargo_bench_support' feature in your Cargo.toml.

Gnuplot not found, using plotters backend
bench_primitive/bench_primitive                                                                            
                        time:   [211.47 us 211.66 us 211.89 us]
                        thrpt:  [18.435 GiB/s 18.456 GiB/s 18.472 GiB/s]
                 change:
                        time:   [-1.5327% -1.2994% -1.0743%] (p = 0.00 < 0.05)
                        thrpt:  [+1.0859% +1.3165% +1.5565%]
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  2 (2.00%) high mild
  8 (8.00%) high severe

bench_primitive_nulls/bench_primitive_nulls                                                                            
                        time:   [775.27 us 775.69 us 776.16 us]
                        change: [-1.4419% -1.1132% -0.7254%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 5 outliers among 100 measurements (5.00%)
  4 (4.00%) high mild
  1 (1.00%) high severe

bench_bool/bench_bool   time:   [558.66 us 559.42 us 560.19 us]                                  
                        thrpt:  [892.56 MiB/s 893.79 MiB/s 894.99 MiB/s]
                 change:
                        time:   [+93.444% +94.320% +95.190%] (p = 0.00 < 0.05)
                        thrpt:  [-48.768% -48.538% -48.306%]
                        Performance has regressed.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

Benchmarking bench_primitive/bench_string: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 9.6s, enable flat sampling, or reduce sample count to 50.
bench_primitive/bench_string                                                                             
                        time:   [1.8946 ms 1.8964 ms 1.8985 ms]
                        thrpt:  [3.3436 GiB/s 3.3473 GiB/s 3.3504 GiB/s]
                 change:
                        time:   [-7.5533% -7.3020% -6.9959%] (p = 0.00 < 0.05)
                        thrpt:  [+7.5222% +7.8771% +8.1705%]
                        Performance has improved.
Found 13 outliers among 100 measurements (13.00%)
  6 (6.00%) high mild
  7 (7.00%) high severe

#[cold]
fn materialize(&mut self) {
if self.bitmap_builder.is_none() {
let mut b = BooleanBufferBuilder::new(self.len.max(self.capacity));
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is usually called right before appending a new element, should it use len+1 instead? Or maybe an additional parameter for the append_n or append_slice usecase?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you @jhorstmann. That's a good catch, which I think we could gain some performance improvement from it.

fix spelling mistake

Co-authored-by: Jörn Horstmann <git@jhorstmann.net>
@jhorstmann
Copy link
Contributor

Another performance idea to try could be to remove the Option. BooleanBufferBuilder with a capacity of 0 should not allocate anything and you could replace the checks for Some with capacity() > 0.

@HaoYang670
Copy link
Contributor Author

@jhorstmann from the benchmark result, We didn't see Option introduce obvious overhead in the no-null case. (And we could trust rustc could optimize pattern-matching as fast (or even faster) as if-else branch, right?)

What blocks this PR so far is that bench_bool gives different behaviour from bench_primitive (#2127 (comment)). And the performance regression is introduced by the value builder (which is not modified in the PR), but not the null buffer builder. (#2127 (comment))

@tustvold
Copy link
Contributor

tustvold commented Jul 25, 2022

In the past I've noticed significant regressions when I've accidentally reordered operations around bound checks in such a way that LLVM cannot elide them. Not sure if that applies in this case though... Capacity reservation would be another likely suspect

@HaoYang670
Copy link
Contributor Author

In the past I've noticed significant regressions when I've accidentally reordered operations around bound checks in such a way that LLVM cannot elide them. Not sure if that applies in this case though... Capacity reservation would be another likely suspect

Amazing @tustvold, you hit it! After reordering the 2 lines in BooleanBuilder::append_slice, the performance regression disappeared!

bench_bool/bench_bool   time:   [259.80 us 260.93 us 262.23 us]                                  
                        thrpt:  [1.8621 GiB/s 1.8713 GiB/s 1.8795 GiB/s]
                 change:
                        time:   [-10.518% -10.061% -9.5849%] (p = 0.00 < 0.05)
                        thrpt:  [+10.601% +11.186% +11.754%]
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  2 (2.00%) high mild
  5 (5.00%) high severe

@HaoYang670
Copy link
Contributor Author

Latest benchmark result: compared with the latest master branch

cargo bench --bench builder -- --baseline master 

Tested on Intel Ubuntu

bench_primitive/bench_primitive                                                                            
                        time:   [201.59 us 201.64 us 201.71 us]
                        thrpt:  [19.366 GiB/s 19.372 GiB/s 19.377 GiB/s]
                 change:
                        time:   [-2.5350% -2.3169% -2.1224%] (p = 0.00 < 0.05)
                        thrpt:  [+2.1684% +2.3719% +2.6009%]
                        Performance has improved.
Found 13 outliers among 100 measurements (13.00%)
  2 (2.00%) high mild
  11 (11.00%) high severe

bench_primitive_nulls/bench_primitive_nulls                                                                            
                        time:   [775.12 us 775.51 us 775.92 us]
                        change: [-1.4140% -1.1018% -0.8333%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 14 outliers among 100 measurements (14.00%)
  3 (3.00%) low mild
  4 (4.00%) high mild
  7 (7.00%) high severe

bench_bool/bench_bool   time:   [256.99 us 257.43 us 257.90 us]                                  
                        thrpt:  [1.8933 GiB/s 1.8968 GiB/s 1.9000 GiB/s]
                 change:
                        time:   [-12.722% -12.251% -11.797%] (p = 0.00 < 0.05)
                        thrpt:  [+13.374% +13.962% +14.576%]
                        Performance has improved.
Found 13 outliers among 100 measurements (13.00%)
  9 (9.00%) low mild
  3 (3.00%) high mild
  1 (1.00%) high severe

Benchmarking bench_primitive/bench_string: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 9.8s, enable flat sampling, or reduce sample count to 50.
bench_primitive/bench_string                                                                             
                        time:   [1.9121 ms 1.9134 ms 1.9150 ms]
                        thrpt:  [3.3147 GiB/s 3.3175 GiB/s 3.3197 GiB/s]
                 change:
                        time:   [-5.7654% -5.4834% -5.1914%] (p = 0.00 < 0.05)
                        thrpt:  [+5.4757% +5.8015% +6.1181%]
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  6 (6.00%) high mild
  2 (2.00%) high severe

Tested on M1 Pro Mac

bench_primitive/bench_primitive                                                                            
                        time:   [620.30 us 623.83 us 627.90 us]
                        thrpt:  [6.2211 GiB/s 6.2617 GiB/s 6.2973 GiB/s]
                 change:
                        time:   [-0.5679% +0.7610% +2.1520%] (p = 0.29 > 0.05)
                        thrpt:  [-2.1066% -0.7552% +0.5712%]
                        No change in performance detected.
Found 9 outliers among 100 measurements (9.00%)
  3 (3.00%) high mild
  6 (6.00%) high severe

Benchmarking bench_primitive_nulls/bench_primitive_nulls: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.0s, enable flat sampling, or reduce sample count to 50.
bench_primitive_nulls/bench_primitive_nulls                                                                             
                        time:   [1.3842 ms 1.3854 ms 1.3865 ms]
                        change: [+0.2321% +0.5164% +0.8048%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 5 outliers among 100 measurements (5.00%)
  1 (1.00%) high mild
  4 (4.00%) high severe

bench_bool/bench_bool   time:   [651.99 us 654.72 us 658.97 us]                                  
                        thrpt:  [758.75 MiB/s 763.69 MiB/s 766.88 MiB/s]
                 change:
                        time:   [-3.1996% -1.5821% -0.1934%] (p = 0.04 < 0.05)
                        thrpt:  [+0.1938% +1.6075% +3.3053%]
                        Change within noise threshold.
Found 7 outliers among 100 measurements (7.00%)
  2 (2.00%) high mild
  5 (5.00%) high severe

bench_primitive/bench_string                                                                             
                        time:   [2.8055 ms 2.8106 ms 2.8161 ms]
                        thrpt:  [2.2540 GiB/s 2.2585 GiB/s 2.2626 GiB/s]
                 change:
                        time:   [-1.7947% -1.4838% -1.1946%] (p = 0.00 < 0.05)
                        thrpt:  [+1.2090% +1.5061% +1.8275%]
                        Performance has improved.
Found 18 outliers among 100 measurements (18.00%)
  6 (6.00%) low severe
  4 (4.00%) low mild
  6 (6.00%) high mild
  2 (2.00%) high severe

cc @tustvold @viirya @jhorstmann @alamb Please help to review, thank you!


/// Appends a `true` into the builder.
#[inline]
pub fn append_true(&mut self) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering the function names append_non_null v.s. append_true and append_null v.s. append_false?

Not strong opinion though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!

if let Some(buf) = self.bitmap_builder.as_mut() {
buf.append_n(n, true)
} else {
self.len += n;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This causes the inconsistency between self.len and bitmap_builder.len. Also, I don't want the struct to store self.len and self.capacity after materializing.

My thought is to create a private enum, named NullBufferBuilderVariant:

enum NullBufferBuilderVariant {
    PreMaterialized {len: usize, capacity: usize},
    Materialized(builder: BooleanBufferBuilder)
}

pub(super) struct NullBufferBuilder {
    variant: NullBufferBuilderVariant,
}
...

Hope this will not introduce performance regression.
I will file a follow-up to track this.

Signed-off-by: remzi <13716567376yh@gmail.com>
Copy link
Member

@viirya viirya left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@tustvold
Copy link
Contributor

bench_primitive/bench_primitive                                                                            
                        time:   [698.49 us 700.07 us 701.54 us]
                        thrpt:  [5.5681 GiB/s 5.5798 GiB/s 5.5924 GiB/s]
                 change:
                        time:   [-1.3657% -0.9539% -0.6017%] (p = 0.00 < 0.05)
                        thrpt:  [+0.6053% +0.9630% +1.3846%]
                        Change within noise threshold.

Benchmarking bench_primitive_nulls/bench_primitive_nulls: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 5.1s, enable flat sampling, or reduce sample count to 60.
bench_primitive_nulls/bench_primitive_nulls                                                                             
                        time:   [1.0073 ms 1.0078 ms 1.0083 ms]
                        change: [-30.190% -30.099% -30.022%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  3 (3.00%) high mild
  6 (6.00%) high severe

bench_bool/bench_bool   time:   [371.17 us 377.48 us 384.47 us]                                  
                        thrpt:  [1.2700 GiB/s 1.2935 GiB/s 1.3155 GiB/s]
                 change:
                        time:   [+3.6612% +5.0798% +6.6791%] (p = 0.00 < 0.05)
                        thrpt:  [-6.2609% -4.8342% -3.5319%]
                        Performance has regressed.
Found 13 outliers among 100 measurements (13.00%)
  4 (4.00%) high mild
  9 (9.00%) high severe

bench_primitive/bench_string                                                                             
                        time:   [3.6773 ms 3.6807 ms 3.6841 ms]
                        thrpt:  [1.7230 GiB/s 1.7246 GiB/s 1.7262 GiB/s]
                 change:
                        time:   [-11.764% -11.651% -11.540%] (p = 0.00 < 0.05)
                        thrpt:  [+13.045% +13.188% +13.332%]
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild

@tustvold tustvold merged commit 9c70e4a into apache:master Jul 26, 2022
@ursabot
Copy link

ursabot commented Jul 26, 2022

Benchmark runs are scheduled for baseline = 19fd885 and contender = 9c70e4a. 9c70e4a is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-rs-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

@alamb
Copy link
Contributor

alamb commented Jul 26, 2022

🎉

@alamb
Copy link
Contributor

alamb commented Jul 26, 2022

Thanks for sticking with it @HaoYang670 and for all the help @jhorstmann @viirya and @tustvold 👍

@HaoYang670 HaoYang670 deleted the lazily_materialize_null_buffer_builder branch July 26, 2022 22:46
@HaoYang670
Copy link
Contributor Author

Thanks everyone for your reviewing and helping!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lazily materialize the null buffer builder for all array builders.
7 participants