Skip to content

Commit ac92e4a

Browse files
committed
Another round of ioi driven optimizations.
1 parent 2da539d commit ac92e4a

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

fluent-syntax/src/parser/expression.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ where
105105
Some(b'-') => {
106106
self.ptr += 1; // -
107107
if self.is_identifier_start() {
108-
let id = self.get_identifier()?;
108+
self.ptr += 1;
109+
let id = self.get_identifier_unchecked();
109110
let attribute = self.get_attribute_accessor()?;
110111
let arguments = self.get_call_arguments()?;
111112
Ok(ast::InlineExpression::TermReference {
@@ -120,12 +121,13 @@ where
120121
}
121122
}
122123
Some(b'$') => {
123-
self.ptr += 1; // -
124+
self.ptr += 1; // $
124125
let id = self.get_identifier()?;
125126
Ok(ast::InlineExpression::VariableReference { id })
126127
}
127128
Some(b) if b.is_ascii_alphabetic() => {
128-
let id = self.get_identifier()?;
129+
self.ptr += 1;
130+
let id = self.get_identifier_unchecked();
129131
let arguments = self.get_call_arguments()?;
130132
if arguments.is_some() {
131133
if !Self::is_callee(&id.name) {
@@ -139,6 +141,7 @@ where
139141
}
140142
}
141143
Some(b'{') => {
144+
self.ptr += 1; // {
142145
let exp = self.get_placeable()?;
143146
Ok(ast::InlineExpression::Placeable {
144147
expression: Box::new(exp),

fluent-syntax/src/parser/mod.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ where
2828
S: Slice<'s>,
2929
{
3030
pub fn new(source: S) -> Self {
31-
let length = source.as_ref().len();
31+
let length = source.as_ref().as_bytes().len();
3232
Self {
3333
source,
3434
ptr: 0,
@@ -199,29 +199,31 @@ where
199199
}
200200
}
201201

202-
fn get_identifier(&mut self) -> Result<ast::Identifier<S>> {
202+
fn get_identifier_unchecked(&mut self) -> ast::Identifier<S> {
203203
let mut ptr = self.ptr;
204204

205-
if self.is_identifier_start() {
206-
ptr += 1;
207-
} else {
208-
return error!(
209-
ErrorKind::ExpectedCharRange {
210-
range: "a-zA-Z".to_string()
211-
},
212-
ptr
213-
);
214-
}
215-
216205
while matches!(get_byte!(self, ptr), Some(b) if b.is_ascii_alphanumeric() || *b == b'-' || *b == b'_')
217206
{
218207
ptr += 1;
219208
}
220209

221-
let name = self.source.slice(self.ptr..ptr);
210+
let name = self.source.slice(self.ptr - 1..ptr);
222211
self.ptr = ptr;
223212

224-
Ok(ast::Identifier { name })
213+
ast::Identifier { name }
214+
}
215+
216+
fn get_identifier(&mut self) -> Result<ast::Identifier<S>> {
217+
if !self.is_identifier_start() {
218+
return error!(
219+
ErrorKind::ExpectedCharRange {
220+
range: "a-zA-Z".to_string()
221+
},
222+
self.ptr
223+
);
224+
}
225+
self.ptr += 1;
226+
Ok(self.get_identifier_unchecked())
225227
}
226228

227229
fn get_attribute_accessor(&mut self) -> Result<Option<ast::Identifier<S>>> {
@@ -234,9 +236,6 @@ where
234236
}
235237

236238
fn get_variant_key(&mut self) -> Result<ast::VariantKey<S>> {
237-
if !self.take_byte_if(b'[') {
238-
return error!(ErrorKind::ExpectedToken('['), self.ptr);
239-
}
240239
self.skip_blank();
241240

242241
let key = if self.is_number_start() {
@@ -257,12 +256,11 @@ where
257256
}
258257

259258
fn get_variants(&mut self) -> Result<Vec<ast::Variant<S>>> {
260-
let mut variants = vec![];
259+
let mut variants = Vec::with_capacity(2);
261260
let mut has_default = false;
262261

263-
while self.is_current_byte(b'*') || self.is_current_byte(b'[') {
262+
loop {
264263
let default = self.take_byte_if(b'*');
265-
266264
if default {
267265
if has_default {
268266
return error!(ErrorKind::MultipleDefaultVariants, self.ptr);
@@ -271,6 +269,10 @@ where
271269
}
272270
}
273271

272+
if !self.take_byte_if(b'[') {
273+
break;
274+
}
275+
274276
let key = self.get_variant_key()?;
275277

276278
let value = self.get_pattern()?;
@@ -295,7 +297,6 @@ where
295297
}
296298

297299
fn get_placeable(&mut self) -> Result<ast::Expression<S>> {
298-
self.expect_byte(b'{')?;
299300
self.skip_blank();
300301
let exp = self.get_expression()?;
301302
self.skip_blank_inline();

fluent-syntax/src/parser/pattern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ where
5757
};
5858

5959
while self.ptr < self.length {
60-
if self.is_current_byte(b'{') {
60+
if self.take_byte_if(b'{') {
6161
if text_element_role == TextElementPosition::LineStart {
6262
common_indent = Some(0);
6363
}

fluent-syntax/src/parser/runtime.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ where
99
) -> std::result::Result<ast::Resource<S>, (ast::Resource<S>, Vec<ParserError>)> {
1010
let mut errors = vec![];
1111

12-
let mut body = vec![];
12+
// That default allocation gives the lowest
13+
// number of instructions and cycles in ioi.
14+
let mut body = Vec::with_capacity(6);
1315

1416
self.skip_blank_block();
1517

0 commit comments

Comments
 (0)