Skip to content

Commit

Permalink
Merge pull request #91 from Kerollmops/moving-to-bytemuck
Browse files Browse the repository at this point in the history
Moving to bytemuck
  • Loading branch information
Kerollmops authored Jan 23, 2021
2 parents e7cd9f1 + 6045020 commit 23828b8
Show file tree
Hide file tree
Showing 18 changed files with 521 additions and 235 deletions.
24 changes: 24 additions & 0 deletions Fushia_LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright 2019 The Fuchsia Authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# heed
A fully typed [LMDB]/[MDBX] wrapper with minimum overhead, uses zerocopy internally.
A fully typed [LMDB]/[MDBX] wrapper with minimum overhead, uses bytemuck internally.

[![Build Status](https://dev.azure.com/renaultcle/heed/_apis/build/status/Kerollmops.heed?branchName=master)](https://dev.azure.com/renaultcle/heed/_build/latest?definitionId=1&branchName=master)
[![Dependency Status](https://deps.rs/repo/github/Kerollmops/heed/status.svg)](https://deps.rs/repo/github/Kerollmops/heed)
Expand Down
6 changes: 5 additions & 1 deletion heed-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ edition = "2018"

[dependencies]
bincode = { version = "1.2.1", optional = true }
bytemuck = { version = "1.5.0", features = ["extern_crate_alloc"] }
byteorder = "1.4.2"
heed-traits = { version = "0.7.0", path = "../heed-traits" }
serde = { version = "1.0.117", optional = true }
serde_json = { version = "1.0.59", optional = true }
zerocopy = "0.3.0"

[dev-dependencies]
rand = "0.8.2"

[features]
default = ["serde-bincode", "serde-json"]
Expand Down
43 changes: 8 additions & 35 deletions heed-types/src/cow_slice.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::borrow::Cow;
use std::{mem, ptr};

use crate::aligned_to;
use bytemuck::{Pod, PodCastError, try_cast_slice, pod_collect_to_vec};
use heed_traits::{BytesDecode, BytesEncode};
use zerocopy::{AsBytes, FromBytes, LayoutVerified};

/// Describes a slice that must be [memory aligned] and
/// will be reallocated if it is not.
Expand All @@ -22,47 +20,22 @@ use zerocopy::{AsBytes, FromBytes, LayoutVerified};
/// [`OwnedSlice`]: crate::OwnedSlice
pub struct CowSlice<T>(std::marker::PhantomData<T>);

impl<'a, T: 'a> BytesEncode<'a> for CowSlice<T>
where
T: AsBytes,
{
impl<'a, T: Pod> BytesEncode<'a> for CowSlice<T> {
type EItem = [T];

fn bytes_encode(item: &'a Self::EItem) -> Option<Cow<[u8]>> {
Some(Cow::Borrowed(<[T] as AsBytes>::as_bytes(item)))
try_cast_slice(item).map(Cow::Borrowed).ok()
}
}

impl<'a, T: 'a> BytesDecode<'a> for CowSlice<T>
where
T: FromBytes + Copy,
{
impl<'a, T: Pod> BytesDecode<'a> for CowSlice<T> {
type DItem = Cow<'a, [T]>;

fn bytes_decode(bytes: &'a [u8]) -> Option<Self::DItem> {
match LayoutVerified::<_, [T]>::new_slice(bytes) {
Some(layout) => Some(Cow::Borrowed(layout.into_slice())),
None => {
let len = bytes.len();
let elem_size = mem::size_of::<T>();

// ensure that it is the alignment that is wrong
// and the length is valid
if len % elem_size == 0 && !aligned_to(bytes, mem::align_of::<T>()) {
let elems = len / elem_size;
let mut vec = Vec::<T>::with_capacity(elems);

unsafe {
let dst = vec.as_mut_ptr() as *mut u8;
ptr::copy_nonoverlapping(bytes.as_ptr(), dst, len);
vec.set_len(elems);
}

return Some(Cow::Owned(vec));
}

None
}
match try_cast_slice(bytes) {
Ok(items) => Some(Cow::Borrowed(items)),
Err(PodCastError::AlignmentMismatch) => Some(Cow::Owned(pod_collect_to_vec(bytes))),
Err(_) => None,
}
}
}
Expand Down
44 changes: 12 additions & 32 deletions heed-types/src/cow_type.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::borrow::Cow;
use std::{mem, ptr};

use crate::aligned_to;
use heed_traits::{BytesDecode, BytesEncode};
use zerocopy::{AsBytes, FromBytes, LayoutVerified};
use bytemuck::{Pod, PodCastError, bytes_of, bytes_of_mut, try_from_bytes};

/// Describes a type that must be [memory aligned] and
/// will be reallocated if it is not.
Expand All @@ -28,44 +26,26 @@ use zerocopy::{AsBytes, FromBytes, LayoutVerified};
/// [`CowSlice`]: crate::CowSlice
pub struct CowType<T>(std::marker::PhantomData<T>);

impl<'a, T: 'a> BytesEncode<'a> for CowType<T>
where
T: AsBytes,
{
impl<'a, T: Pod> BytesEncode<'a> for CowType<T> {
type EItem = T;

fn bytes_encode(item: &'a Self::EItem) -> Option<Cow<[u8]>> {
Some(Cow::Borrowed(<T as AsBytes>::as_bytes(item)))
Some(Cow::Borrowed(bytes_of(item)))
}
}

impl<'a, T: 'a> BytesDecode<'a> for CowType<T>
where
T: FromBytes + Copy,
{
impl<'a, T: Pod> BytesDecode<'a> for CowType<T> {
type DItem = Cow<'a, T>;

fn bytes_decode(bytes: &'a [u8]) -> Option<Self::DItem> {
match LayoutVerified::<_, T>::new(bytes) {
Some(layout) => Some(Cow::Borrowed(layout.into_ref())),
None => {
let len = bytes.len();
let elem_size = mem::size_of::<T>();

// ensure that it is the alignment that is wrong
// and the length is valid
if len == elem_size && !aligned_to(bytes, mem::align_of::<T>()) {
let mut data = mem::MaybeUninit::<T>::uninit();

unsafe {
let dst = data.as_mut_ptr() as *mut u8;
ptr::copy_nonoverlapping(bytes.as_ptr(), dst, len);
return Some(Cow::Owned(data.assume_init()));
}
}

None
}
match try_from_bytes(bytes) {
Ok(item) => Some(Cow::Borrowed(item)),
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned) => {
let mut item = T::zeroed();
bytes_of_mut(&mut item).copy_from_slice(bytes);
Some(Cow::Owned(item))
},
Err(_) => None,
}
}
}
Expand Down
Loading

0 comments on commit 23828b8

Please sign in to comment.