author | alfadur |
Tue, 27 Aug 2019 20:09:17 +0300 | |
changeset 15367 | d6b4586b271f |
parent 15358 | b5e0a39856fd |
child 15368 | 445138f388d4 |
permissions | -rw-r--r-- |
15354 | 1 |
use super::common::GearId; |
2 |
use std::{ |
|
3 |
any::TypeId, |
|
4 |
mem::{size_of, MaybeUninit}, |
|
5 |
num::NonZeroU16, |
|
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
6 |
ptr::{copy_nonoverlapping, NonNull, null_mut}, |
15354 | 7 |
slice, |
8 |
}; |
|
15305 | 9 |
|
15356 | 10 |
pub unsafe trait TypeTuple: Sized { |
15305 | 11 |
fn len() -> usize; |
12 |
fn get_types(dest: &mut Vec<TypeId>); |
|
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
13 |
unsafe fn iter<F>(slices: &[*mut u8], count: usize, mut f: F) |
15354 | 14 |
where |
15358 | 15 |
F: FnMut(Self); |
15305 | 16 |
} |
17 |
||
15356 | 18 |
unsafe impl<T: 'static> TypeTuple for (&T,) { |
15305 | 19 |
fn len() -> usize { |
20 |
1 |
|
21 |
} |
|
22 |
||
23 |
fn get_types(dest: &mut Vec<TypeId>) { |
|
24 |
dest.push(TypeId::of::<T>()); |
|
25 |
} |
|
15354 | 26 |
|
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
27 |
unsafe fn iter<F>(slices: &[*mut u8], count: usize, mut f: F) |
15354 | 28 |
where |
15358 | 29 |
F: FnMut(Self), |
15354 | 30 |
{ |
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
31 |
let slice1 = slice::from_raw_parts(slices[0] as *const T, count); |
15354 | 32 |
for i in 0..count { |
33 |
f((slice1.get_unchecked(i),)); |
|
34 |
} |
|
35 |
} |
|
15305 | 36 |
} |
37 |
||
15354 | 38 |
const BLOCK_SIZE: usize = 32768; |
39 |
||
40 |
struct DataBlock { |
|
41 |
max_elements: u16, |
|
42 |
elements_count: u16, |
|
43 |
data: Box<[u8; BLOCK_SIZE]>, |
|
15358 | 44 |
component_blocks: [Option<NonNull<u8>>; 64], |
15305 | 45 |
} |
46 |
||
15354 | 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() |
|
15358 | 54 |
.filter(|(i, _)| mask & (1 << *i as u64) != 0) |
15354 | 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]> = |
|
15357 | 60 |
Box::new(unsafe { MaybeUninit::uninit().assume_init() }); |
15354 | 61 |
let mut blocks = [None; 64]; |
62 |
let mut offset = 0; |
|
63 |
||
64 |
for i in 0..64 { |
|
15358 | 65 |
if mask & (1 << i) != 0 { |
15354 | 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 |
} |
|
15305 | 70 |
Self { |
15354 | 71 |
elements_count: 0, |
72 |
max_elements, |
|
73 |
data, |
|
15358 | 74 |
component_blocks: blocks, |
15305 | 75 |
} |
76 |
} |
|
77 |
||
15354 | 78 |
fn is_full(&self) -> bool { |
79 |
self.elements_count == self.max_elements |
|
80 |
} |
|
15305 | 81 |
} |
82 |
||
15354 | 83 |
#[derive(Clone, Copy, Debug, Default)] |
84 |
pub struct LookupEntry { |
|
85 |
index: Option<NonZeroU16>, |
|
86 |
block_index: u16, |
|
15305 | 87 |
} |
88 |
||
89 |
pub struct GearDataManager { |
|
90 |
types: Vec<TypeId>, |
|
15354 | 91 |
blocks: Vec<DataBlock>, |
92 |
block_masks: Vec<u64>, |
|
93 |
element_sizes: Box<[u16; 64]>, |
|
94 |
lookup: Box<[LookupEntry]>, |
|
15305 | 95 |
} |
96 |
||
97 |
impl GearDataManager { |
|
98 |
pub fn new() -> Self { |
|
99 |
Self { |
|
100 |
types: vec![], |
|
15354 | 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) { |
|
15357 | 115 |
debug_assert!(from_block_index != to_block_index); |
15354 | 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]; |
|
15357 | 122 |
debug_assert!(from_index < source.elements_count); |
123 |
debug_assert!(!destination.is_full()); |
|
124 |
||
15354 | 125 |
for i in 0..64 { |
15358 | 126 |
if source_mask & 1 << i != 0 { |
15357 | 127 |
unsafe { |
128 |
copy_nonoverlapping( |
|
15358 | 129 |
source.component_blocks[i].unwrap().as_ptr(), |
130 |
destination.component_blocks[i].unwrap().as_ptr(), |
|
15357 | 131 |
self.element_sizes[i] as usize, |
132 |
); |
|
133 |
} |
|
134 |
} |
|
15354 | 135 |
} |
15357 | 136 |
self.blocks[from_block_index as usize].elements_count -= 1; |
137 |
self.blocks[to_block_index as usize].elements_count += 1; |
|
15354 | 138 |
} |
139 |
||
15356 | 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; |
|
15354 | 154 |
} |
155 |
||
156 |
fn remove_from_block(&mut self, block_index: u16, index: u16) { |
|
15356 | 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 { |
|
15358 | 162 |
if let Some(ptr) = block.component_blocks[i] { |
15356 | 163 |
unsafe { |
15357 | 164 |
copy_nonoverlapping( |
15356 | 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; |
|
15354 | 175 |
} |
176 |
||
177 |
#[inline] |
|
15358 | 178 |
fn ensure_block(&mut self, mask: u64) -> u16 { |
15354 | 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)); |
|
15358 | 188 |
self.block_masks.push(mask); |
15354 | 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>() { |
|
15358 | 195 |
let type_bit = 1 << type_index as u64; |
15354 | 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 { |
|
15358 | 203 |
let dest_block_index = self.ensure_block(new_mask); |
15354 | 204 |
self.move_between_blocks(entry.block_index, index.get() - 1, dest_block_index); |
205 |
} |
|
206 |
} else { |
|
15358 | 207 |
let dest_block_index = self.ensure_block(type_bit); |
15354 | 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 { |
|
15357 | 219 |
let destination_mask = |
15358 | 220 |
self.block_masks[entry.block_index as usize] & !(1 << type_index as u64); |
15357 | 221 |
|
222 |
if destination_mask == 0 { |
|
223 |
self.remove_all(gear_id) |
|
224 |
} else { |
|
15358 | 225 |
let destination_block_index = self.ensure_block(destination_mask); |
15357 | 226 |
self.move_between_blocks( |
227 |
entry.block_index, |
|
228 |
index.get() - 1, |
|
229 |
destination_block_index, |
|
230 |
); |
|
231 |
} |
|
15354 | 232 |
} |
15357 | 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); |
|
15305 | 242 |
} |
243 |
} |
|
244 |
||
245 |
pub fn register<T: 'static>(&mut self) { |
|
15356 | 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); |
|
15354 | 249 |
|
15305 | 250 |
let id = TypeId::of::<T>(); |
251 |
if !self.types.contains(&id) { |
|
15354 | 252 |
self.element_sizes[self.types.len()] = size_of::<T>() as u16; |
15305 | 253 |
self.types.push(id); |
254 |
} |
|
255 |
} |
|
256 |
||
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
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:
15358
diff
changeset
|
258 |
let mut arg_types = Vec::with_capacity(64); |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
259 |
T::get_types(&mut arg_types); |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
260 |
|
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
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:
15358
diff
changeset
|
262 |
let mut selector = 0u64; |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
263 |
|
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
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:
15358
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:
15358
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:
15358
diff
changeset
|
267 |
Some(i) => { |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
268 |
type_indices[arg_index] = i as i8; |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
269 |
selector &= 1 << i as u64; |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
270 |
}, |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
271 |
None => panic!("Unregistered type") |
15305 | 272 |
} |
273 |
} |
|
274 |
||
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
275 |
let mut slices = vec![null_mut(); arg_types.len()]; |
15358 | 276 |
|
15354 | 277 |
for (block_index, mask) in self.block_masks.iter().enumerate() { |
278 |
if mask & selector == selector { |
|
279 |
let block = &self.blocks[block_index]; |
|
15358 | 280 |
|
15367
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
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:
15358
diff
changeset
|
282 |
slices[arg_index as usize] = |
d6b4586b271f
make sure component slice order corresponds to the type args
alfadur
parents:
15358
diff
changeset
|
283 |
block.component_blocks[type_index as usize].unwrap().as_ptr() |
15354 | 284 |
} |
15358 | 285 |
|
286 |
unsafe { |
|
287 |
T::iter(&slices[..], block.elements_count as usize, |x| f(x)); |
|
288 |
} |
|
15305 | 289 |
} |
290 |
} |
|
291 |
} |
|
292 |
} |
|
15354 | 293 |
|
294 |
#[cfg(test)] |
|
295 |
mod test { |
|
15358 | 296 |
use super::{super::common::GearId, GearDataManager}; |
15354 | 297 |
|
15358 | 298 |
#[derive(Clone)] |
15354 | 299 |
struct Datum { |
300 |
value: u32, |
|
301 |
} |
|
302 |
||
303 |
#[test] |
|
15358 | 304 |
fn single_component_iteration() { |
305 |
assert!(std::mem::size_of::<Datum>() > 0); |
|
306 |
||
15354 | 307 |
let mut manager = GearDataManager::new(); |
308 |
manager.register::<Datum>(); |
|
15358 | 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); |
|
15354 | 317 |
} |
318 |
} |