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