Skip to content

Commit 95a5fd7

Browse files
committed
0.4.0
1 parent 90299da commit 95a5fd7

File tree

6 files changed

+207
-153
lines changed

6 files changed

+207
-153
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "toolshed"
3-
version = "0.3.2"
3+
version = "0.4.0"
44
authors = ["maciejhirsz <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
description = "Arena allocator and a handful of useful data structures"

benches/list.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,70 +79,66 @@ fn vec_create_256(b: &mut Bencher) {
7979
#[bench]
8080
fn list_create_016(b: &mut Bencher) {
8181
let arena = Arena::new();
82-
let a = &arena;
8382
let words = &WORDS[1..16];
8483

8584
b.iter(|| {
86-
unsafe { a.clear() };
87-
let mut builder = ListBuilder::new(a, WORDS[0]);
85+
unsafe { arena.clear() };
86+
let builder = ListBuilder::new(&arena, WORDS[0]);
8887

8988
for word in words.iter() {
90-
builder.push(*word);
89+
builder.push(&arena, *word);
9190
}
9291

93-
black_box(builder.into_list());
92+
black_box(builder.as_list());
9493
})
9594
}
9695

9796
#[bench]
9897
fn list_create_032(b: &mut Bencher) {
9998
let arena = Arena::new();
100-
let a = &arena;
10199
let words = &WORDS[1..32];
102100

103101
b.iter(|| {
104-
unsafe { a.clear() };
105-
let mut builder = ListBuilder::new(a, WORDS[0]);
102+
unsafe { arena.clear() };
103+
let builder = ListBuilder::new(&arena, WORDS[0]);
106104

107105
for word in words.iter() {
108-
builder.push(*word);
106+
builder.push(&arena, *word);
109107
}
110108

111-
black_box(builder.into_list());
109+
black_box(builder.as_list());
112110
})
113111
}
114112

115113
#[bench]
116114
fn list_create_064(b: &mut Bencher) {
117115
let arena = Arena::new();
118-
let a = &arena;
119116
let words = &WORDS[1..64];
120117

121118
b.iter(|| {
122-
unsafe { a.clear() };
123-
let mut builder = ListBuilder::new(a, WORDS[0]);
119+
unsafe { arena.clear() };
120+
let builder = ListBuilder::new(&arena, WORDS[0]);
124121

125122
for word in words.iter() {
126-
builder.push(*word);
123+
builder.push(&arena, *word);
127124
}
128125

129-
black_box(builder.into_list());
126+
black_box(builder.as_list());
130127
})
131128
}
132129

133130
#[bench]
134131
fn list_create_256(b: &mut Bencher) {
135132
let arena = Arena::new();
136-
let a = &arena;
137133

138134
b.iter(|| {
139-
unsafe { a.clear() };
140-
let mut builder = ListBuilder::new(a, (0usize, 0));
135+
unsafe { arena.clear() };
136+
let builder = ListBuilder::new(&arena, (0usize, 0));
141137

142138
for i in 1..256usize {
143-
builder.push((i, i));
139+
builder.push(&arena, (i, i));
144140
}
145141

146-
black_box(builder.into_list());
142+
black_box(builder.as_list());
147143
})
148144
}

src/arena.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,11 @@ impl Arena {
7575
}
7676
}
7777

78-
/// Allocate an `&str` slice onto the arena with an extra null byte at the end.
79-
/// Can be useful for C-style byte parsers where a null byte is not expected in
80-
/// a well formatted string.
81-
pub fn alloc_str_zero_end<'a>(&'a self, val: &str) -> *const u8 {
78+
/// Allocate an `&str` slice onto the arena as null terminated C-style string.
79+
/// No checks are performed on the source and whether or not it already contains
80+
/// any nul bytes. While this does not create any memory issues, it assumes that
81+
/// the reader of the source can deal with malformed source.
82+
pub fn alloc_str_with_nul<'a>(&'a self, val: &str) -> *const u8 {
8283
let len_with_zero = val.len() + 1;
8384
let ptr = self.require(len_with_zero);
8485

@@ -159,9 +160,22 @@ impl Arena {
159160
/// The only case where the use of this method would be justified is
160161
/// in benchmarks where creation of a structure on the arena is to be
161162
/// tested without the cost of re-creating the arena itself on every iteration.
163+
#[doc(hidden)]
162164
#[inline]
163165
pub unsafe fn clear(&self) {
164-
self.offset.set(0)
166+
self.reset_to(0)
167+
}
168+
169+
#[doc(hidden)]
170+
#[inline]
171+
pub unsafe fn offset(&self) -> usize {
172+
self.offset.get()
173+
}
174+
175+
#[doc(hidden)]
176+
#[inline]
177+
pub unsafe fn reset_to(&self, offset: usize) {
178+
self.offset.set(offset)
165179
}
166180
}
167181

@@ -224,9 +238,9 @@ mod test {
224238
}
225239

226240
#[test]
227-
fn alloc_str_zero_end() {
241+
fn alloc_str_with_nul() {
228242
let arena = Arena::new();
229-
let ptr = arena.alloc_str_zero_end("abcdefghijk");
243+
let ptr = arena.alloc_str_with_nul("abcdefghijk");
230244
let allocated = unsafe { ::std::slice::from_raw_parts(ptr, 12) };
231245

232246
assert_eq!(arena.offset.get(), 16);

src/impl_debug.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::fmt::{self, Debug};
2-
use list::List;
2+
use list::{List, GrowableList, ListBuilder};
33
use map::{Map, BloomMap};
44
use set::{Set, BloomSet};
55

@@ -13,6 +13,26 @@ where
1313
}
1414
}
1515

16+
impl<'arena, T> Debug for GrowableList<'arena, T>
17+
where
18+
T: Debug,
19+
{
20+
#[inline]
21+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22+
self.as_list().fmt(f)
23+
}
24+
}
25+
26+
impl<'arena, T> Debug for ListBuilder<'arena, T>
27+
where
28+
T: Debug,
29+
{
30+
#[inline]
31+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32+
self.as_list().fmt(f)
33+
}
34+
}
35+
1636
impl<'arena, K, V> Debug for Map<'arena, K, V>
1737
where
1838
K: Debug,

0 commit comments

Comments
 (0)