15354
|
1 |
use super::common::GearId;
|
|
2 |
use std::{
|
|
3 |
any::TypeId,
|
|
4 |
mem::{size_of, MaybeUninit},
|
|
5 |
num::NonZeroU16,
|
|
6 |
ptr::NonNull,
|
|
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>);
|
15354
|
13 |
unsafe fn iter<F>(slices: &[NonNull<u8>], count: usize, f: F)
|
|
14 |
where
|
|
15 |
F: Fn(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 |
|
|
27 |
unsafe fn iter<F>(slices: &[NonNull<u8>], count: usize, f: F)
|
|
28 |
where
|
|
29 |
F: Fn(Self),
|
|
30 |
{
|
|
31 |
let slice1 = slice::from_raw_parts(slices[0].as_ptr() as *const T, count);
|
|
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]>,
|
|
44 |
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()
|
|
54 |
.filter(|(i, _)| mask & (164 << *i as u64) != 0)
|
|
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]> =
|
|
60 |
Box::new(unsafe { std::mem::MaybeUninit::uninit().assume_init() });
|
|
61 |
let mut blocks = [None; 64];
|
|
62 |
let mut offset = 0;
|
|
63 |
|
|
64 |
for i in 0..64 {
|
|
65 |
if mask & (164 << i) != 0 {
|
|
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,
|
|
74 |
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) {
|
|
115 |
let source_mask = self.block_masks[from_block_index as usize];
|
|
116 |
let destination_mask = self.block_masks[to_block_index as usize];
|
|
117 |
debug_assert!(source_mask & destination_mask == source_mask);
|
|
118 |
|
|
119 |
let source = &self.blocks[from_block_index as usize];
|
|
120 |
let destination = &self.blocks[to_block_index as usize];
|
|
121 |
for i in 0..64 {
|
|
122 |
unimplemented!()
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
15356
|
126 |
fn add_to_block<T: Clone>(&mut self, block_index: u16, value: &T) {
|
|
127 |
debug_assert!(self.block_masks[block_index as usize].count_ones() == 1);
|
|
128 |
|
|
129 |
let block = &mut self.blocks[block_index as usize];
|
|
130 |
debug_assert!(block.elements_count < block.max_elements);
|
|
131 |
|
|
132 |
unsafe {
|
|
133 |
let slice = slice::from_raw_parts_mut(
|
|
134 |
block.data.as_mut_ptr() as *mut T,
|
|
135 |
block.max_elements as usize,
|
|
136 |
);
|
|
137 |
*slice.get_unchecked_mut(block.elements_count as usize) = value.clone();
|
|
138 |
};
|
|
139 |
block.elements_count += 1;
|
15354
|
140 |
}
|
|
141 |
|
|
142 |
fn remove_from_block(&mut self, block_index: u16, index: u16) {
|
15356
|
143 |
let block = &mut self.blocks[block_index as usize];
|
|
144 |
debug_assert!(index < block.elements_count);
|
|
145 |
|
|
146 |
for (i, size) in self.element_sizes.iter().cloned().enumerate() {
|
|
147 |
if index < block.elements_count - 1 {
|
|
148 |
if let Some(mut ptr) = block.blocks[i] {
|
|
149 |
unsafe {
|
|
150 |
std::ptr::copy_nonoverlapping(
|
|
151 |
ptr.as_ptr()
|
|
152 |
.add((size * (block.elements_count - 1)) as usize),
|
|
153 |
ptr.as_ptr().add((size * index) as usize),
|
|
154 |
size as usize,
|
|
155 |
);
|
|
156 |
}
|
|
157 |
}
|
|
158 |
}
|
|
159 |
}
|
|
160 |
block.elements_count -= 1;
|
15354
|
161 |
}
|
|
162 |
|
|
163 |
#[inline]
|
|
164 |
fn ensure_group(&mut self, mask: u64) -> u16 {
|
|
165 |
if let Some(index) = self
|
|
166 |
.block_masks
|
|
167 |
.iter()
|
|
168 |
.enumerate()
|
|
169 |
.position(|(i, m)| *m == mask && !self.blocks[i].is_full())
|
|
170 |
{
|
|
171 |
index as u16
|
|
172 |
} else {
|
|
173 |
self.blocks.push(DataBlock::new(mask, &self.element_sizes));
|
|
174 |
(self.blocks.len() - 1) as u16
|
|
175 |
}
|
|
176 |
}
|
|
177 |
|
|
178 |
pub fn add<T: Clone + 'static>(&mut self, gear_id: GearId, value: &T) {
|
|
179 |
if let Some(type_index) = self.get_type_index::<T>() {
|
|
180 |
let type_bit = 1u64 << type_index as u64;
|
|
181 |
let entry = self.lookup[gear_id.get() as usize - 1];
|
|
182 |
|
|
183 |
if let Some(index) = entry.index {
|
|
184 |
let mask = self.block_masks[entry.block_index as usize];
|
|
185 |
let new_mask = mask | type_bit;
|
|
186 |
|
|
187 |
if new_mask != mask {
|
|
188 |
let dest_block_index = self.ensure_group(new_mask);
|
|
189 |
self.move_between_blocks(entry.block_index, index.get() - 1, dest_block_index);
|
|
190 |
}
|
|
191 |
} else {
|
|
192 |
let dest_block_index = self.ensure_group(type_bit);
|
|
193 |
self.add_to_block(dest_block_index, value);
|
|
194 |
}
|
|
195 |
} else {
|
|
196 |
panic!("Unregistered type")
|
|
197 |
}
|
|
198 |
}
|
|
199 |
|
|
200 |
pub fn remove<T: 'static>(&mut self, gear_id: GearId) {
|
|
201 |
if let Some(type_index) = self.get_type_index::<T>() {
|
|
202 |
let entry = self.lookup[gear_id.get() as usize - 1];
|
|
203 |
if let Some(index) = entry.index {
|
|
204 |
self.remove_from_block(entry.block_index, index.get() - 1);
|
|
205 |
}
|
15305
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
pub fn register<T: 'static>(&mut self) {
|
15356
|
210 |
debug_assert!(!std::mem::needs_drop::<T>());
|
|
211 |
debug_assert!(self.types.len() <= 64);
|
|
212 |
debug_assert!(size_of::<T>() <= u16::max_value() as usize);
|
15354
|
213 |
|
15305
|
214 |
let id = TypeId::of::<T>();
|
|
215 |
if !self.types.contains(&id) {
|
15354
|
216 |
self.element_sizes[self.types.len()] = size_of::<T>() as u16;
|
15305
|
217 |
self.types.push(id);
|
|
218 |
}
|
|
219 |
}
|
|
220 |
|
|
221 |
fn create_selector(&self, types: &[TypeId]) -> u64 {
|
|
222 |
let mut selector = 0u64;
|
|
223 |
for (i, typ) in self.types.iter().enumerate() {
|
|
224 |
if types.contains(&typ) {
|
15354
|
225 |
selector |= 1u64 << (i as u64)
|
15305
|
226 |
}
|
|
227 |
}
|
|
228 |
selector
|
|
229 |
}
|
|
230 |
|
|
231 |
pub fn iter<T: TypeTuple + 'static, F: Fn(T) + Copy>(&self, f: F) {
|
|
232 |
let mut types = vec![];
|
|
233 |
T::get_types(&mut types);
|
15356
|
234 |
debug_assert!(types.iter().all(|t| self.types.contains(t)));
|
15354
|
235 |
|
15356
|
236 |
let types_count = types.len();
|
15305
|
237 |
let selector = self.create_selector(&types);
|
15356
|
238 |
|
15354
|
239 |
for (block_index, mask) in self.block_masks.iter().enumerate() {
|
|
240 |
if mask & selector == selector {
|
|
241 |
let block = &self.blocks[block_index];
|
|
242 |
for element_index in 0..block.max_elements {
|
15356
|
243 |
unsafe {
|
|
244 |
T::iter(unimplemented!(), block.elements_count as usize, f);
|
|
245 |
}
|
15354
|
246 |
}
|
15305
|
247 |
}
|
|
248 |
}
|
|
249 |
}
|
|
250 |
}
|
15354
|
251 |
|
|
252 |
#[cfg(test)]
|
|
253 |
mod test {
|
|
254 |
use super::GearDataManager;
|
|
255 |
|
|
256 |
struct Datum {
|
|
257 |
value: u32,
|
|
258 |
}
|
|
259 |
|
|
260 |
#[test]
|
|
261 |
fn iteration() {
|
|
262 |
let mut manager = GearDataManager::new();
|
|
263 |
manager.register::<Datum>();
|
|
264 |
manager.iter(|d: (&Datum,)| {});
|
|
265 |
}
|
|
266 |
}
|