Skip to content

Commit 60de96d

Browse files
committed
Replace Vector::Empty with Vector::Inline using InlineArray.
1 parent b7578f8 commit 60de96d

File tree

5 files changed

+117
-83
lines changed

5 files changed

+117
-83
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ The minimum supported Rust version is now 1.34.0.
3030

3131
### Fixed
3232

33+
- `Vector` now uses
34+
[`sized_chunks::InlineArray`](https://docs.rs/sized-chunks/0.3.0/sized_chunks/inline_array/struct.InlineArray.html)
35+
instead of an `Empty` enum case to avoid allocation at very small sizes,
36+
letting you store a handful of elements on the stack before needing to grow
37+
into a full chunk. This has a beneficial effect on performance as well, as
38+
there's no pointer into the heap to dereference, making it faster than
39+
`std::vec::Vec` in this configuration.
3340
- Some complexity timings have been added and corrected. (#87)
3441
- `OrdSet::is_subset(&self, other)` now returns immediately when `self` is
3542
larger than `other` and thus could not possibly be a subset of it. (#87)

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ rustc_version = "0.2"
2525

2626
[dependencies]
2727
typenum = "1.10"
28-
sized-chunks = "0.2.0"
28+
sized-chunks = "0.3.0"
2929
quickcheck = { version = "0.8", optional = true }
3030
proptest = { version = "0.9", optional = true }
3131
serde = { version = "1.0", optional = true }

rc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ rustc_version = "0.2"
2424

2525
[dependencies]
2626
typenum = "1.10"
27-
sized-chunks = "0.1.2"
27+
sized-chunks = "0.3.0"
2828
quickcheck = { version = "0.8", optional = true }
2929
proptest = { version = "0.9", optional = true }
3030
serde = { version = "1.0", optional = true }

src/vector/focus.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ pub enum Focus<'a, A>
8585
where
8686
A: 'a,
8787
{
88-
#[doc(hidden)]
89-
Empty,
9088
#[doc(hidden)]
9189
Single(&'a [A]),
9290
#[doc(hidden)]
@@ -102,7 +100,7 @@ where
102100
/// [Vector]: enum.Vector.html
103101
pub fn new(vector: &'a Vector<A>) -> Self {
104102
match vector {
105-
Vector::Empty => Focus::Empty,
103+
Vector::Inline(chunk) => Focus::Single(chunk),
106104
Vector::Single(chunk) => Focus::Single(chunk),
107105
Vector::Full(tree) => Focus::Full(TreeFocus::new(tree)),
108106
}
@@ -113,7 +111,6 @@ where
113111
/// [Vector]: enum.Vector.html
114112
pub fn len(&self) -> usize {
115113
match self {
116-
Focus::Empty => 0,
117114
Focus::Single(chunk) => chunk.len(),
118115
Focus::Full(tree) => tree.len(),
119116
}
@@ -129,7 +126,6 @@ where
129126
/// Get a reference to the value at a given index.
130127
pub fn get(&mut self, index: usize) -> Option<&A> {
131128
match self {
132-
Focus::Empty => None,
133129
Focus::Single(chunk) => chunk.get(index),
134130
Focus::Full(tree) => tree.get(index),
135131
}
@@ -152,7 +148,6 @@ where
152148
panic!("vector::Focus::chunk_at: index out of bounds");
153149
}
154150
match self {
155-
Focus::Empty => (0..0, &[]),
156151
Focus::Single(chunk) => (0..len, chunk),
157152
Focus::Full(tree) => tree.get_chunk(index),
158153
}
@@ -190,7 +185,6 @@ where
190185
panic!("vector::Focus::narrow: range out of bounds");
191186
}
192187
match self {
193-
Focus::Empty => Focus::Empty,
194188
Focus::Single(chunk) => Focus::Single(&chunk[r]),
195189
Focus::Full(tree) => Focus::Full(tree.narrow(r)),
196190
}
@@ -231,7 +225,6 @@ where
231225
panic!("vector::Focus::split_at: index out of bounds");
232226
}
233227
match self {
234-
Focus::Empty => (Focus::Empty, Focus::Empty),
235228
Focus::Single(chunk) => {
236229
let (left, right) = chunk.split_at(index);
237230
(Focus::Single(left), Focus::Single(right))
@@ -262,7 +255,6 @@ where
262255
{
263256
fn clone(&self) -> Self {
264257
match self {
265-
Focus::Empty => Focus::Empty,
266258
Focus::Single(chunk) => Focus::Single(chunk),
267259
Focus::Full(tree) => Focus::Full(tree.clone()),
268260
}
@@ -483,8 +475,6 @@ pub enum FocusMut<'a, A>
483475
where
484476
A: 'a,
485477
{
486-
#[doc(hidden)]
487-
Empty,
488478
#[doc(hidden)]
489479
Single(&'a mut [A]),
490480
#[doc(hidden)]
@@ -498,7 +488,7 @@ where
498488
/// Construct a `FocusMut` for a `Vector`.
499489
pub fn new(vector: &'a mut Vector<A>) -> Self {
500490
match vector {
501-
Vector::Empty => FocusMut::Empty,
491+
Vector::Inline(chunk) => FocusMut::Single(chunk),
502492
Vector::Single(chunk) => FocusMut::Single(Ref::make_mut(chunk).as_mut_slice()),
503493
Vector::Full(tree) => FocusMut::Full(TreeFocusMut::new(tree)),
504494
}
@@ -507,7 +497,6 @@ where
507497
/// Get the length of the focused `Vector`.
508498
pub fn len(&self) -> usize {
509499
match self {
510-
FocusMut::Empty => 0,
511500
FocusMut::Single(chunk) => chunk.len(),
512501
FocusMut::Full(tree) => tree.len(),
513502
}
@@ -526,7 +515,6 @@ where
526515
/// Get a mutable reference to the value at a given index.
527516
pub fn get_mut(&mut self, index: usize) -> Option<&mut A> {
528517
match self {
529-
FocusMut::Empty => None,
530518
FocusMut::Single(chunk) => chunk.get_mut(index),
531519
FocusMut::Full(tree) => tree.get(index),
532520
}
@@ -645,7 +633,6 @@ where
645633
panic!("vector::FocusMut::chunk_at: index out of bounds");
646634
}
647635
match self {
648-
FocusMut::Empty => (0..0, &mut []),
649636
FocusMut::Single(chunk) => (0..len, chunk),
650637
FocusMut::Full(tree) => {
651638
let (range, chunk) = tree.get_chunk(index);
@@ -686,7 +673,6 @@ where
686673
panic!("vector::FocusMut::narrow: range out of bounds");
687674
}
688675
match self {
689-
FocusMut::Empty => FocusMut::Empty,
690676
FocusMut::Single(chunk) => FocusMut::Single(&mut chunk[r]),
691677
FocusMut::Full(tree) => FocusMut::Full(tree.narrow(r)),
692678
}
@@ -734,7 +720,6 @@ where
734720
panic!("vector::FocusMut::split_at: index out of bounds");
735721
}
736722
match self {
737-
FocusMut::Empty => (FocusMut::Empty, FocusMut::Empty),
738723
FocusMut::Single(chunk) => {
739724
let (left, right) = chunk.split_at_mut(index);
740725
(FocusMut::Single(left), FocusMut::Single(right))
@@ -749,7 +734,6 @@ where
749734
/// Convert a `FocusMut` into a `Focus`.
750735
pub fn unmut(self) -> Focus<'a, A> {
751736
match self {
752-
FocusMut::Empty => Focus::Empty,
753737
FocusMut::Single(chunk) => Focus::Single(chunk),
754738
FocusMut::Full(mut tree) => Focus::Full(TreeFocus {
755739
tree: {

0 commit comments

Comments
 (0)