Skip to content

Commit

Permalink
Merge branch 'main' into i/1791/calendar-benches
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewpollack committed May 20, 2022
2 parents e24613f + 6ef7eda commit 3b00440
Show file tree
Hide file tree
Showing 42 changed files with 424 additions and 617 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,8 @@ jobs:
# icu_benchmark_memory cli.
examples:
- >-
icu_calendar/iso_date_manipulations
icu_calendar/iso_datetime_manipulations
icu_datetime/work_log
icu_locid/syntatically_canonicalize_locales
icu_locid/filter_langids
Expand Down
3 changes: 1 addition & 2 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions components/calendar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ include = [

[features]
std = []
bench = []
serde = ["dep:serde", "zerovec/serde", "tinystr/serde", "icu_provider/serde"]
datagen = ["serde", "zerovec/serde_serialize"]

Expand All @@ -47,6 +48,7 @@ zerovec = { version = "0.7", path = "../../utils/zerovec", default-features = fa
[dev-dependencies]
criterion = "0.3"
icu = { path = "../icu", default-features = false }
icu_benchmark_macros = { version = "0.6", path = "../../tools/benchmark/macros" }
icu_calendar = { version = "0.6", path = "../calendar", features = ["serde"] } # Dependency required to prevent `delayed_good_path_bugs` error (https://github.com/unicode-org/icu4x/pull/1844#issuecomment-1118111564)
icu_testdata = { version = "0.6", path = "../../provider/testdata" }
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -59,3 +61,9 @@ harness = false
[[bench]]
name = "datetime"
harness = false

[[example]]
name = "iso_date_manipulations"

[[example]]
name = "iso_datetime_manipulations"
62 changes: 62 additions & 0 deletions components/calendar/examples/iso_date_manipulations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

// An example application which uses icu_datetime to format entries
// from a log into human readable dates and times.

#![no_main] // https://github.com/unicode-org/icu4x/issues/395

icu_benchmark_macros::static_setup!();

use icu_calendar::{Calendar, Date, DateTimeError, Iso};

const DATES_ISO: &[(i32, u8, u8)] = &[
(1970, 1, 1),
(1982, 3, 11),
(1999, 2, 21),
(2000, 12, 29),
(2001, 9, 8),
(2017, 7, 12),
(2020, 2, 29),
(2021, 3, 21),
(2021, 6, 10),
(2021, 9, 2),
(2022, 10, 8),
(2022, 2, 9),
(2033, 6, 10),
];

fn print<A: Calendar>(_date_input: &Date<A>) {
#[cfg(debug_assertions)]
{
let formatted_date = format!(
"Year: {}, Month: {}, Day: {}",
_date_input.year().number,
_date_input.month().number,
_date_input.day_of_month().0,
);

println!("{}", formatted_date);
}
}

fn tuple_to_iso_date(date: (i32, u8, u8)) -> Result<Date<Iso>, DateTimeError> {
Date::new_iso_date_from_integers(date.0, date.1, date.2)
}

#[no_mangle]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
icu_benchmark_macros::main_setup!();

let dates = DATES_ISO
.iter()
.copied()
.map(tuple_to_iso_date)
.collect::<Result<Vec<Date<Iso>>, _>>()
.expect("Failed to parse dates.");

dates.iter().map(print).for_each(drop);

0
}
65 changes: 65 additions & 0 deletions components/calendar/examples/iso_datetime_manipulations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

// An example application which uses icu_datetime to format entries
// from a log into human readable dates and times.

#![no_main] // https://github.com/unicode-org/icu4x/issues/395

icu_benchmark_macros::static_setup!();

use icu_calendar::{Calendar, DateTime, DateTimeError, Iso};

const DATETIMES_ISO: &[(i32, u8, u8, u8, u8, u8)] = &[
(1970, 1, 1, 3, 5, 12),
(1982, 3, 11, 2, 25, 59),
(1999, 2, 21, 13, 12, 23),
(2000, 12, 29, 10, 50, 23),
(2001, 9, 8, 11, 5, 5),
(2017, 7, 12, 3, 1, 1),
(2020, 2, 29, 23, 12, 23),
(2021, 3, 21, 18, 35, 34),
(2021, 6, 10, 13, 12, 23),
(2021, 9, 2, 5, 50, 22),
(2022, 10, 8, 9, 45, 32),
(2022, 2, 9, 10, 32, 45),
(2033, 6, 10, 17, 22, 22),
];

fn print<A: Calendar>(_datetime_input: &DateTime<A>) {
#[cfg(debug_assertions)]
{
let formatted_datetime = format!(
"Year: {}, Month: {}, Day: {}, Hour: {}, Minute: {}, Second: {}",
_datetime_input.date.year().number,
_datetime_input.date.month().number,
_datetime_input.date.day_of_month().0,
u8::from(_datetime_input.time.hour),
u8::from(_datetime_input.time.minute),
u8::from(_datetime_input.time.second),
);

println!("{}", formatted_datetime);
}
}

fn tuple_to_iso_datetime(date: (i32, u8, u8, u8, u8, u8)) -> Result<DateTime<Iso>, DateTimeError> {
DateTime::new_iso_datetime_from_integers(date.0, date.1, date.2, date.3, date.4, date.5)
}

#[no_mangle]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
icu_benchmark_macros::main_setup!();

let datetimes = DATETIMES_ISO
.iter()
.copied()
.map(tuple_to_iso_datetime)
.collect::<Result<Vec<DateTime<Iso>>, _>>()
.expect("Failed to parse datetimes.");

datetimes.iter().map(print).for_each(drop);

0
}
2 changes: 0 additions & 2 deletions provider/blob/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ zerovec = { version = "0.7", path = "../../utils/zerovec", features = ["serde",
# For the export feature
log = { version = "0.4", optional = true }
litemap = { version = "0.4", path = "../../utils/litemap/", optional = true }
erased-serde = { version = "0.3", default-features = false, features = ["alloc"], optional = true }

[dev-dependencies]
icu_locid = { version = "0.6", path = "../../components/locid", features = ["serde"] }
Expand All @@ -54,6 +53,5 @@ export = [
"litemap",
"icu_provider/datagen",
"zerovec/serde_serialize",
"erased-serde",
]
std = ["icu_provider/std"]
2 changes: 1 addition & 1 deletion provider/blob/src/export/blob_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl DataExporter<SerializeMarker> for BlobExporter<'_> {
let mut serializer = postcard::Serializer {
output: postcard::flavors::AllocVec(Vec::new()),
};
payload.serialize(&mut <dyn erased_serde::Serializer>::erase(&mut serializer))?;
payload.serialize(&mut serializer)?;
self.resources.lock().unwrap().push((
key.get_hash(),
options.write_to_string().into_owned().into_bytes(),
Expand Down
2 changes: 1 addition & 1 deletion provider/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ deserialize_bincode_1 = ["serde", "bincode", "std"]
deserialize_postcard_07 = ["serde", "postcard"]

# Dependencies for running data generation
datagen = ["dhat", "serde", "erased-serde", "crabbake", "std"]
datagen = ["dhat", "serde", "erased-serde", "crabbake", "std", "serde_json"]

[dependencies]
icu_locid = { version = "0.6", path = "../../components/locid" }
Expand Down
20 changes: 20 additions & 0 deletions provider/core/src/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,23 @@ pub enum BufferFormat {
/// Serialize using Postcard version 0.7.
Postcard07,
}

impl BufferFormat {
/// Returns an error if the buffer format is not enabled.
pub fn check_available(&self) -> Result<(), DataError> {
match self {
#[cfg(feature = "deserialize_json")]
BufferFormat::Json => Ok(()),

#[cfg(feature = "deserialize_bincode_1")]
BufferFormat::Bincode1 => Ok(()),

#[cfg(feature = "deserialize_postcard_07")]
BufferFormat::Postcard07 => Ok(()),

// Allowed for cases in which all features are enabled
#[allow(unreachable_patterns)]
_ => Err(DataErrorKind::UnavailableBufferFormat(*self).into_error()),
}
}
}
41 changes: 7 additions & 34 deletions provider/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::buf::BufferFormat;
use crate::prelude::*;
use displaydoc::Display;

Expand Down Expand Up @@ -77,6 +78,11 @@ pub enum DataErrorKind {
#[displaydoc("Missing source data")]
#[cfg(feature = "datagen")]
MissingSourceData,

/// An error indicating that the desired buffer format is not available. This usually
/// means that a required feature was not enabled
#[displaydoc("Unavailable buffer format: {0:?} (does icu_provider need to be compiled with an additional feature?)")]
UnavailableBufferFormat(BufferFormat),
}

/// The error type for ICU4X data provider operations.
Expand Down Expand Up @@ -224,25 +230,12 @@ impl DataError {
/// it will print out the context.
#[cfg(feature = "std")]
#[cfg_attr(not(feature = "log_error_context"), allow(unused_variables))]
pub fn with_path<P: AsRef<std::path::Path> + ?Sized>(self, path: &P) -> Self {
pub fn with_path_context<P: AsRef<std::path::Path> + ?Sized>(self, path: &P) -> Self {
#[cfg(feature = "log_error_context")]
log::warn!("{} (path: {:?})", self, path.as_ref());
self
}

/// Logs the data error with the given context, then return self.
///
/// This does not modify the error, but if the "log_error_context" feature is enabled,
/// it will print out the context.
#[cfg(feature = "std")]
#[cfg_attr(not(feature = "log_error_context"), allow(unused_variables))]
#[inline]
pub fn with_error_context<E: std::error::Error + ?Sized>(self, err: &E) -> Self {
#[cfg(feature = "log_error_context")]
log::warn!("{}: {}", self, err);
self
}

/// Logs the data error with the given context, then return self.
///
/// This does not modify the error, but if the "log_error_context" feature is enabled,
Expand Down Expand Up @@ -280,26 +273,6 @@ impl DataError {
#[cfg(feature = "std")]
impl std::error::Error for DataError {}

#[cfg(feature = "serde")]
impl From<crate::serde::Error> for DataError {
#[cfg_attr(not(feature = "log_error_context"), allow(unused_variables))]
fn from(e: crate::serde::Error) -> Self {
#[cfg(feature = "log_error_context")]
log::warn!("Serde error: {}", e);
DataError::custom("Serde error")
}
}

#[cfg(feature = "postcard")]
impl From<postcard::Error> for DataError {
#[cfg_attr(not(feature = "log_error_context"), allow(unused_variables))]
fn from(e: postcard::Error) -> Self {
#[cfg(feature = "log_error_context")]
log::warn!("Postcard error: {}", e);
DataError::custom("Postcard error")
}
}

#[cfg(feature = "std")]
impl From<std::io::Error> for DataError {
fn from(e: std::io::Error) -> Self {
Expand Down
Loading

0 comments on commit 3b00440

Please sign in to comment.