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

Migrate StaticDataProvider and BlobDataProvider to ZeroMap #1058

Merged
merged 7 commits into from
Nov 23, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions provider/blob/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ icu_locid = { version = "0.4", path = "../../components/locid", features = ["ser
serde = { version = "1.0", default-features = false, features = ["alloc"] }
postcard = { version = "0.7.0", default-features = false }
erased-serde = { version = "0.3", default-features = false, features = ["alloc"] }
litemap = { version = "0.2.0", path = "../../utils/litemap/", features = ["serde", "serde_serialize"] }
writeable = { version = "0.2", path = "../../utils/writeable" }
yoke = { version = "0.3.1", path = "../../utils/yoke" }
zerovec = { version = "0.5", path = "../../utils/zerovec", features = ["serde", "yoke"] }

# For the export feature
log = { version = "0.4", optional = true }
litemap = { version = "0.2.0", path = "../../utils/litemap/", optional = true }

[dev-dependencies]
icu = { version = "0.4", path = "../../components/icu" }
Expand All @@ -48,5 +49,5 @@ icu_locid_macros = { version = "0.4", path = "../../components/locid/macros" }
path = "src/lib.rs"

[features]
export = ["log", "postcard/alloc", "std"]
export = ["log", "postcard/alloc", "std", "litemap"]
std = ["icu_locid/std", "icu_provider/std"]
24 changes: 13 additions & 11 deletions provider/blob/src/blob_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use icu_provider::serde::{SerdeDeDataProvider, SerdeDeDataReceiver};
use serde::de::Deserialize;
use yoke::trait_hack::YokeTraitHack;
use yoke::*;
use zerovec::map::ZeroMapBorrowed;

/// A data provider loading data from blobs dynamically created at runtime.
///
Expand Down Expand Up @@ -62,15 +63,20 @@ use yoke::*;
///
/// [`StaticDataProvider`]: crate::StaticDataProvider
pub struct BlobDataProvider {
blob: Yoke<BlobSchema<'static>, Rc<[u8]>>,
data: Yoke<ZeroMapBorrowed<'static, str, [u8]>, Rc<[u8]>>,
}

impl BlobDataProvider {
/// Create a [`BlobDataProvider`] from an `Rc` blob of ICU4X data.
pub fn new_from_rc_blob(blob: Rc<[u8]>) -> Result<Self, DataError> {
Ok(BlobDataProvider {
blob: Yoke::try_attach_to_cart_badly(blob, |bytes| {
BlobSchema::deserialize(&mut postcard::Deserializer::from_bytes(bytes))
data: Yoke::try_attach_to_cart_badly(blob, |bytes| {
BlobSchema::deserialize(&mut postcard::Deserializer::from_bytes(bytes)).map(
|blob| {
let BlobSchema::V001(blob) = blob;
blob.resources
},
)
})
.map_err(DataError::new_resc_error)?,
})
Expand All @@ -80,14 +86,10 @@ impl BlobDataProvider {
/// to the buffer backing the BlobSchema.
fn get_file(&self, req: &DataRequest) -> Result<Yoke<&'static [u8], Rc<[u8]>>, DataError> {
let path = path_util::resource_path_to_string(&req.resource_path);
self.blob
.try_project_cloned_with_capture::<&'static [u8], String, ()>(
path,
move |blob, path, _| {
let BlobSchema::V001(blob) = blob;
blob.resources.get(&*path).ok_or(()).map(|v| *v)
},
)
self.data
.try_project_cloned_with_capture::<&'static [u8], String, ()>(path, |zm, path, _| {
zm.get(&path).ok_or(())
})
.map_err(|_| DataError::MissingResourceKey(req.resource_path.key))
}
}
Expand Down
7 changes: 3 additions & 4 deletions provider/blob/src/blob_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use litemap::LiteMap;
use zerovec::map::ZeroMapBorrowed;

/// A versioned Serde schema for ICU4X data blobs.
#[derive(serde::Serialize, serde::Deserialize, yoke::Yokeable)]
#[derive(serde::Serialize, serde::Deserialize)]
pub enum BlobSchema<'data> {
#[serde(borrow)]
V001(BlobSchemaV1<'data>),
Expand All @@ -14,7 +14,6 @@ pub enum BlobSchema<'data> {
/// Version 1 of the ICU4X data blob schema.
#[derive(serde::Serialize, serde::Deserialize)]
pub struct BlobSchemaV1<'data> {
// TODO(#829): Use ZeroMap instead of LiteMap.
#[serde(borrow)]
pub resources: LiteMap<&'data str, &'data [u8]>,
pub resources: ZeroMapBorrowed<'data, str, [u8]>,
}
17 changes: 7 additions & 10 deletions provider/blob/src/export/blob_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use icu_provider::export::DataExporter;
use icu_provider::prelude::*;
use icu_provider::serde::SerdeSeDataStructMarker;
use litemap::LiteMap;
use zerovec::ZeroMap;

/// A data exporter that writes data to a single-file blob.
/// See the module-level docs for an example.
Expand Down Expand Up @@ -57,18 +58,14 @@ impl DataExporter<SerdeSeDataStructMarker> for BlobExporter<'_> {
}

fn close(&mut self) -> Result<(), DataError> {
// Convert from LiteMap<String, Vec> to LiteMap<&str, &[]>
let mut schema = BlobSchemaV1 {
resources: LiteMap::with_capacity(self.resources.len()),
};
// Convert from LiteMap<String, Vec<u8>> to ZeroVecBorrowed<&str, &[u8]>
let mut zm: ZeroMap<str, [u8]> = ZeroMap::with_capacity(self.resources.len());
for (k, v) in self.resources.iter() {
schema
.resources
.try_append(k, v)
.ok_or(())
.expect_err("Same order");
zm.try_append(k, v).ok_or(()).expect_err("Same order");
}
let blob = BlobSchema::V001(schema);
let blob = BlobSchema::V001(BlobSchemaV1 {
resources: zm.as_borrowed(),
});
log::info!("Serializing blob to output stream...");
let vec = serialize(&blob)?;
self.sink.write(&vec).map_err(|e| e.to_string())?;
Expand Down
20 changes: 10 additions & 10 deletions provider/blob/src/static_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::blob_schema::*;
use crate::path_util;
use icu_provider::prelude::*;
use icu_provider::serde::{SerdeDeDataProvider, SerdeDeDataReceiver};
use litemap::LiteMap;
use serde::de::Deserialize;
use zerovec::map::ZeroMapBorrowed;

/// A data provider loading data statically baked in to the binary.
///
Expand Down Expand Up @@ -52,14 +52,18 @@ use serde::de::Deserialize;
///
/// [`BlobDataProvider`]: crate::BlobDataProvider
pub struct StaticDataProvider {
blob: BlobSchema<'static>,
data: ZeroMapBorrowed<'static, str, [u8]>,
}

impl StaticDataProvider {
/// Create a [`StaticDataProvider`] from a `'static` blob of ICU4X data.
pub fn new_from_static_blob(blob: &'static [u8]) -> Result<Self, DataError> {
Ok(StaticDataProvider {
blob: BlobSchema::deserialize(&mut postcard::Deserializer::from_bytes(blob))
data: BlobSchema::deserialize(&mut postcard::Deserializer::from_bytes(blob))
.map(|blob| {
let BlobSchema::V001(blob) = blob;
blob.resources
})
.map_err(DataError::new_resc_error)?,
})
}
Expand Down Expand Up @@ -91,19 +95,15 @@ impl StaticDataProvider {
/// ```
pub fn new_empty() -> Self {
StaticDataProvider {
blob: BlobSchema::V001(BlobSchemaV1 {
resources: LiteMap::new(),
}),
data: ZeroMapBorrowed::new(),
}
}

fn get_file(&self, req: &DataRequest) -> Result<&'static [u8], DataError> {
let path = path_util::resource_path_to_string(&req.resource_path);
let BlobSchema::V001(blob) = &self.blob;
blob.resources
.get(&*path)
self.data
.get(&path)
.ok_or(DataError::MissingResourceKey(req.resource_path.key))
.map(|v| *v)
}
}

Expand Down
Binary file modified provider/blob/tests/data/hello_world.postcard
Binary file not shown.
Binary file modified provider/testdata/data/decimal-bn-en.postcard
Binary file not shown.
Binary file modified provider/testdata/data/testdata.postcard
Binary file not shown.