1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use crate::{
	buffer_pool::AlignedBufferPoolInner,
	pool::{Pool, PoolAllocator},
	BufferRetentionPolicy,
};
use aligned_buffer::{SharedAlignedBuffer, UniqueAlignedBuffer};
use fxhash::FxHashMap;
use std::{
	alloc,
	collections::hash_map,
	fmt, mem,
	ptr::{self, NonNull},
	sync::{Arc, Weak},
};

struct FxUnify {
	map: FxHashMap<usize, usize>,
}

impl FxUnify {
	fn new() -> Self {
		Self {
			map: FxHashMap::default(),
		}
	}
}

struct SharingAllocator;

impl PoolAllocator<FxUnify> for SharingAllocator {
	fn allocate(&self) -> FxUnify {
		FxUnify {
			map: FxHashMap::default(),
		}
	}

	#[inline(always)]
	fn is_valid(&self, unify: &FxUnify) -> bool {
		unify.map.capacity() < 256
	}

	#[inline(always)]
	fn reset(&self, unify: &mut FxUnify) {
		unify.map.clear();
	}
}

struct Inner<P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize> {
	writers: AlignedBufferPoolInner<P, ALIGNMENT>,
	scratch: AlignedBufferPoolInner<P, ALIGNMENT>,
	unify: Pool<SharingAllocator, FxUnify>,
}

pub struct SerializerPool<P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize> {
	inner: Arc<Inner<P, ALIGNMENT>>,
}

impl<P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize> SerializerPool<P, ALIGNMENT> {
	pub fn new(policy: P, capacity: usize) -> Self {
		let writers = AlignedBufferPoolInner::new(policy, capacity);
		let scratch = AlignedBufferPoolInner::new(policy, capacity);
		let unify_pool = Pool::new(SharingAllocator, capacity);

		Self {
			inner: Arc::new(Inner {
				writers,
				scratch,
				unify: unify_pool,
			}),
		}
	}

	pub fn get(&self) -> Serializer<P, ALIGNMENT> {
		let writer = self.inner.writers.get();
		let scratch = self.inner.scratch.get();
		let unify = self.inner.unify.get();

		Serializer {
			pool: Arc::downgrade(&self.inner),
			writer,
			scratch,
			unify,
		}
	}

	pub fn recycle_buffer(&self, buffer: UniqueAlignedBuffer<ALIGNMENT>) {
		self.inner.writers.recycle(buffer);
	}

	pub fn serialize<
		T: rkyv::Serialize<rkyv::rancor::Strategy<Serializer<P, ALIGNMENT>, rkyv::rancor::BoxedError>>,
	>(
		&self,
		value: &T,
	) -> Result<UniqueAlignedBuffer<ALIGNMENT>, rkyv::rancor::BoxedError> {
		let mut buf = self.get();
		buf.serialize(value)?;
		Ok(buf.into_buffer())
	}
}

impl<P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize> SerializerPool<P, ALIGNMENT>
where
	P: Default,
{
	pub fn with_capacity(capacity: usize) -> Self {
		Self::new(P::default(), capacity)
	}
}

pub struct Serializer<P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize> {
	pool: Weak<Inner<P, ALIGNMENT>>,
	writer: UniqueAlignedBuffer<ALIGNMENT>,
	scratch: UniqueAlignedBuffer<ALIGNMENT>,
	unify: FxUnify,
}

impl<P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize> Drop
	for Serializer<P, ALIGNMENT>
{
	fn drop(&mut self) {
		if let Some(pool) = self.pool.upgrade() {
			let writer = mem::replace(&mut self.writer, UniqueAlignedBuffer::new());
			let scratch = mem::replace(&mut self.scratch, UniqueAlignedBuffer::new());
			let unify = mem::replace(&mut self.unify, FxUnify::new());

			pool.writers.recycle(writer);
			pool.scratch.recycle(scratch);
			pool.unify.recycle(unify);
		}
	}
}

impl<P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize> Serializer<P, ALIGNMENT> {
	pub fn into_buffer(mut self) -> UniqueAlignedBuffer<ALIGNMENT> {
		if let Some(pool) = self.pool.upgrade() {
			let scratch = mem::replace(&mut self.scratch, UniqueAlignedBuffer::new());
			let unify = mem::replace(&mut self.unify, FxUnify::new());

			pool.scratch.recycle(scratch);
			pool.unify.recycle(unify);
		}

		// prevent the new values from being inserted into the pool
		self.pool = Weak::new();
		mem::replace(&mut self.writer, UniqueAlignedBuffer::new())
	}

