Skip to content

Commit

Permalink
Favor SmallVec over Box in pluralrules
Browse files Browse the repository at this point in the history
This will help avoid heap allocations as most lists are only a few items
long. The only boxed slice that was retained was the SampleRanges, as
this can be of an indeterminate length.
  • Loading branch information
gregtatum committed Oct 1, 2020
1 parent e6579f8 commit 0052021
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 40 deletions.
1 change: 1 addition & 0 deletions components/pluralrules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ include = [
[dependencies]
icu-locale = { path = "../locale" }
icu-data-provider = { path = "../data-provider" }
smallvec = "1.4"

[dev-dependencies]
criterion = "0.3"
Expand Down
56 changes: 32 additions & 24 deletions components/pluralrules/src/rules/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,35 @@
//! ```
//! use icu_pluralrules::rules::parse_condition;
//! use icu_pluralrules::rules::ast::*;
//! use smallvec::{SmallVec,smallvec};
//!
//! let input = "i = 1";
//!
//! let ast = parse_condition(input.as_bytes())
//! .expect("Parsing failed.");
//!
//! assert_eq!(ast, Condition(Box::new([
//! AndCondition(Box::new([
//! assert_eq!(ast, Condition(smallvec![
//! AndCondition(smallvec![
//! Relation {
//! expression: Expression {
//! operand: Operand::I,
//! modulus: None,
//! },
//! operator: Operator::Eq,
//! range_list: RangeList(Box::new([
//! range_list: RangeList(smallvec![
//! RangeListItem::Value(
//! Value(1)
//! )
//! ]))
//! ])
//! }
//! ]))
//! ])));
//! ])
//! ]));
//! ```
//!
//! [`PluralCategory`]: ../enum.PluralCategory.html
//! [`parse`]: ../fn.parse.html
//! [`test_condition`]: ../fn.test_condition.html
use smallvec::SmallVec;
use std::ops::RangeInclusive;

/// A complete AST representation of a plural rule.
Expand Down Expand Up @@ -89,25 +91,26 @@ pub struct Rule {
/// ```
/// use icu_pluralrules::rules::ast::*;
/// use icu_pluralrules::rules::parse_condition;
/// use smallvec::{SmallVec,smallvec};
///
/// let condition = Condition(Box::new([
/// AndCondition(Box::new([Relation {
/// let condition = Condition(smallvec![
/// AndCondition(smallvec![Relation {
/// expression: Expression {
/// operand: Operand::I,
/// modulus: None,
/// },
/// operator: Operator::Eq,
/// range_list: RangeList(Box::new([RangeListItem::Value(Value(5))])),
/// }])),
/// AndCondition(Box::new([Relation {
/// range_list: RangeList(smallvec![RangeListItem::Value(Value(5))]),
/// }]),
/// AndCondition(smallvec![Relation {
/// expression: Expression {
/// operand: Operand::V,
/// modulus: None,
/// },
/// operator: Operator::Eq,
/// range_list: RangeList(Box::new([RangeListItem::Value(Value(2))])),
/// }])),
/// ]));
/// range_list: RangeList(smallvec![RangeListItem::Value(Value(2))]),
/// }]),
/// ]);
///
/// assert_eq!(
/// condition,
Expand All @@ -116,7 +119,7 @@ pub struct Rule {
/// )
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Condition(pub Box<[AndCondition]>);
pub struct Condition(pub SmallVec<[AndCondition; 2]>);

/// An incomplete AST representation of a plural rule. Comprises a vector of Relations.
///
Expand All @@ -132,29 +135,30 @@ pub struct Condition(pub Box<[AndCondition]>);
///
/// ```
/// use icu_pluralrules::rules::ast::*;
/// use smallvec::{SmallVec,smallvec};
///
/// AndCondition(Box::new([
/// AndCondition(smallvec![
/// Relation {
/// expression: Expression {
/// operand: Operand::I,
/// modulus: None,
/// },
/// operator: Operator::Eq,
/// range_list: RangeList(Box::new([RangeListItem::Value(Value(5))])),
/// range_list: RangeList(smallvec![RangeListItem::Value(Value(5))]),
/// },
/// Relation {
/// expression: Expression {
/// operand: Operand::V,
/// modulus: None,
/// },
/// operator: Operator::NotEq,
/// range_list: RangeList(Box::new([RangeListItem::Value(Value(2))])),
/// range_list: RangeList(smallvec![RangeListItem::Value(Value(2))]),
/// },
/// ]));
/// ]);
///
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct AndCondition(pub Box<[Relation]>);
pub struct AndCondition(pub SmallVec<[Relation; 2]>);

/// An incomplete AST representation of a plural rule. Comprises an Expression, an Operator, and a RangeList.
///
Expand All @@ -170,14 +174,15 @@ pub struct AndCondition(pub Box<[Relation]>);
///
/// ```
/// use icu_pluralrules::rules::ast::*;
/// use smallvec::{SmallVec,smallvec};
///
/// Relation {
/// expression: Expression {
/// operand: Operand::I,
/// modulus: None,
/// },
/// operator: Operator::Eq,
/// range_list: RangeList(Box::new([RangeListItem::Value(Value(3))])),
/// range_list: RangeList(smallvec![RangeListItem::Value(Value(3))]),
/// };
///
/// ```
Expand Down Expand Up @@ -277,15 +282,16 @@ pub enum Operand {
///
/// ```
/// use icu_pluralrules::rules::ast::*;
/// use smallvec::{SmallVec,smallvec};
///
/// RangeList(Box::new([
/// RangeList(smallvec![
/// RangeListItem::Value(Value(5)),
/// RangeListItem::Value(Value(7)),
/// RangeListItem::Value(Value(9)),
/// ]));
/// ]);
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct RangeList(pub Box<[RangeListItem]>);
pub struct RangeList(pub SmallVec<[RangeListItem; 2]>);

/// An enum of items that appear in a RangeList: Range or a Value.
///
Expand Down Expand Up @@ -376,6 +382,8 @@ pub struct Samples {
///
/// ```
/// use icu_pluralrules::rules::ast::*;
/// use smallvec::{SmallVec,smallvec};
///
/// SampleList {
/// sample_ranges: Box::new([
/// SampleRange {
Expand Down
19 changes: 10 additions & 9 deletions components/pluralrules/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,42 @@
//! When parsed, the resulting [`AST`] will look like this:
//!
//! ```
//! use smallvec::{SmallVec,smallvec};
//! use icu_pluralrules::rules::parse_condition;
//! use icu_pluralrules::rules::ast::*;
//!
//! let input = "i = 1 and v = 0 @integer 1";
//!
//! let ast = parse_condition(input.as_bytes())
//! .expect("Parsing failed.");
//! assert_eq!(ast, Condition(Box::new([
//! AndCondition(Box::new([
//! assert_eq!(ast, Condition(smallvec![
//! AndCondition(smallvec![
//! Relation {
//! expression: Expression {
//! operand: Operand::I,
//! modulus: None,
//! },
//! operator: Operator::Eq,
//! range_list: RangeList(Box::new([
//! range_list: RangeList(smallvec![
//! RangeListItem::Value(
//! Value(1)
//! )
//! ]))
//! ])
//! },
//! Relation {
//! expression: Expression {
//! operand: Operand::V,
//! modulus: None,
//! },
//! operator: Operator::Eq,
//! range_list: RangeList(Box::new([
//! range_list: RangeList(smallvec![
//! RangeListItem::Value(
//! Value(0)
//! )
//! ]))
//! },
//! ])),
//! ])));
//! ])
//! }
//! ]),
//! ]));
//! ```
//!
//! Finally, we can pass this [`AST`] (in fact, just the [`Condition`] node),
Expand Down
15 changes: 8 additions & 7 deletions components/pluralrules/src/rules/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::ast;
use super::lexer::{Lexer, Token};
use smallvec::smallvec;
use std::iter::Peekable;

#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -99,12 +100,12 @@ impl<'p> Parser<'p> {
}

fn get_condition(&mut self) -> Result<ast::Condition, ParserError> {
let mut result = vec![];
let mut result = smallvec![];

if let Some(cond) = self.get_and_condition()? {
result.push(cond);
} else {
return Ok(ast::Condition(result.into_boxed_slice()));
return Ok(ast::Condition(result));
}

while self.take_if(Token::Or) {
Expand All @@ -115,12 +116,12 @@ impl<'p> Parser<'p> {
}
}
// If lexer is not done, error?
Ok(ast::Condition(result.into_boxed_slice()))
Ok(ast::Condition(result))
}

fn get_and_condition(&mut self) -> Result<Option<ast::AndCondition>, ParserError> {
if let Some(relation) = self.get_relation()? {
let mut rel = vec![relation];
let mut rel = smallvec![relation];

while self.take_if(Token::And) {
if let Some(relation) = self.get_relation()? {
Expand All @@ -129,7 +130,7 @@ impl<'p> Parser<'p> {
return Err(ParserError::ExpectedRelation);
}
}
Ok(Some(ast::AndCondition(rel.into_boxed_slice())))
Ok(Some(ast::AndCondition(rel)))
} else {
Ok(None)
}
Expand Down Expand Up @@ -168,14 +169,14 @@ impl<'p> Parser<'p> {
}

fn get_range_list(&mut self) -> Result<ast::RangeList, ParserError> {
let mut range_list = Vec::with_capacity(1);
let mut range_list = smallvec![];
loop {
range_list.push(self.get_range_list_item()?);
if !self.take_if(Token::Comma) {
break;
}
}
Ok(ast::RangeList(range_list.into_boxed_slice()))
Ok(ast::RangeList(range_list))
}

fn take_if(&mut self, token: Token) -> bool {
Expand Down

0 comments on commit 0052021

Please sign in to comment.