Skip to content

Commit 0142c04

Browse files
committed
Some micro optimizations
1 parent ad02a2f commit 0142c04

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

src/object.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ pub struct Object {
230230
impl Object {
231231
/// Create a new, empty instance of `Object`. Empty `Object` performs no
232232
/// allocation until a value is inserted into it.
233+
#[inline(always)]
233234
pub fn new() -> Self {
234235
Object {
235236
store: Vec::new()
@@ -238,23 +239,24 @@ impl Object {
238239

239240
/// Create a new `Object` with memory preallocated for `capacity` number
240241
/// of entries.
242+
#[inline(always)]
241243
pub fn with_capacity(capacity: usize) -> Self {
242244
Object {
243245
store: Vec::with_capacity(capacity)
244246
}
245247
}
246248

249+
#[inline(always)]
247250
fn node_at_index<'a>(&self, index: usize) -> &'a Node {
248-
let store_ptr = self.store.as_ptr();
249251
unsafe {
250-
&*store_ptr.offset(index as isize)
252+
&*self.store.as_ptr().offset(index as isize)
251253
}
252254
}
253255

256+
#[inline(always)]
254257
fn node_at_index_mut<'a>(&mut self, index: usize) -> &'a mut Node {
255-
let store_ptr = self.store.as_mut_ptr();
256258
unsafe {
257-
&mut *store_ptr.offset(index as isize)
259+
&mut *self.store.as_mut_ptr().offset(index as isize)
258260
}
259261
}
260262

@@ -264,14 +266,13 @@ impl Object {
264266

265267
if index < self.store.capacity() {
266268
self.store.push(Node::new(value));
267-
self.store[index].attach_key(key, hash);
269+
self.node_at_index_mut(index).attach_key(key, hash);
268270
} else {
269271
self.store.push(Node::new(value));
270-
self.store[index].attach_key(key, hash);
272+
self.node_at_index_mut(index).attach_key(key, hash);
271273

272-
// FIXME: don't fix the last element again
273-
for node in self.store.iter_mut() {
274-
node.fix_key_ptr();
274+
for i in 0 .. index - 1 {
275+
self.node_at_index_mut(i).fix_key_ptr();
275276
}
276277
}
277278

@@ -430,10 +431,12 @@ impl Object {
430431
Some(removed)
431432
}
432433

434+
#[inline(always)]
433435
pub fn len(&self) -> usize {
434436
self.store.len()
435437
}
436438

439+
#[inline(always)]
437440
pub fn is_empty(&self) -> bool {
438441
self.store.is_empty()
439442
}

src/parser.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ macro_rules! expect_string {
138138
continue;
139139
}
140140
if ch == b'"' {
141-
// result = &$parser.source[start .. $parser.index - 1];
142-
143141
unsafe {
144142
let ptr = $parser.byte_ptr.offset(start as isize);
145143
let len = $parser.index - 1 - start;
@@ -594,8 +592,7 @@ impl<'a> Parser<'a> {
594592
expect!{ self,
595593
b']' => break,
596594
b',' => {
597-
let value = expect_value!(self);
598-
array.push(value);
595+
array.push(expect_value!(self));
599596
}
600597
};
601598
}

src/short.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{ ptr, str, slice, fmt };
1+
use std::{ ptr, str, slice, fmt, mem };
22
use std::ops::Deref;
33

44
pub const MAX_LEN: usize = 30;
@@ -23,8 +23,7 @@ impl Short {
2323
#[inline]
2424
pub unsafe fn from_slice(slice: &str) -> Self {
2525
let mut short = Short {
26-
// initializing memory with 0s makes things faster in the long run
27-
value: [0; MAX_LEN],
26+
value: mem::uninitialized(),
2827
len: slice.len() as u8,
2928
};
3029

@@ -45,6 +44,7 @@ impl Short {
4544
}
4645

4746
impl PartialEq for Short {
47+
#[inline]
4848
fn eq(&self, other: &Short) -> bool {
4949
self.as_str() == other.as_str()
5050
}

0 commit comments

Comments
 (0)