author | alfadur |
Tue, 27 Aug 2019 20:09:17 +0300 | |
changeset 15372 | d6b4586b271f |
parent 15363 | b5e0a39856fd |
child 15373 | 445138f388d4 |
permissions | -rw-r--r-- |
15359 | 1 |
use super::common::GearId; |
2 |
use std::{ |
|
3 |
any::TypeId, |
|
4 |
mem::{size_of, MaybeUninit}, |
|
5 |
num::NonZeroU16, |
|
15372
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
6 |
ptr::{copy_nonoverlapping, NonNull, null_mut}, |
15359 | 7 |
slice, |
8 |
}; |
|
15310 | 9 |
|
15361 | 10 |
pub unsafe trait TypeTuple: Sized { |
15310 | 11 |
fn len() -> usize; |
12 |
fn get_types(dest: &mut Vec<TypeId>); |
|
15372
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
13 |
unsafe fn iter<F>(slices: &[*mut u8], count: usize, mut f: F) |
15359 | 14 |
where |
15363 | 15 |
F: FnMut(Self); |
15310 | 16 |
} |
17 |
||
15361 | 18 |
unsafe impl<T: 'static> TypeTuple for (&T,) { |
15310 | 19 |
fn len() -> usize { |
20 |
1 |
|
21 |
} |
|
22 |
||
23 |
fn get_types(dest: &mut Vec<TypeId>) { |
|
24 |
dest.push(TypeId::of::<T>()); |
|
25 |
} |
|
15359 | 26 |
|
15372
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
27 |
unsafe fn iter<F>(slices: &[*mut u8], count: usize, mut f: F) |
15359 | 28 |
where |
15363 | 29 |
F: FnMut(Self), |
15359 | 30 |
{ |
15372
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
31 |
let slice1 = slice::from_raw_parts(slices[0] as *const T, count); |
15359 | 32 |
for i in 0..count { |
33 |
f((slice1.get_unchecked(i),)); |
|
34 |
} |
|
35 |
} |
|
15310 | 36 |
} |
37 |
||
15359 | 38 |
const BLOCK_SIZE: usize = 32768; |
39 |
||
40 |
struct DataBlock { |
|
41 |
max_elements: u16, |
|
42 |
elements_count: u16, |
|
43 |
data: Box<[u8; BLOCK_SIZE]>, |
|
15363 | 44 |
component_blocks: [Option<NonNull<u8>>; 64], |
15310 | 45 |
} |
46 |
||
15359 | 47 |
impl Unpin for DataBlock {} |
48 |
||
49 |
impl DataBlock { |
|
50 |
fn new(mask: u64, element_sizes: &[u16; 64]) -> Self { |
|
51 |
let total_size: u16 = element_sizes |
|
52 |
.iter() |
|
53 |
.enumerate() |
|
15363 | 54 |
.filter(|(i, _)| mask & (1 << *i as u64) != 0) |
15359 | 55 |
.map(|(_, size)| *size) |
56 |
.sum(); |
|
57 |
let max_elements = (BLOCK_SIZE / total_size as usize) as u16; |
|
58 |
||
59 |
let mut data: Box<[u8; BLOCK_SIZE]> = |
|
15362 | 60 |
Box::new(unsafe { MaybeUninit::uninit().assume_init() }); |
15359 | 61 |
let mut blocks = [None; 64]; |
62 |
let mut offset = 0; |
|
63 |
||
64 |
for i in 0..64 { |
|
15363 | 65 |
if mask & (1 << i) != 0 { |
15359 | 66 |
blocks[i] = Some(NonNull::new(data[offset..].as_mut_ptr()).unwrap()); |
67 |
offset += element_sizes[i] as usize * max_elements as usize; |
|
68 |
} |
|
69 |
} |
|
15310 | 70 |
Self { |
15359 | 71 |
elements_count: 0, |
72 |
max_elements, |
|
73 |
data, |
|
15363 | 74 |
component_blocks: blocks, |
15310 | 75 |
} |
76 |
} |
|
77 |
||
15359 | 78 |
fn is_full(&self) -> bool { |
79 |
self.elements_count == self.max_elements |
|
80 |
} |
|
15310 | 81 |
} |
82 |
||
15359 | 83 |
#[derive(Clone, Copy, Debug, Default)] |
84 |
pub struct LookupEntry { |
|
85 |
index: Option<NonZeroU16>, |
|
86 |
block_index: u16, |
|
15310 | 87 |
} |
88 |
||
89 |
pub struct GearDataManager { |
|
90 |
types: Vec<TypeId>, |
|
15359 | 91 |
blocks: Vec<DataBlock>, |
92 |
block_masks: Vec<u64>, |
|
93 |
element_sizes: Box<[u16; 64]>, |
|
94 |
lookup: Box<[LookupEntry]>, |
|
15310 | 95 |
} |
96 |
||
97 |
impl GearDataManager { |
|
98 |
pub fn new() -> Self { |
|
99 |
Self { |
|
100 |
types: vec![], |
|
15359 | 101 |
blocks: vec![], |
102 |
block_masks: vec![], |
|
103 |
element_sizes: Box::new([0; 64]), |
|
104 |
lookup: vec![LookupEntry::default(); u16::max_value() as usize].into_boxed_slice(), |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
#[inline] |
|
109 |
fn get_type_index<T: 'static>(&self) -> Option<usize> { |
|
110 |
let type_id = TypeId::of::<T>(); |
|
111 |
self.types.iter().position(|id| *id == type_id) |
|
112 |
} |
|
113 |
||
114 |
fn move_between_blocks(&mut self, from_block_index: u16, from_index: u16, to_block_index: u16) { |
|
15362 | 115 |
debug_assert!(from_block_index != to_block_index); |
15359 | 116 |
let source_mask = self.block_masks[from_block_index as usize]; |
117 |
let destination_mask = self.block_masks[to_block_index as usize]; |
|
118 |
debug_assert!(source_mask & destination_mask == source_mask); |
|
119 |
||
120 |
let source = &self.blocks[from_block_index as usize]; |
|
121 |
let destination = &self.blocks[to_block_index as usize]; |
|
15362 | 122 |
debug_assert!(from_index < source.elements_count); |
123 |
debug_assert!(!destination.is_full()); |
|
124 |
||
15359 | 125 |
for i in 0..64 { |
15363 | 126 |
if source_mask & 1 << i != 0 { |
15362 | 127 |
unsafe { |
128 |
copy_nonoverlapping( |
|
15363 | 129 |
source.component_blocks[i].unwrap().as_ptr(), |
130 |
destination.component_blocks[i].unwrap().as_ptr(), |
|
15362 | 131 |
self.element_sizes[i] as usize, |
132 |
); |
|
133 |
} |
|
134 |
} |
|
15359 | 135 |
} |
15362 | 136 |
self.blocks[from_block_index as usize].elements_count -= 1; |
137 |
self.blocks[to_block_index as usize].elements_count += 1; |
|
15359 | 138 |
} |
139 |
||
15361 | 140 |
fn add_to_block<T: Clone>(&mut self, block_index: u16, value: &T) { |
141 |
debug_assert!(self.block_masks[block_index as usize].count_ones() == 1); |
|
142 |
||
143 |
let block = &mut self.blocks[block_index as usize]; |
|
144 |
debug_assert!(block.elements_count < block.max_elements); |
|
145 |
||
146 |
unsafe { |
|
147 |
let slice = slice::from_raw_parts_mut( |
|
148 |
block.data.as_mut_ptr() as *mut T, |
|
149 |
block.max_elements as usize, |
|
150 |
); |
|
151 |
*slice.get_unchecked_mut(block.elements_count as usize) = value.clone(); |
|
152 |
}; |
|
153 |
block.elements_count += 1; |
|
15359 | 154 |
} |
155 |
||
156 |
fn remove_from_block(&mut self, block_index: u16, index: u16) { |
|
15361 | 157 |
let block = &mut self.blocks[block_index as usize]; |
158 |
debug_assert!(index < block.elements_count); |
|
159 |
||
160 |
for (i, size) in self.element_sizes.iter().cloned().enumerate() { |
|
161 |
if index < block.elements_count - 1 { |
|
15363 | 162 |
if let Some(ptr) = block.component_blocks[i] { |
15361 | 163 |
unsafe { |
15362 | 164 |
copy_nonoverlapping( |
15361 | 165 |
ptr.as_ptr() |
166 |
.add((size * (block.elements_count - 1)) as usize), |
|
167 |
ptr.as_ptr().add((size * index) as usize), |
|
168 |
size as usize, |
|
169 |
); |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
173 |
} |
|
174 |
block.elements_count -= 1; |
|
15359 | 175 |
} |
176 |
||
177 |
#[inline] |
|
15363 | 178 |
fn ensure_block(&mut self, mask: u64) -> u16 { |
15359 | 179 |
if let Some(index) = self |
180 |
.block_masks |
|
181 |
.iter() |
|
182 |
.enumerate() |
|
183 |
.position(|(i, m)| *m == mask && !self.blocks[i].is_full()) |
|
184 |
{ |
|
185 |
index as u16 |
|
186 |
} else { |
|
187 |
self.blocks.push(DataBlock::new(mask, &self.element_sizes)); |
|
15363 | 188 |
self.block_masks.push(mask); |
15359 | 189 |
(self.blocks.len() - 1) as u16 |
190 |
} |
|
191 |
} |
|
192 |
||
193 |
pub fn add<T: Clone + 'static>(&mut self, gear_id: GearId, value: &T) { |
|
194 |
if let Some(type_index) = self.get_type_index::<T>() { |
|
15363 | 195 |
let type_bit = 1 << type_index as u64; |
15359 | 196 |
let entry = self.lookup[gear_id.get() as usize - 1]; |
197 |
||
198 |
if let Some(index) = entry.index { |
|
199 |
let mask = self.block_masks[entry.block_index as usize]; |
|
200 |
let new_mask = mask | type_bit; |
|
201 |
||
202 |
if new_mask != mask { |
|
15363 | 203 |
let dest_block_index = self.ensure_block(new_mask); |
15359 | 204 |
self.move_between_blocks(entry.block_index, index.get() - 1, dest_block_index); |
205 |
} |
|
206 |
} else { |
|
15363 | 207 |
let dest_block_index = self.ensure_block(type_bit); |
15359 | 208 |
self.add_to_block(dest_block_index, value); |
209 |
} |
|
210 |
} else { |
|
211 |
panic!("Unregistered type") |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
pub fn remove<T: 'static>(&mut self, gear_id: GearId) { |
|
216 |
if let Some(type_index) = self.get_type_index::<T>() { |
|
217 |
let entry = self.lookup[gear_id.get() as usize - 1]; |
|
218 |
if let Some(index) = entry.index { |
|
15362 | 219 |
let destination_mask = |
15363 | 220 |
self.block_masks[entry.block_index as usize] & !(1 << type_index as u64); |
15362 | 221 |
|
222 |
if destination_mask == 0 { |
|
223 |
self.remove_all(gear_id) |
|
224 |
} else { |
|
15363 | 225 |
let destination_block_index = self.ensure_block(destination_mask); |
15362 | 226 |
self.move_between_blocks( |
227 |
entry.block_index, |
|
228 |
index.get() - 1, |
|
229 |
destination_block_index, |
|
230 |
); |
|
231 |
} |
|
15359 | 232 |
} |
15362 | 233 |
} else { |
234 |
panic!("Unregistered type") |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
pub fn remove_all(&mut self, gear_id: GearId) { |
|
239 |
let entry = self.lookup[gear_id.get() as usize - 1]; |
|
240 |
if let Some(index) = entry.index { |
|
241 |
self.remove_from_block(entry.block_index, index.get() - 1); |
|
15310 | 242 |
} |
243 |
} |
|
244 |
||
245 |
pub fn register<T: 'static>(&mut self) { |
|
15361 | 246 |
debug_assert!(!std::mem::needs_drop::<T>()); |
247 |
debug_assert!(self.types.len() <= 64); |
|
248 |
debug_assert!(size_of::<T>() <= u16::max_value() as usize); |
|
15359 | 249 |
|
15310 | 250 |
let id = TypeId::of::<T>(); |
251 |
if !self.types.contains(&id) { |
|
15359 | 252 |
self.element_sizes[self.types.len()] = size_of::<T>() as u16; |
15310 | 253 |
self.types.push(id); |
254 |
} |
|
255 |
} |
|
256 |
||
15372
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
257 |
pub fn iter<T: TypeTuple + 'static, F: FnMut(T)>(&self, mut f: F) { |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
258 |
let mut arg_types = Vec::with_capacity(64); |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
259 |
T::get_types(&mut arg_types); |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
260 |
|
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
261 |
let mut type_indices = vec![-1i8; arg_types.len()]; |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
262 |
let mut selector = 0u64; |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
263 |
|
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
264 |
for (arg_index, type_id) in arg_types.iter().enumerate() { |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
265 |
match self.types.iter().position(|t| t == type_id) { |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
266 |
Some(i) if selector & 1 << i as u64 != 0 => panic!("Duplicate type"), |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
267 |
Some(i) => { |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
268 |
type_indices[arg_index] = i as i8; |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
269 |
selector &= 1 << i as u64; |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
270 |
}, |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
271 |
None => panic!("Unregistered type") |
15310 | 272 |
} |
273 |
} |
|
274 |
||
15372
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
275 |
let mut slices = vec![null_mut(); arg_types.len()]; |
15363 | 276 |
|
15359 | 277 |
for (block_index, mask) in self.block_masks.iter().enumerate() { |
278 |
if mask & selector == selector { |
|
279 |
let block = &self.blocks[block_index]; |
|
15363 | 280 |
|
15372
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
281 |
for (arg_index, type_index) in type_indices.iter().cloned().enumerate() { |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
282 |
slices[arg_index as usize] = |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15363
diff
changeset
|
283 |
block.component_blocks[type_index as usize].unwrap().as_ptr() |
15359 | 284 |
} |
15363 | 285 |
|
286 |
unsafe { |
|
287 |
T::iter(&slices[..], block.elements_count as usize, |x| f(x)); |
|
288 |
} |
|
15310 | 289 |
} |
290 |
} |
|
291 |
} |
|
292 |
} |
|
15359 | 293 |
|
294 |
#[cfg(test)] |
|
295 |
mod test { |
|
15363 | 296 |
use super::{super::common::GearId, GearDataManager}; |
15359 | 297 |
|
15363 | 298 |
#[derive(Clone)] |
15359 | 299 |
struct Datum { |
300 |
value: u32, |
|
301 |
} |
|
302 |
||
303 |
#[test] |
|
15363 | 304 |
fn single_component_iteration() { |
305 |
assert!(std::mem::size_of::<Datum>() > 0); |
|
306 |
||
15359 | 307 |
let mut manager = GearDataManager::new(); |
308 |
manager.register::<Datum>(); |
|
15363 | 309 |
for i in 1..=5 { |
310 |
manager.add(GearId::new(i as u16).unwrap(), &Datum { value: i }); |
|
311 |
} |
|
312 |
||
313 |
let mut sum = 0; |
|
314 |
manager.iter(|(d,): (&Datum,)| sum += d.value); |
|
315 |
||
316 |
assert_eq!(sum, 15); |
|
15359 | 317 |
} |
318 |
} |