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

Avoid double utf8 checks on MutableUtf8 -> Utf8 #655

Merged
merged 2 commits into from
Dec 2, 2021
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
toolchain: nightly-2021-10-24
override: true
- uses: Swatinem/rust-cache@v1
with:
key: key1
- name: Install Miri
run: |
rustup component add miri
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ jobs:
toolchain: nightly-2021-10-24
override: true
- uses: Swatinem/rust-cache@v1
with:
key: key1
- name: Install Miri
run: |
rustup component add miri
Expand All @@ -96,6 +98,8 @@ jobs:
toolchain: nightly-2021-10-24
override: true
- uses: Swatinem/rust-cache@v1
with:
key: key1
- name: Install Miri
run: |
rustup component add miri
Expand Down
36 changes: 23 additions & 13 deletions src/array/utf8/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,31 @@ impl<O: Offset> MutableArray for MutableUtf8Array<O> {
}

fn as_box(&mut self) -> Box<dyn Array> {
Box::new(Utf8Array::from_data(
Self::default_data_type(),
std::mem::take(&mut self.offsets).into(),
std::mem::take(&mut self.values).into(),
std::mem::take(&mut self.validity).map(|x| x.into()),
))
// Safety:
// `MutableUtf8Array` has the same invariants as `Utf8Array` and thus
// `Utf8Array` can be safely created from `MutableUtf8Array` without checks.
Box::new(unsafe {
Utf8Array::from_data_unchecked(
self.data_type.clone(),
std::mem::take(&mut self.offsets).into(),
std::mem::take(&mut self.values).into(),
std::mem::take(&mut self.validity).map(|x| x.into()),
)
})
}

fn as_arc(&mut self) -> Arc<dyn Array> {
Arc::new(Utf8Array::from_data(
Self::default_data_type(),
std::mem::take(&mut self.offsets).into(),
std::mem::take(&mut self.values).into(),
std::mem::take(&mut self.validity).map(|x| x.into()),
))
// Safety:
// `MutableUtf8Array` has the same invariants as `Utf8Array` and thus
// `Utf8Array` can be safely created from `MutableUtf8Array` without checks.
Arc::new(unsafe {
Utf8Array::from_data_unchecked(
self.data_type.clone(),
std::mem::take(&mut self.offsets).into(),
std::mem::take(&mut self.values).into(),
std::mem::take(&mut self.validity).map(|x| x.into()),
)
})
}

fn data_type(&self) -> &DataType {
Expand Down Expand Up @@ -359,7 +369,7 @@ impl<O: Offset> MutableUtf8Array<O> {
Self::from_data_unchecked(Self::default_data_type(), offsets, values, None)
}

/// Creates a new [`Utf8Array`] from a [`TrustedLen`] of `&str`.
/// Creates a new [`MutableUtf8Array`] from a [`TrustedLen`] of `&str`.
#[inline]
pub fn from_trusted_len_values_iter<T: AsRef<str>, I: TrustedLen<Item = T>>(
iterator: I,
Expand Down