Skip to content

Commit bd11122

Browse files
committed
add a reserve API to allow pre-allocation
1 parent d9314f5 commit bd11122

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/snapshot_vec.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ impl<D: SnapshotVecDelegate> SnapshotVec<D> {
119119
&self.values[index]
120120
}
121121

122+
/// Reserve space for new values, just like an ordinary vec.
123+
pub fn reserve(&mut self, additional: usize) {
124+
// This is not affected by snapshots or anything.
125+
self.values.reserve(additional);
126+
}
127+
122128
/// Returns a mutable pointer into the vec; whatever changes you make here cannot be undone
123129
/// automatically, so you should be sure call `record()` with some sort of suitable undo
124130
/// action.

src/unify/backing_vec.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub trait UnificationStore: ops::Index<usize, Output = VarValue<Key<Self>>> + Cl
2929

3030
fn push(&mut self, value: VarValue<Self::Key>);
3131

32+
fn reserve(&mut self, num_new_values: usize);
33+
3234
fn update<F>(&mut self, index: usize, op: F)
3335
where F: FnOnce(&mut VarValue<Self::Key>);
3436

@@ -79,6 +81,11 @@ impl<K: UnifyKey> UnificationStore for InPlace<K> {
7981
self.values.push(value);
8082
}
8183

84+
#[inline]
85+
fn reserve(&mut self, num_new_values: usize) {
86+
self.values.reserve(num_new_values);
87+
}
88+
8289
#[inline]
8390
fn update<F>(&mut self, index: usize, op: F)
8491
where F: FnOnce(&mut VarValue<Self::Key>)
@@ -147,6 +154,11 @@ impl<K: UnifyKey> UnificationStore for Persistent<K> {
147154
self.values.push(value);
148155
}
149156

157+
#[inline]
158+
fn reserve(&mut self, _num_new_values: usize) {
159+
// not obviously relevant to DVec.
160+
}
161+
150162
#[inline]
151163
fn update<F>(&mut self, index: usize, op: F)
152164
where F: FnOnce(&mut VarValue<Self::Key>)

src/unify/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ impl<S: UnificationStore> UnificationTable<S> {
274274
key
275275
}
276276

277+
/// Reserve memory for `num_new_keys` to be created. Does not
278+
/// actually create the new keys; you must then invoke `new_key`.
279+
pub fn reserve(&mut self, num_new_keys: usize) {
280+
self.values.reserve(num_new_keys);
281+
}
282+
277283
/// Returns the number of keys created so far.
278284
pub fn len(&self) -> usize {
279285
self.values.len()

0 commit comments

Comments
 (0)