	pub fn serialize<T: rkyv::Serialize<rkyv::rancor::Strategy<Self, rkyv::rancor::BoxedError>>>(
		&mut self,
		value: &T,
	) -> Result<(), rkyv::rancor::BoxedError> {
		rkyv::util::serialize(value, self)
	}
}

impl<P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize> From<Serializer<P, ALIGNMENT>>
	for UniqueAlignedBuffer<ALIGNMENT>
{
	#[inline]
	fn from(serializer: Serializer<P, ALIGNMENT>) -> Self {
		serializer.into_buffer()
	}
}

impl<P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize> From<Serializer<P, ALIGNMENT>>
	for SharedAlignedBuffer<ALIGNMENT>
{
	#[inline]
	fn from(serializer: Serializer<P, ALIGNMENT>) -> Self {
		serializer.into_buffer().into_shared()
	}
}

impl<E: rkyv::rancor::Error, P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize>
	rkyv::ser::sharing::Sharing<E> for Serializer<P, ALIGNMENT>
{
	#[inline]
	fn get_shared_ptr(&self, address: usize) -> Option<usize> {
		self.unify.map.get(&address).copied()
	}

	fn add_shared_ptr(&mut self, address: usize, pos: usize) -> Result<(), E> {
		match self.unify.map.entry(address) {
			hash_map::Entry::Occupied(_) => {
				rkyv::rancor::fail!(DuplicateSharedPointer { address });
			}
			hash_map::Entry::Vacant(e) => {
				e.insert(pos);
				Ok(())
			}
		}
	}
}

impl<E: rkyv::rancor::Error, P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize>
	rkyv::ser::Allocator<E> for Serializer<P, ALIGNMENT>
{
	#[inline]
	unsafe fn push_alloc(&mut self, layout: alloc::Layout) -> Result<ptr::NonNull<[u8]>, E> {
		let scratch: &mut UniqueAlignedBuffer<ALIGNMENT> = &mut self.scratch;
		let alloc_offset = scratch.as_ptr() as usize + scratch.len();
		let alloc_pad = 0usize.wrapping_sub(alloc_offset) % layout.align();
		let alloc_len = alloc_pad + layout.size();
		scratch.reserve(alloc_len);

		// we need to make sure the bytes are initialized, as `UniqueAlignedBuffer` allows creating a slice
		// form 0 to `len` - and assumes that the bytes are initialized.
		let ptr = scratch.as_mut_ptr().wrapping_add(scratch.len());
		ptr.write_bytes(0u8, alloc_len);

		let old_len = scratch.len();
		let start = old_len + alloc_pad;
		scratch.set_len(scratch.len() + alloc_len);

		let result_slice =
			ptr::slice_from_raw_parts_mut(scratch.as_mut_ptr().wrapping_add(start), layout.size());

		debug_assert!((*result_slice).iter().all(|&b| b == 0));
		Ok(NonNull::new_unchecked(result_slice))
	}

	#[inline]
	unsafe fn pop_alloc(&mut self, ptr: ptr::NonNull<u8>, layout: alloc::Layout) -> Result<(), E> {
		let scratch: &mut UniqueAlignedBuffer<ALIGNMENT> = &mut self.scratch;

		let bytes = scratch.as_mut();
		let ptr = ptr.as_ptr();

		if bytes.as_mut_ptr_range().contains(&ptr) {
			let popped_pos = ptr.offset_from(bytes.as_mut_ptr()) as usize;
			if popped_pos + layout.size() <= scratch.len() {
				scratch.set_len(popped_pos);
				Ok(())
			} else {
				rkyv::rancor::fail!(BufferAllocError::NotPoppedInReverseOrder {
					pos: scratch.len(),
					popped_pos,
					popped_size: layout.size(),
				});
			}
		} else {
			rkyv::rancor::fail!(BufferAllocError::DoesNotContainAllocation);
		}
	}
}

impl<P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize> rkyv::ser::Positional
	for Serializer<P, ALIGNMENT>
{
	#[inline(always)]
	fn pos(&self) -> usize {
		rkyv::ser::Positional::pos(&self.writer)
	}
}

