1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//! Collections of chunks with the same archetype.
use std::convert::TryFrom;
use std::sync::Arc;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use crate::{Archetype, Chunk, EntityID};
use crate::chunk::{ChunkAction, ChunkEdit, ChunkEntityData};
use crate::component_data::{ComponentDataSlice, ComponentValueRef};
use std::ops::Deref;
fn chunk_min(c: &Chunk) -> EntityID {
*c.components::<EntityID>().unwrap().first().unwrap()
}
fn chunk_max(c: &Chunk) -> EntityID {
*c.components::<EntityID>().unwrap().last().unwrap()
}
/// A sorted collection of `Chunk`s which all refer to the same `Archetype`.
///
/// This structure is used for efficient lookup and modification of multiple
/// chunks.
#[derive(Debug, Clone)]
pub struct ChunkSet {
storage: Vec<Arc<Chunk>>,
}
impl ChunkSet {
/// Create a new empty chunk set.
pub fn new() -> ChunkSet {
ChunkSet {
storage: Vec::new(),
}
}
/// Create a new chunk set with a given capacity.
pub fn with_capacity(capacity: usize) -> ChunkSet {
ChunkSet {
storage: Vec::with_capacity(capacity),
}
}
/// Get the number of chunks in this set.
pub fn len(&self) -> usize {
self.storage.len()
}
/// Return true if this chunk set is empty.
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Get the list of chunks which are members of this `ChunkSet`.
pub fn chunks(&self) -> &[Arc<Chunk>] {
&self.storage
}
/// Lookup the index of the chunk which may contain the given entity.
pub fn chunk_index_for_entity(&self, id: EntityID) -> Option<usize> {
if self.storage.is_empty() {
None
} else {
let idx = match self.storage.binary_search_by_key(&id, |c| {
*c.components::<EntityID>().unwrap().first().unwrap()
}) {
Ok(idx) => idx,
Err(idx) => idx.min(1) - 1,
};
Some(idx)
}
}
/// Lookup the chunk which may contain an entity with the given ID.
///
/// This may return `Some` even if the given chunk does not actually contain
/// the entity with the given ID. However, it does return the only chunk
/// which could contain it.
pub fn chunk_for_entity(&self, id: EntityID) -> Option<&Arc<Chunk>> {
let idx = self.chunk_index_for_entity(id);
idx.map(|idx| &self.storage[idx])
}
/// Lookup a mutable reference to the chunk which may contain an entity
/// with the given ID.
pub fn chunk_for_entity_mut(&mut self, id: EntityID) -> Option<&mut Arc<Chunk>> {
if let Some(idx) = self.chunk_index_for_entity(id) {
Some(&mut self.storage[idx])
} else {
None
}
}
/// Lookup the index of the chunk which may contain an entity, starting with a given hint index.
///
/// Hint may be used to speed up searching if the entity is nearby.
pub fn chunk_index_for_entity_hint(&self, id: EntityID, hint: usize) -> Option<usize> {
let chunk_a = self.storage.get(hint);
// Optimistically check whether the entity is in the next two chunks.
if chunk_a.map_or(false, |c| chunk_min(c) <= id) {
let chunk_a = chunk_a.unwrap();
if chunk_max(chunk_a) >= id {
return Some(hint);
}
if let Some(chunk_b) = self.storage.get(hint + 1) {
if chunk_max(chunk_b) >= id {
return Some(hint + 1);
}
}
}
if hint == 0 {
None
} else {
self.chunk_index_for_entity(id)
}
}
/// Create a parallel iterator over the chunks in this set.
pub fn par_iter_chunks(&self) -> impl ParallelIterator<Item=&Arc<Chunk>> {
self.storage.par_iter()
}
/// Create an iterator over all the chunks in this set.
pub fn iter_chunks(&self) -> impl Iterator<Item=&Arc<Chunk>> {
self.storage.iter()
}
/// Re-assert the invariant: all chunks should contain between N/2 and N
/// entities (so long as there are 2 or more chunks).
fn rebalance(&mut self) {
if self.storage.len() < 2 {
return;
}
let mut split_idx = 1;
while split_idx < self.storage.len() {
let (chunk_a, chunk_b) = self.storage.split_at_mut(split_idx);
let chunk_a = chunk_a.last_mut().unwrap();
let chunk_b = chunk_b.first_mut().unwrap();
let cap = chunk_a.capacity();
let half_cap = cap >> 1;
let a_len = chunk_a.len();
let b_len = chunk_b.len();
if a_len + b_len < cap {
// The two chunks can be very simply merged.
let chunk_a = Arc::make_mut(chunk_a);
chunk_a.copy_from(chunk_a.len(), chunk_b, ..);
self.storage.remove(split_idx);
} else if a_len < half_cap {
// A is too small.
let chunk_a = Arc::make_mut(chunk_a);
let chunk_b = Arc::make_mut(chunk_b);
let split_at = (a_len + b_len) >> 1;
let to_move = split_at - a_len;
chunk_a.copy_from(a_len, chunk_b, ..to_move);
chunk_b.remove(..to_move);
} else if b_len < half_cap {
// B is too small.
let chunk_a = Arc::make_mut(chunk_a);
let chunk_b = Arc::make_mut(chunk_b);
let split_at = (a_len + b_len) >> 1;
chunk_b.copy_from(0, chunk_a, split_at..);
chunk_a.remove(split_at..);
} else {
// Both sides are N/2 <= len <= N
split_idx += 1;
}
}
}
/// Apply an edit list to this chunk.
pub(crate) fn modify<'a>(&mut self,
archetype: Arc<Archetype>,
mut edits: Vec<ChunkEdit>,
component_data: &[ComponentValueRef<'a>]) {
// This is needed so we can optimally resize single chunks later on.
edits.sort_by_key(|e| e.0);
let mut last_chunk_index = 0;
let mut chunk_edit_start = 0;
while chunk_edit_start < edits.len() {
let ChunkEdit(first_id, _) = edits[chunk_edit_start];
let chunk_idx = self.chunk_index_for_entity_hint(first_id, last_chunk_index);
let mut chunk_idx = if let Some(chunk_idx) = chunk_idx {
chunk_idx
} else {
self.storage.push(Arc::new(archetype.clone().new_chunk()));
0
};
last_chunk_index = chunk_idx;
let max_entity_id = self.storage.get(chunk_idx + 1)
.and_then(|c| c.components::<EntityID>().unwrap().first())
.copied();
let edit_chunk = &mut self.storage[chunk_idx];
// Find all the entries that would go in this chunk.
let chunk_edit_end = if let Some(max_id) = max_entity_id {
let mut end = chunk_edit_start;
while end < edits.len() {
let id = edits[end].0;
if id >= max_id {
break;
}
end += 1;
}
end
} else {
edits.len()
};
let chunk_edits = &edits[chunk_edit_start..chunk_edit_end];
let new_size = chunk_edits.iter()
.fold(edit_chunk.len(), |acc, edit| {
let ChunkEdit(_, action) = edit;
match action {
ChunkAction::Upsert(_, _) => acc + 1,
ChunkAction::Remove => acc - 1,
}
});
if new_size == 0 {
// We drained a chunk, remove it.
self.storage.remove(chunk_idx);
} else if new_size <= edit_chunk.capacity() {
// If this makes up a single chunk, mutate the existing chunk.
// Reverse iterate so we can resize optimally.
let edit_chunk = Arc::make_mut(edit_chunk);
edit_chunk.modify(chunk_edits.iter().rev(), component_data);
} else {
// It doesn't fit in one chunk, so just start streaming chunks until we've
// applied all edits.
// Unbalanced chunks will be balanced later.
let old_chunk = self.storage.remove(chunk_idx);
let entity_ids = old_chunk.components::<EntityID>().unwrap();
let mut read_idx = 0;
let mut edit_idx = 0;
let mut new_chunk = archetype.clone().new_chunk();
fn alloc_new_chunk(storage: &mut Vec<Arc<Chunk>>, archetype: &Arc<Archetype>, new_chunk: &mut Chunk, chunk_idx: &mut usize) {
if new_chunk.len() == new_chunk.capacity() {
let to_insert = Arc::new(std::mem::replace(
new_chunk,
archetype.clone().new_chunk()));
storage.insert(*chunk_idx, to_insert);
*chunk_idx += 1;
}
}
// Ad-hoc sorted merge.
while read_idx < entity_ids.len() || edit_idx < chunk_edits.len() {
let old_id = entity_ids.get(read_idx).cloned();
let next_edit = chunk_edits.get(edit_idx).cloned();
match next_edit {
Some(ChunkEdit(id, action)) => {
match action {
ChunkAction::Upsert(data_start, data_end) => {
alloc_new_chunk(&mut self.storage, &archetype, &mut new_chunk, &mut chunk_idx);
let data_slice = &component_data[data_start..data_end];
let data = ComponentDataSlice::try_from(data_slice).unwrap();
if Some(id) == old_id {
// If this is an update we have to apply the old components
// first, then copy over the top to make sure we don't
// lose any values.
let write_idx = new_chunk.len();
new_chunk.insert(
write_idx,
&ChunkEntityData::new(old_chunk.clone(), read_idx));
new_chunk.update_at(write_idx, &data);
read_idx += 1;
} else {
let write_idx = new_chunk.len();
new_chunk.insert(write_idx, &data);
new_chunk.components_mut::<EntityID>().unwrap()[write_idx] = id;
}
}
ChunkAction::Remove => {
assert_eq!(id, old_id.unwrap());
read_idx += 1;
}
}
edit_idx += 1;
}
None => {
while read_idx < entity_ids.len() {
alloc_new_chunk(&mut self.storage, &archetype, &mut new_chunk, &mut chunk_idx);
let to_copy = (entity_ids.len() - read_idx)
.min(new_chunk.capacity() - new_chunk.len());
new_chunk.copy_from(
new_chunk.len(),
&old_chunk,
read_idx..read_idx+to_copy);
read_idx += to_copy;
}
}
}
}
self.storage.insert(chunk_idx, Arc::new(new_chunk));
}
chunk_edit_start = chunk_edit_end;
}
self.rebalance();
}
}
impl Deref for ChunkSet {
type Target = [Arc<Chunk>];
fn deref(&self) -> &Self::Target {
&self.storage
}
}