author | alfadur |
Wed, 28 Aug 2019 00:04:23 +0300 | |
changeset 15389 | 445138f388d4 |
parent 15388 | d6b4586b271f |
child 15390 | 0f2fd8d12734 |
permissions | -rw-r--r-- |
15375 | 1 |
use super::common::GearId; |
2 |
use std::{ |
|
3 |
any::TypeId, |
|
4 |
mem::{size_of, MaybeUninit}, |
|
5 |
num::NonZeroU16, |
|
15389 | 6 |
ptr::{copy_nonoverlapping, null_mut, NonNull}, |
15375 | 7 |
slice, |
8 |
}; |
|
15326 | 9 |
|
15389 | 10 |
pub trait TypeTuple: Sized { |
15326 | 11 |
fn len() -> usize; |
12 |
fn get_types(dest: &mut Vec<TypeId>); |
|
15389 | 13 |
unsafe fn iter<F: FnMut(Self)>(slices: &[*mut u8], count: usize, mut f: F); |
15326 | 14 |
} |
15 |
||
15389 | 16 |
macro_rules! type_typle_impl { |
17 |
($($n: literal: $t: ident),*) => { |
|
18 |
impl<$($t: 'static),*> TypeTuple for ($(&$t),*,) { |
|
19 |
fn len() -> usize { |
|
20 |
[$({TypeId::of::<$t>(); 1}),*].iter().sum() |
|
21 |
} |
|
22 |
||
23 |
fn get_types(types: &mut Vec<TypeId>) { |
|
24 |
$(types.push(TypeId::of::<$t>()));* |
|
25 |
} |
|
15326 | 26 |
|
15389 | 27 |
unsafe fn iter<F: FnMut(Self)>(slices: &[*mut u8], count: usize, mut f: F) |
28 |
{ |
|
29 |
for i in 0..count { |
|
30 |
unsafe { |
|
31 |
f(($(&*(*slices.get_unchecked($n) as *mut $t).add(i)),*,)); |
|
32 |
} |
|
33 |
} |
|
34 |
} |
|
35 |
} |
|
15375 | 36 |
|
15389 | 37 |
impl<$($t: 'static),*> TypeTuple for ($(&mut $t),*,) { |
38 |
fn len() -> usize { |
|
39 |
[$({TypeId::of::<$t>(); 1}),*].iter().sum() |
|
40 |
} |
|
41 |
||
42 |
fn get_types(types: &mut Vec<TypeId>) { |
|
43 |
$(types.push(TypeId::of::<$t>()));* |
|
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 { |
|
50 |
f(($(&mut *(*slices.get_unchecked($n) as *mut $t).add(i)),*,)); |
|
51 |
} |
|
52 |
} |
|
53 |
} |
|
15375 | 54 |
} |
55 |
} |
|
15326 | 56 |
} |
57 |
||
15389 | 58 |
type_typle_impl!(0: A); |
59 |
type_typle_impl!(0: A, 1: B); |
|
60 |
type_typle_impl!(0: A, 1: B, 2: C); |
|
61 |
type_typle_impl!(0: A, 1: B, 2: C, 3: D); |
|
62 |
type_typle_impl!(0: A, 1: B, 2: C, 3: D, 4: E); |
|
63 |
||
15375 | 64 |
const BLOCK_SIZE: usize = 32768; |
65 |
||
66 |
struct DataBlock { |
|
67 |
max_elements: u16, |
|
68 |
elements_count: u16, |
|
69 |
data: Box<[u8; BLOCK_SIZE]>, |
|
15380 | 70 |
component_blocks: [Option<NonNull<u8>>; 64], |
15326 | 71 |
} |
72 |
||
15375 | 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() |
|
15380 | 80 |
.filter(|(i, _)| mask & (1 << *i as u64) != 0) |
15375 | 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]> = |
|
15379 | 86 |
Box::new(unsafe { MaybeUninit::uninit().assume_init() }); |
15375 | 87 |
let mut blocks = [None; 64]; |
88 |
let mut offset = 0; |
|
89 |
||
90 |
for i in 0..64 { |
|
15380 | 91 |
if mask & (1 << i) != 0 { |
15375 | 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 |
} |
|
15326 | 96 |
Self { |
15375 | 97 |
elements_count: 0, |
98 |
max_elements, |
|
99 |
data, |
|
15380 | 100 |
component_blocks: blocks, |
15326 | 101 |
} |
102 |
} |
|
103 |
||
15375 | 104 |
fn is_full(&self) -> bool { |
105 |
self.elements_count == self.max_elements |
|
106 |
} |
|
15326 | 107 |
} |
108 |
||
15375 | 109 |
#[derive(Clone, Copy, Debug, Default)] |
110 |
pub struct LookupEntry { |
|
111 |
index: Option<NonZeroU16>, |
|
112 |
block_index: u16, |
|
15326 | 113 |
} |
114 |
||
115 |
pub struct GearDataManager { |
|
116 |
types: Vec<TypeId>, |
|
15375 | 117 |
blocks: Vec<DataBlock>, |
118 |
block_masks: Vec<u64>, |
|
119 |
element_sizes: Box<[u16; 64]>, |
|
120 |
lookup: Box<[LookupEntry]>, |
|
15326 | 121 |
} |
122 |
||
123 |
impl GearDataManager { |
|
124 |
pub fn new() -> Self { |
|
125 |
Self { |
|
126 |
types: vec![], |
|
15375 | 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) { |
|
15379 | 141 |
debug_assert!(from_block_index != to_block_index); |
15375 | 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]; |
|
15379 | 148 |
debug_assert!(from_index < source.elements_count); |
149 |
debug_assert!(!destination.is_full()); |
|
150 |
||
15375 | 151 |
for i in 0..64 { |
15380 | 152 |
if source_mask & 1 << i != 0 { |
15379 | 153 |
unsafe { |
154 |
copy_nonoverlapping( |
|
15380 | 155 |
source.component_blocks[i].unwrap().as_ptr(), |
156 |
destination.component_blocks[i].unwrap().as_ptr(), |
|
15379 | 157 |
self.element_sizes[i] as usize, |
158 |
); |
|
159 |
} |
|
160 |
} |
|
15375 | 161 |
} |
15379 | 162 |
self.blocks[from_block_index as usize].elements_count -= 1; |
163 |
self.blocks[to_block_index as usize].elements_count += 1; |
|
15375 | 164 |
} |
165 |
||
15378 | 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; |
|
15375 | 180 |
} |
181 |
||
182 |
fn remove_from_block(&mut self, block_index: u16, index: u16) { |
|
15378 | 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 { |
|
15380 | 188 |
if let Some(ptr) = block.component_blocks[i] { |
15378 | 189 |
unsafe { |
15379 | 190 |
copy_nonoverlapping( |
15378 | 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; |
|
15375 | 201 |
} |
202 |
||
203 |
#[inline] |
|
15380 | 204 |
fn ensure_block(&mut self, mask: u64) -> u16 { |
15375 | 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)); |
|
15380 | 214 |
self.block_masks.push(mask); |
15375 | 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>() { |
|
15380 | 221 |
let type_bit = 1 << type_index as u64; |
15375 | 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 { |
|
15380 | 229 |
let dest_block_index = self.ensure_block(new_mask); |
15375 | 230 |
self.move_between_blocks(entry.block_index, index.get() - 1, dest_block_index); |
231 |
} |
|
232 |
} else { |
|
15380 | 233 |
let dest_block_index = self.ensure_block(type_bit); |
15375 | 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 { |
|
15379 | 245 |
let destination_mask = |
15380 | 246 |
self.block_masks[entry.block_index as usize] & !(1 << type_index as u64); |
15379 | 247 |
|
248 |
if destination_mask == 0 { |
|
249 |
self.remove_all(gear_id) |
|
250 |
} else { |
|
15380 | 251 |
let destination_block_index = self.ensure_block(destination_mask); |
15379 | 252 |
self.move_between_blocks( |
253 |
entry.block_index, |
|
254 |
index.get() - 1, |
|
255 |
destination_block_index, |
|
256 |
); |
|
257 |
} |
|
15375 | 258 |
} |
15379 | 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); |
|
15326 | 268 |
} |
269 |
} |
|
270 |
||
271 |
pub fn register<T: 'static>(&mut self) { |
|
15378 | 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); |
|
15375 | 275 |
|
15326 | 276 |
let id = TypeId::of::<T>(); |
277 |
if !self.types.contains(&id) { |
|
15375 | 278 |
self.element_sizes[self.types.len()] = size_of::<T>() as u16; |
15326 | 279 |
self.types.push(id); |
280 |
} |
|
281 |
} |
|
282 |
||
15388
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15380
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:
15380
diff
changeset
|
284 |
let mut arg_types = Vec::with_capacity(64); |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15380
diff
changeset
|
285 |
T::get_types(&mut arg_types); |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15380
diff
changeset
|
286 |
|
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15380
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:
15380
diff
changeset
|
288 |
let mut selector = 0u64; |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15380
diff
changeset
|
289 |
|
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15380
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:
15380
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:
15380
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:
15380
diff
changeset
|
293 |
Some(i) => { |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15380
diff
changeset
|
294 |
type_indices[arg_index] = i as i8; |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15380
diff
changeset
|
295 |
selector &= 1 << i as u64; |
15389 | 296 |
} |
297 |
None => panic!("Unregistered type"), |
|
15326 | 298 |
} |
299 |
} |
|
300 |
||
15388
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15380
diff
changeset
|
301 |
let mut slices = vec![null_mut(); arg_types.len()]; |
15380 | 302 |
|
15375 | 303 |
for (block_index, mask) in self.block_masks.iter().enumerate() { |
304 |
if mask & selector == selector { |
|
305 |
let block = &self.blocks[block_index]; |
|
15380 | 306 |
|
15388
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15380
diff
changeset
|
307 |
for (arg_index, type_index) in type_indices.iter().cloned().enumerate() { |
15389 | 308 |
slices[arg_index as usize] = block.component_blocks[type_index as usize] |
309 |
.unwrap() |
|
310 |
.as_ptr() |
|
15375 | 311 |
} |
15380 | 312 |
|
313 |
unsafe { |
|
314 |
T::iter(&slices[..], block.elements_count as usize, |x| f(x)); |
|
315 |
} |
|
15326 | 316 |
} |
317 |
} |
|
318 |
} |
|
319 |
} |
|
15375 | 320 |
|
321 |
#[cfg(test)] |
|
322 |
mod test { |
|
15380 | 323 |
use super::{super::common::GearId, GearDataManager}; |
15375 | 324 |
|
15380 | 325 |
#[derive(Clone)] |
15375 | 326 |
struct Datum { |
327 |
value: u32, |
|
328 |
} |
|
329 |
||
330 |
#[test] |
|
15380 | 331 |
fn single_component_iteration() { |
332 |
assert!(std::mem::size_of::<Datum>() > 0); |
|
333 |
||
15375 | 334 |
let mut manager = GearDataManager::new(); |
335 |
manager.register::<Datum>(); |
|
15380 | 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); |
|
15389 | 342 |
assert_eq!(sum, 15); |
15380 | 343 |
|
15389 | 344 |
manager.iter(|(d,): (&mut Datum,)| d.value += 1); |
345 |
manager.iter(|(d,): (&Datum,)| sum += d.value); |
|
346 |
assert_eq!(sum, 35); |
|
15375 | 347 |
} |
348 |
} |