impl<E: rkyv::rancor::Error, P: BufferRetentionPolicy<ALIGNMENT>, const ALIGNMENT: usize>
	rkyv::ser::Writer<E> for Serializer<P, ALIGNMENT>
{
	#[inline(always)]
	fn write(&mut self, bytes: &[u8]) -> Result<(), E> {
		rkyv::ser::Writer::write(&mut self.writer, bytes).map_err(E::new)
	}
}

#[derive(Debug)]
struct DuplicateSharedPointer {
	address: usize,
}

impl fmt::Display for DuplicateSharedPointer {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(
			f,
			"duplicate shared pointer: {:#.*x}",
			mem::size_of::<usize>() * 2,
			self.address
		)
	}
}

impl std::error::Error for DuplicateSharedPointer {}

#[derive(Debug)]
enum BufferAllocError {
	NotPoppedInReverseOrder {
		pos: usize,
		popped_pos: usize,
		popped_size: usize,
	},
	DoesNotContainAllocation,
}

impl fmt::Display for BufferAllocError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::NotPoppedInReverseOrder {
				pos,
				popped_pos,
				popped_size,
			} => write!(
				f,
				"allocation popped at {} with length {} runs past buffer allocator start {}",
				popped_pos, popped_size, pos,
			),
			Self::DoesNotContainAllocation => {
				write!(f, "allocator does not contain popped allocation")
			}
		}
	}
}

impl std::error::Error for BufferAllocError {}

#[cfg(test)]
mod tests {
	use crate::RetainAllRetentionPolicy;

	use super::*;
	use rkyv::{Archive, Deserialize, Serialize};

	#[derive(Archive, Serialize, Deserialize)]
	#[archive(check_bytes)]
	struct TestStruct1 {
		name: String,
		boxed_name: Box<str>,
		age: u16,
	}

	impl TestStruct1 {
		fn new(name: &str, age: u16) -> Self {
			Self {
				name: name.to_string(),
				boxed_name: name.into(),
				age,
			}
		}
	}

	fn check(buf: &[u8], value: &TestStruct1) {
		let archived =
			rkyv::access::<TestStruct1, rkyv::rancor::BoxedError>(buf).expect("failed to access");

		assert_eq!(archived.name, value.name);
		assert_eq!(archived.boxed_name, value.boxed_name);
		assert_eq!(archived.age, value.age);
	}

	#[test]
	fn serialization_buffer() {
		let pool = SerializerPool::<RetainAllRetentionPolicy, 64>::with_capacity(10);
		let test1 = TestStruct1::new("test1", 10);
		let test2 = TestStruct1::new("test2", 20);

		let buf1 = pool.serialize(&test1).expect("failed to serialize");
		check(&buf1, &test1);

		let buf2 = pool.serialize(&test2).expect("failed to serialize");
		check(&buf2, &test2);

		pool.recycle_buffer(buf1);
		pool.recycle_buffer(buf2);
	}

	#[test]
	fn reuse_buffer() {
		let pool = SerializerPool::<RetainAllRetentionPolicy, 64>::with_capacity(10);

		let test1 = TestStruct1::new("Test Name 1", 10);
		let buf = pool.serialize(&test1).expect("failed to serialize");
		check(&buf, &test1);
		let initial_cap = buf.capacity();
		pool.recycle_buffer(buf);

		let test2 = TestStruct1::new("Test Name 2", 20);
		let buf = pool.serialize(&test2).expect("failed to serialize");
		check(&buf, &test2);
		pool.recycle_buffer(buf);

		let test3 = TestStruct1::new("Test Name 3", 30);
		let buf = pool.serialize(&test3).expect("failed to serialize");
		check(&buf, &test3);
		pool.recycle_buffer(buf);

		let test4 = TestStruct1::new("Test Name 4", 40);
		let buf = pool.serialize(&test4).expect("failed to serialize");
		check(&buf, &test4);
		debug_assert_eq!(buf.capacity(), initial_cap);
		pool.recycle_buffer(buf);
	}

	#[test]
	fn discard_buffers() {
		let pool = SerializerPool::<RetainAllRetentionPolicy, 64>::with_capacity(10);

		for i in 0..20 {
			let test = TestStruct1::new(&format!("Test Name {}", i), i * 10);
			let buf = pool.serialize(&test).expect("failed to serialize");
			check(&buf, &test);
		}
	}
}