-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
buffer.rs
1258 lines (1113 loc) · 39.1 KB
/
buffer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
//! The main type in the module is `Buffer`, a contiguous immutable memory region of
//! fixed size aligned at a 64-byte boundary. `MutableBuffer` is like `Buffer`, but it can
//! be mutated and grown.
#[cfg(feature = "simd")]
use packed_simd::u8x64;
use crate::{
bytes::{Bytes, Deallocation},
ffi,
};
use std::cmp;
use std::convert::AsRef;
use std::fmt::Debug;
use std::mem;
use std::ops::{BitAnd, BitOr, Not};
use std::slice::{from_raw_parts, from_raw_parts_mut};
use std::sync::Arc;
#[cfg(feature = "avx512")]
use crate::arch::avx512::*;
use crate::datatypes::ArrowNativeType;
use crate::error::{ArrowError, Result};
use crate::memory;
use crate::util::bit_chunk_iterator::BitChunks;
use crate::util::bit_util;
use crate::util::bit_util::ceil;
#[cfg(any(feature = "simd", feature = "avx512"))]
use std::borrow::BorrowMut;
/// Buffer is a contiguous memory region of fixed size and is aligned at a 64-byte
/// boundary. Buffer is immutable.
#[derive(Clone, PartialEq, Debug)]
pub struct Buffer {
/// Reference-counted pointer to the internal byte buffer.
data: Arc<Bytes>,
/// The offset into the buffer.
offset: usize,
}
impl Buffer {
/// Creates a buffer from an existing memory region (must already be byte-aligned), this
/// `Buffer` will free this piece of memory when dropped.
///
/// # Arguments
///
/// * `ptr` - Pointer to raw parts
/// * `len` - Length of raw parts in **bytes**
/// * `capacity` - Total allocated memory for the pointer `ptr`, in **bytes**
///
/// # Safety
///
/// This function is unsafe as there is no guarantee that the given pointer is valid for `len`
/// bytes. If the `ptr` and `capacity` come from a `Buffer`, then this is guaranteed.
pub unsafe fn from_raw_parts(ptr: *const u8, len: usize, capacity: usize) -> Self {
Buffer::build_with_arguments(ptr, len, Deallocation::Native(capacity))
}
/// Creates a buffer from an existing memory region (must already be byte-aligned), this
/// `Buffer` **does not** free this piece of memory when dropped.
///
/// # Arguments
///
/// * `ptr` - Pointer to raw parts
/// * `len` - Length of raw parts in **bytes**
/// * `data` - An [ffi::FFI_ArrowArray] with the data
///
/// # Safety
///
/// This function is unsafe as there is no guarantee that the given pointer is valid for `len`
/// bytes and that the foreign deallocator frees the region.
pub unsafe fn from_unowned(
ptr: *const u8,
len: usize,
data: Arc<ffi::FFI_ArrowArray>,
) -> Self {
Buffer::build_with_arguments(ptr, len, Deallocation::Foreign(data))
}
/// Auxiliary method to create a new Buffer
unsafe fn build_with_arguments(
ptr: *const u8,
len: usize,
deallocation: Deallocation,
) -> Self {
let bytes = Bytes::new(ptr, len, deallocation);
Buffer {
data: Arc::new(bytes),
offset: 0,
}
}
/// Returns the number of bytes in the buffer
pub fn len(&self) -> usize {
self.data.len() - self.offset
}
/// Returns the capacity of this buffer.
/// For exernally owned buffers, this returns zero
pub fn capacity(&self) -> usize {
self.data.capacity()
}
/// Returns whether the buffer is empty.
pub fn is_empty(&self) -> bool {
self.data.len() - self.offset == 0
}
/// Returns the byte slice stored in this buffer
pub fn data(&self) -> &[u8] {
&self.data.as_slice()[self.offset..]
}
/// Returns a slice of this buffer, starting from `offset`.
pub fn slice(&self, offset: usize) -> Self {
assert!(
offset <= self.len(),
"the offset of the new Buffer cannot exceed the existing length"
);
Self {
data: self.data.clone(),
offset: self.offset + offset,
}
}
/// Returns a raw pointer for this buffer.
///
/// Note that this should be used cautiously, and the returned pointer should not be
/// stored anywhere, to avoid dangling pointers.
pub fn raw_data(&self) -> *const u8 {
unsafe { self.data.raw_data().add(self.offset) }
}
/// View buffer as typed slice.
///
/// # Safety
///
/// `ArrowNativeType` is public so that it can be used as a trait bound for other public
/// components, such as the `ToByteSlice` trait. However, this means that it can be
/// implemented by user defined types, which it is not intended for.
///
/// Also `typed_data::<bool>` is unsafe as `0x00` and `0x01` are the only valid values for
/// `bool` in Rust. However, `bool` arrays in Arrow are bit-packed which breaks this condition.
pub unsafe fn typed_data<T: ArrowNativeType + num::Num>(&self) -> &[T] {
assert_eq!(self.len() % mem::size_of::<T>(), 0);
assert!(memory::is_ptr_aligned::<T>(self.raw_data() as *const T));
from_raw_parts(
self.raw_data() as *const T,
self.len() / mem::size_of::<T>(),
)
}
/// Returns a slice of this buffer starting at a certain bit offset.
/// If the offset is byte-aligned the returned buffer is a shallow clone,
/// otherwise a new buffer is allocated and filled with a copy of the bits in the range.
pub fn bit_slice(&self, offset: usize, len: usize) -> Self {
if offset % 8 == 0 && len % 8 == 0 {
return self.slice(offset / 8);
}
bitwise_unary_op_helper(&self, offset, len, |a| a)
}
/// Returns a `BitChunks` instance which can be used to iterate over this buffers bits
/// in larger chunks and starting at arbitrary bit offsets.
/// Note that both `offset` and `length` are measured in bits.
pub fn bit_chunks(&self, offset: usize, len: usize) -> BitChunks {
BitChunks::new(&self.data.as_slice()[self.offset..], offset, len)
}
/// Returns the number of 1-bits in this buffer.
pub fn count_set_bits(&self) -> usize {
let len_in_bits = self.len() * 8;
// self.offset is already taken into consideration by the bit_chunks implementation
self.count_set_bits_offset(0, len_in_bits)
}
/// Returns the number of 1-bits in this buffer, starting from `offset` with `length` bits
/// inspected. Note that both `offset` and `length` are measured in bits.
pub fn count_set_bits_offset(&self, offset: usize, len: usize) -> usize {
let chunks = self.bit_chunks(offset, len);
let mut count = chunks.iter().map(|c| c.count_ones() as usize).sum();
count += chunks.remainder_bits().count_ones() as usize;
count
}
}
/// Creating a `Buffer` instance by copying the memory from a `AsRef<[u8]>` into a newly
/// allocated memory region.
impl<T: AsRef<[u8]>> From<T> for Buffer {
fn from(p: T) -> Self {
// allocate aligned memory buffer
let slice = p.as_ref();
let len = slice.len() * mem::size_of::<u8>();
let capacity = bit_util::round_upto_multiple_of_64(len);
let buffer = memory::allocate_aligned(capacity);
unsafe {
memory::memcpy(buffer, slice.as_ptr(), len);
Buffer::build_with_arguments(buffer, len, Deallocation::Native(capacity))
}
}
}
/// Apply a bitwise operation `simd_op` / `scalar_op` to two inputs using simd instructions and return the result as a Buffer.
/// The `simd_op` functions gets applied on chunks of 64 bytes (512 bits) at a time
/// and the `scalar_op` gets applied to remaining bytes.
/// Contrary to the non-simd version `bitwise_bin_op_helper`, the offset and length is specified in bytes
/// and this version does not support operations starting at arbitrary bit offsets.
#[cfg(simd_x86)]
fn bitwise_bin_op_simd_helper<F_SIMD, F_SCALAR>(
left: &Buffer,
left_offset: usize,
right: &Buffer,
right_offset: usize,
len: usize,
simd_op: F_SIMD,
scalar_op: F_SCALAR,
) -> Buffer
where
F_SIMD: Fn(u8x64, u8x64) -> u8x64,
F_SCALAR: Fn(u8, u8) -> u8,
{
let mut result = MutableBuffer::new(len).with_bitset(len, false);
let lanes = u8x64::lanes();
let mut left_chunks = left.data()[left_offset..].chunks_exact(lanes);
let mut right_chunks = right.data()[right_offset..].chunks_exact(lanes);
let mut result_chunks = result.data_mut().chunks_exact_mut(lanes);
result_chunks
.borrow_mut()
.zip(left_chunks.borrow_mut().zip(right_chunks.borrow_mut()))
.for_each(|(res, (left, right))| {
unsafe { bit_util::bitwise_bin_op_simd(&left, &right, res, &simd_op) };
});
result_chunks
.into_remainder()
.iter_mut()
.zip(
left_chunks
.remainder()
.iter()
.zip(right_chunks.remainder().iter()),
)
.for_each(|(res, (left, right))| {
*res = scalar_op(*left, *right);
});
result.freeze()
}
/// Apply a bitwise operation `simd_op` / `scalar_op` to one input using simd instructions and return the result as a Buffer.
/// The `simd_op` functions gets applied on chunks of 64 bytes (512 bits) at a time
/// and the `scalar_op` gets applied to remaining bytes.
/// Contrary to the non-simd version `bitwise_unary_op_helper`, the offset and length is specified in bytes
/// and this version does not support operations starting at arbitrary bit offsets.
#[cfg(simd_x86)]
fn bitwise_unary_op_simd_helper<F_SIMD, F_SCALAR>(
left: &Buffer,
left_offset: usize,
len: usize,
simd_op: F_SIMD,
scalar_op: F_SCALAR,
) -> Buffer
where
F_SIMD: Fn(u8x64) -> u8x64,
F_SCALAR: Fn(u8) -> u8,
{
let mut result = MutableBuffer::new(len).with_bitset(len, false);
let lanes = u8x64::lanes();
let mut left_chunks = left.data()[left_offset..].chunks_exact(lanes);
let mut result_chunks = result.data_mut().chunks_exact_mut(lanes);
result_chunks
.borrow_mut()
.zip(left_chunks.borrow_mut())
.for_each(|(res, left)| unsafe {
let data_simd = u8x64::from_slice_unaligned_unchecked(left);
let simd_result = simd_op(data_simd);
simd_result.write_to_slice_unaligned_unchecked(res);
});
result_chunks
.into_remainder()
.iter_mut()
.zip(left_chunks.remainder().iter())
.for_each(|(res, left)| {
*res = scalar_op(*left);
});
result.freeze()
}
/// Apply a bitwise operation `op` to two inputs and return the result as a Buffer.
/// The inputs are treated as bitmaps, meaning that offsets and length are specified in number of bits.
fn bitwise_bin_op_helper<F>(
left: &Buffer,
left_offset_in_bits: usize,
right: &Buffer,
right_offset_in_bits: usize,
len_in_bits: usize,
op: F,
) -> Buffer
where
F: Fn(u64, u64) -> u64,
{
// reserve capacity and set length so we can get a typed view of u64 chunks
let mut result =
MutableBuffer::new(ceil(len_in_bits, 8)).with_bitset(len_in_bits / 64 * 8, false);
let left_chunks = left.bit_chunks(left_offset_in_bits, len_in_bits);
let right_chunks = right.bit_chunks(right_offset_in_bits, len_in_bits);
let result_chunks = result.typed_data_mut::<u64>().iter_mut();
result_chunks
.zip(left_chunks.iter().zip(right_chunks.iter()))
.for_each(|(res, (left, right))| {
*res = op(left, right);
});
let remainder_bytes = ceil(left_chunks.remainder_len(), 8);
let rem = op(left_chunks.remainder_bits(), right_chunks.remainder_bits());
// we are counting its starting from the least significant bit, to to_le_bytes should be correct
let rem = &rem.to_le_bytes()[0..remainder_bytes];
result.extend_from_slice(rem);
result.freeze()
}
/// Apply a bitwise operation `op` to one input and return the result as a Buffer.
/// The input is treated as a bitmap, meaning that offset and length are specified in number of bits.
fn bitwise_unary_op_helper<F>(
left: &Buffer,
offset_in_bits: usize,
len_in_bits: usize,
op: F,
) -> Buffer
where
F: Fn(u64) -> u64,
{
// reserve capacity and set length so we can get a typed view of u64 chunks
let mut result =
MutableBuffer::new(ceil(len_in_bits, 8)).with_bitset(len_in_bits / 64 * 8, false);
let left_chunks = left.bit_chunks(offset_in_bits, len_in_bits);
let result_chunks = result.typed_data_mut::<u64>().iter_mut();
result_chunks
.zip(left_chunks.iter())
.for_each(|(res, left)| {
*res = op(left);
});
let remainder_bytes = ceil(left_chunks.remainder_len(), 8);
let rem = op(left_chunks.remainder_bits());
// we are counting its starting from the least significant bit, to to_le_bytes should be correct
let rem = &rem.to_le_bytes()[0..remainder_bytes];
result.extend_from_slice(rem);
result.freeze()
}
#[cfg(all(target_arch = "x86_64", feature = "avx512"))]
pub(super) fn buffer_bin_and(
left: &Buffer,
left_offset_in_bits: usize,
right: &Buffer,
right_offset_in_bits: usize,
len_in_bits: usize,
) -> Buffer {
if left_offset_in_bits % 8 == 0
&& right_offset_in_bits % 8 == 0
&& len_in_bits % 8 == 0
{
let len = len_in_bits / 8;
let left_offset = left_offset_in_bits / 8;
let right_offset = right_offset_in_bits / 8;
let mut result = MutableBuffer::new(len).with_bitset(len, false);
let mut left_chunks = left.data()[left_offset..].chunks_exact(AVX512_U8X64_LANES);
let mut right_chunks =
right.data()[right_offset..].chunks_exact(AVX512_U8X64_LANES);
let mut result_chunks = result.data_mut().chunks_exact_mut(AVX512_U8X64_LANES);
result_chunks
.borrow_mut()
.zip(left_chunks.borrow_mut().zip(right_chunks.borrow_mut()))
.for_each(|(res, (left, right))| unsafe {
avx512_bin_and(left, right, res);
});
result_chunks
.into_remainder()
.iter_mut()
.zip(
left_chunks
.remainder()
.iter()
.zip(right_chunks.remainder().iter()),
)
.for_each(|(res, (left, right))| {
*res = *left & *right;
});
result.freeze()
} else {
bitwise_bin_op_helper(
&left,
left_offset_in_bits,
right,
right_offset_in_bits,
len_in_bits,
|a, b| a & b,
)
}
}
#[cfg(simd_x86)]
pub(super) fn buffer_bin_and(
left: &Buffer,
left_offset_in_bits: usize,
right: &Buffer,
right_offset_in_bits: usize,
len_in_bits: usize,
) -> Buffer {
if left_offset_in_bits % 8 == 0
&& right_offset_in_bits % 8 == 0
&& len_in_bits % 8 == 0
{
bitwise_bin_op_simd_helper(
&left,
left_offset_in_bits / 8,
&right,
right_offset_in_bits / 8,
len_in_bits / 8,
|a, b| a & b,
|a, b| a & b,
)
} else {
bitwise_bin_op_helper(
&left,
left_offset_in_bits,
right,
right_offset_in_bits,
len_in_bits,
|a, b| a & b,
)
}
}
// Note: do not target specific features like x86 without considering
// other targets like wasm32, as those would fail to build
#[cfg(all(not(any(feature = "simd", feature = "avx512"))))]
pub(super) fn buffer_bin_and(
left: &Buffer,
left_offset_in_bits: usize,
right: &Buffer,
right_offset_in_bits: usize,
len_in_bits: usize,
) -> Buffer {
bitwise_bin_op_helper(
&left,
left_offset_in_bits,
right,
right_offset_in_bits,
len_in_bits,
|a, b| a & b,
)
}
#[cfg(all(target_arch = "x86_64", feature = "avx512"))]
pub(super) fn buffer_bin_or(
left: &Buffer,
left_offset_in_bits: usize,
right: &Buffer,
right_offset_in_bits: usize,
len_in_bits: usize,
) -> Buffer {
if left_offset_in_bits % 8 == 0
&& right_offset_in_bits % 8 == 0
&& len_in_bits % 8 == 0
{
let len = len_in_bits / 8;
let left_offset = left_offset_in_bits / 8;
let right_offset = right_offset_in_bits / 8;
let mut result = MutableBuffer::new(len).with_bitset(len, false);
let mut left_chunks = left.data()[left_offset..].chunks_exact(AVX512_U8X64_LANES);
let mut right_chunks =
right.data()[right_offset..].chunks_exact(AVX512_U8X64_LANES);
let mut result_chunks = result.data_mut().chunks_exact_mut(AVX512_U8X64_LANES);
result_chunks
.borrow_mut()
.zip(left_chunks.borrow_mut().zip(right_chunks.borrow_mut()))
.for_each(|(res, (left, right))| unsafe {
avx512_bin_or(left, right, res);
});
result_chunks
.into_remainder()
.iter_mut()
.zip(
left_chunks
.remainder()
.iter()
.zip(right_chunks.remainder().iter()),
)
.for_each(|(res, (left, right))| {
*res = *left | *right;
});
result.freeze()
} else {
bitwise_bin_op_helper(
&left,
left_offset_in_bits,
right,
right_offset_in_bits,
len_in_bits,
|a, b| a | b,
)
}
}
#[cfg(simd_x86)]
pub(super) fn buffer_bin_or(
left: &Buffer,
left_offset_in_bits: usize,
right: &Buffer,
right_offset_in_bits: usize,
len_in_bits: usize,
) -> Buffer {
if left_offset_in_bits % 8 == 0
&& right_offset_in_bits % 8 == 0
&& len_in_bits % 8 == 0
{
bitwise_bin_op_simd_helper(
&left,
left_offset_in_bits / 8,
&right,
right_offset_in_bits / 8,
len_in_bits / 8,
|a, b| a | b,
|a, b| a | b,
)
} else {
bitwise_bin_op_helper(
&left,
left_offset_in_bits,
right,
right_offset_in_bits,
len_in_bits,
|a, b| a | b,
)
}
}
#[cfg(all(not(any(feature = "simd", feature = "avx512"))))]
pub(super) fn buffer_bin_or(
left: &Buffer,
left_offset_in_bits: usize,
right: &Buffer,
right_offset_in_bits: usize,
len_in_bits: usize,
) -> Buffer {
bitwise_bin_op_helper(
&left,
left_offset_in_bits,
right,
right_offset_in_bits,
len_in_bits,
|a, b| a | b,
)
}
pub(super) fn buffer_unary_not(
left: &Buffer,
offset_in_bits: usize,
len_in_bits: usize,
) -> Buffer {
// SIMD implementation if available and byte-aligned
#[cfg(simd_x86)]
if offset_in_bits % 8 == 0 && len_in_bits % 8 == 0 {
return bitwise_unary_op_simd_helper(
&left,
offset_in_bits / 8,
len_in_bits / 8,
|a| !a,
|a| !a,
);
}
// Default implementation
#[allow(unreachable_code)]
{
bitwise_unary_op_helper(&left, offset_in_bits, len_in_bits, |a| !a)
}
}
impl<'a, 'b> BitAnd<&'b Buffer> for &'a Buffer {
type Output = Result<Buffer>;
fn bitand(self, rhs: &'b Buffer) -> Result<Buffer> {
if self.len() != rhs.len() {
return Err(ArrowError::ComputeError(
"Buffers must be the same size to apply Bitwise AND.".to_string(),
));
}
let len_in_bits = self.len() * 8;
Ok(buffer_bin_and(&self, 0, &rhs, 0, len_in_bits))
}
}
impl<'a, 'b> BitOr<&'b Buffer> for &'a Buffer {
type Output = Result<Buffer>;
fn bitor(self, rhs: &'b Buffer) -> Result<Buffer> {
if self.len() != rhs.len() {
return Err(ArrowError::ComputeError(
"Buffers must be the same size to apply Bitwise OR.".to_string(),
));
}
let len_in_bits = self.len() * 8;
Ok(buffer_bin_or(&self, 0, &rhs, 0, len_in_bits))
}
}
impl Not for &Buffer {
type Output = Buffer;
fn not(self) -> Buffer {
let len_in_bits = self.len() * 8;
buffer_unary_not(&self, 0, len_in_bits)
}
}
unsafe impl Sync for Buffer {}
unsafe impl Send for Buffer {}
/// Similar to `Buffer`, but is growable and can be mutated. A mutable buffer can be
/// converted into a immutable buffer via the `freeze` method.
#[derive(Debug)]
pub struct MutableBuffer {
data: *mut u8,
len: usize,
capacity: usize,
}
impl MutableBuffer {
/// Allocate a new mutable buffer with initial capacity to be `capacity`.
pub fn new(capacity: usize) -> Self {
let new_capacity = bit_util::round_upto_multiple_of_64(capacity);
let ptr = memory::allocate_aligned(new_capacity);
Self {
data: ptr,
len: 0,
capacity: new_capacity,
}
}
/// creates a new [MutableBuffer] where every bit is initialized to `0`
pub fn new_null(len: usize) -> Self {
let num_bytes = bit_util::ceil(len, 8);
MutableBuffer::new(num_bytes).with_bitset(num_bytes, false)
}
/// Set the bits in the range of `[0, end)` to 0 (if `val` is false), or 1 (if `val`
/// is true). Also extend the length of this buffer to be `end`.
///
/// This is useful when one wants to clear (or set) the bits and then manipulate
/// the buffer directly (e.g., modifying the buffer by holding a mutable reference
/// from `data_mut()`).
pub fn with_bitset(mut self, end: usize, val: bool) -> Self {
assert!(end <= self.capacity);
let v = if val { 255 } else { 0 };
unsafe {
std::ptr::write_bytes(self.data, v, end);
self.len = end;
}
self
}
/// Ensure that `count` bytes from `start` contain zero bits
///
/// This is used to initialize the bits in a buffer, however, it has no impact on the
/// `len` of the buffer and so can be used to initialize the memory region from
/// `len` to `capacity`.
pub fn set_null_bits(&mut self, start: usize, count: usize) {
assert!(start + count <= self.capacity);
unsafe {
std::ptr::write_bytes(self.data.add(start), 0, count);
}
}
/// Ensures that this buffer has at least `capacity` slots in this buffer. This will
/// also ensure the new capacity will be a multiple of 64 bytes.
///
/// Returns the new capacity for this buffer.
pub fn reserve(&mut self, capacity: usize) -> usize {
if capacity > self.capacity {
let new_capacity = bit_util::round_upto_multiple_of_64(capacity);
let new_capacity = cmp::max(new_capacity, self.capacity * 2);
let new_data =
unsafe { memory::reallocate(self.data, self.capacity, new_capacity) };
self.data = new_data as *mut u8;
self.capacity = new_capacity;
}
self.capacity
}
/// Resizes the buffer so that the `len` will equal to the `new_len`.
///
/// If `new_len` is greater than `len`, the buffer's length is simply adjusted to be
/// the former, optionally extending the capacity. The data between `len` and
/// `new_len` will be zeroed out.
///
/// If `new_len` is less than `len`, the buffer will be truncated.
pub fn resize(&mut self, new_len: usize) {
if new_len > self.len {
self.reserve(new_len);
} else {
let new_capacity = bit_util::round_upto_multiple_of_64(new_len);
if new_capacity < self.capacity {
let new_data =
unsafe { memory::reallocate(self.data, self.capacity, new_capacity) };
self.data = new_data as *mut u8;
self.capacity = new_capacity;
}
}
self.len = new_len;
}
/// Returns whether this buffer is empty or not.
#[inline]
pub const fn is_empty(&self) -> bool {
self.len == 0
}
/// Returns the length (the number of bytes written) in this buffer.
#[inline]
pub const fn len(&self) -> usize {
self.len
}
/// Returns the total capacity in this buffer.
#[inline]
pub const fn capacity(&self) -> usize {
self.capacity
}
/// Clear all existing data from this buffer.
pub fn clear(&mut self) {
self.len = 0
}
/// Returns the data stored in this buffer as a slice.
pub fn data(&self) -> &[u8] {
if self.data.is_null() {
&[]
} else {
unsafe { std::slice::from_raw_parts(self.raw_data(), self.len()) }
}
}
/// Returns the data stored in this buffer as a mutable slice.
pub fn data_mut(&mut self) -> &mut [u8] {
if self.data.is_null() {
&mut []
} else {
unsafe { std::slice::from_raw_parts_mut(self.raw_data_mut(), self.len()) }
}
}
/// Returns a raw pointer for this buffer.
///
/// Note that this should be used cautiously, and the returned pointer should not be
/// stored anywhere, to avoid dangling pointers.
#[inline]
pub const fn raw_data(&self) -> *const u8 {
self.data
}
#[inline]
pub fn raw_data_mut(&mut self) -> *mut u8 {
self.data
}
/// Freezes this buffer and return an immutable version of it.
pub fn freeze(self) -> Buffer {
let buffer_data = unsafe {
Bytes::new(self.data, self.len, Deallocation::Native(self.capacity))
};
std::mem::forget(self);
Buffer {
data: Arc::new(buffer_data),
offset: 0,
}
}
/// View buffer as typed slice.
pub fn typed_data_mut<T: ArrowNativeType>(&mut self) -> &mut [T] {
assert_eq!(self.len() % mem::size_of::<T>(), 0);
assert!(memory::is_ptr_aligned::<T>(self.raw_data() as *const T));
unsafe {
from_raw_parts_mut(
self.raw_data() as *mut T,
self.len() / mem::size_of::<T>(),
)
}
}
/// Extends the buffer from a byte slice, incrementing its capacity if needed.
#[inline]
pub fn extend_from_slice(&mut self, bytes: &[u8]) {
let new_len = self.len + bytes.len();
if new_len > self.capacity {
self.reserve(new_len);
}
unsafe {
memory::memcpy(self.data.add(self.len), bytes.as_ptr(), bytes.len());
}
self.len = new_len;
}
/// Extends the buffer by `len` with all bytes equal to `0u8`, incrementing its capacity if needed.
pub fn extend(&mut self, len: usize) {
let remaining_capacity = self.capacity - self.len;
if len > remaining_capacity {
self.reserve(self.len + len);
}
self.len += len;
}
}
impl Drop for MutableBuffer {
fn drop(&mut self) {
if !self.data.is_null() {
unsafe { memory::free_aligned(self.data, self.capacity) };
}
}
}
impl PartialEq for MutableBuffer {
fn eq(&self, other: &MutableBuffer) -> bool {
if self.len != other.len {
return false;
}
if self.capacity != other.capacity {
return false;
}
unsafe { memory::memcmp(self.data, other.data, self.len) == 0 }
}
}
unsafe impl Sync for MutableBuffer {}
unsafe impl Send for MutableBuffer {}
#[cfg(test)]
mod tests {
use std::ptr::null_mut;
use std::thread;
use super::*;
use crate::datatypes::ToByteSlice;
#[test]
fn test_buffer_data_equality() {
let buf1 = Buffer::from(&[0, 1, 2, 3, 4]);
let buf2 = Buffer::from(&[0, 1, 2, 3, 4]);
assert_eq!(buf1, buf2);
// slice with same offset should still preserve equality
let buf3 = buf1.slice(2);
assert_ne!(buf1, buf3);
let buf4 = buf2.slice(2);
assert_eq!(buf3, buf4);
// Different capacities should still preserve equality
let mut buf2 = MutableBuffer::new(65);
buf2.extend_from_slice(&[0, 1, 2, 3, 4]);
let buf2 = buf2.freeze();
assert_eq!(buf1, buf2);
// unequal because of different elements
let buf2 = Buffer::from(&[0, 0, 2, 3, 4]);
assert_ne!(buf1, buf2);
// unequal because of different length
let buf2 = Buffer::from(&[0, 1, 2, 3]);
assert_ne!(buf1, buf2);
}
#[test]
fn test_from_raw_parts() {
let buf = unsafe { Buffer::from_raw_parts(null_mut(), 0, 0) };
assert_eq!(0, buf.len());
assert_eq!(0, buf.data().len());
assert_eq!(0, buf.capacity());
assert!(buf.raw_data().is_null());
let buf = Buffer::from(&[0, 1, 2, 3, 4]);
assert_eq!(5, buf.len());
assert!(!buf.raw_data().is_null());
assert_eq!([0, 1, 2, 3, 4], buf.data());
}
#[test]
fn test_from_vec() {
let buf = Buffer::from(&[0, 1, 2, 3, 4]);
assert_eq!(5, buf.len());
assert!(!buf.raw_data().is_null());
assert_eq!([0, 1, 2, 3, 4], buf.data());
}
#[test]
fn test_copy() {
let buf = Buffer::from(&[0, 1, 2, 3, 4]);
let buf2 = buf;
assert_eq!(5, buf2.len());
assert_eq!(64, buf2.capacity());
assert!(!buf2.raw_data().is_null());
assert_eq!([0, 1, 2, 3, 4], buf2.data());
}
#[test]
fn test_slice() {
let buf = Buffer::from(&[2, 4, 6, 8, 10]);
let buf2 = buf.slice(2);
assert_eq!([6, 8, 10], buf2.data());
assert_eq!(3, buf2.len());
assert_eq!(unsafe { buf.raw_data().offset(2) }, buf2.raw_data());
let buf3 = buf2.slice(1);
assert_eq!([8, 10], buf3.data());
assert_eq!(2, buf3.len());
assert_eq!(unsafe { buf.raw_data().offset(3) }, buf3.raw_data());
let buf4 = buf.slice(5);
let empty_slice: [u8; 0] = [];
assert_eq!(empty_slice, buf4.data());
assert_eq!(0, buf4.len());
assert!(buf4.is_empty());
assert_eq!(buf2.slice(2).data(), &[10]);
}
#[test]
#[should_panic(
expected = "the offset of the new Buffer cannot exceed the existing length"
)]
fn test_slice_offset_out_of_bound() {
let buf = Buffer::from(&[2, 4, 6, 8, 10]);
buf.slice(6);
}
#[test]
fn test_with_bitset() {
let mut_buf = MutableBuffer::new(64).with_bitset(64, false);
let buf = mut_buf.freeze();
assert_eq!(0, buf.count_set_bits());
let mut_buf = MutableBuffer::new(64).with_bitset(64, true);
let buf = mut_buf.freeze();
assert_eq!(512, buf.count_set_bits());
}
#[test]
fn test_set_null_bits() {
let mut mut_buf = MutableBuffer::new(64).with_bitset(64, true);
mut_buf.set_null_bits(0, 64);
let buf = mut_buf.freeze();
assert_eq!(0, buf.count_set_bits());