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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! A high-performance object pool that reuses allocations instead of freeing them.
//!
//! # Quick Start
//!
//! ```
//! use poolshark::local::LPooled;
//! use std::collections::HashMap;
//!
//! // Take a HashMap from the thread-local pool (or create new if empty)
//! let mut map: LPooled<HashMap<String, i32>> = LPooled::take();
//! map.insert("answer".to_string(), 42);
//! // When dropped, the HashMap is cleared and returned to the pool
//! ```
//!
//! # Which Pool Should I Use?
//!
//! - **Use [`local::LPooled`]** (default choice): Faster, for objects created and dropped on the same thread(s)
//! - **Use [`global::GPooled`]**: When one thread creates objects and other threads drop them (producer-consumer)
//!
//! # Why Poolshark?
//!
//! - **Reduce allocations**: Reuse containers instead of repeatedly allocating and freeing
//! - **Predictable performance**: Consistent behavior across platforms, independent of allocator
//! - **Fast**: Local pools avoid atomic operations and are more ergonomic than `thread_local!`
//! - **Flexible**: Choose between fast thread-local pools or lock-free cross-thread pools
//!
//! # Pool Types
//!
//! ## Global Pooling
//!
//! Global pools share objects between threads (see [`global::GPooled`]).
//! An object taken from a global pool always returns to the pool it was
//! taken from, regardless of which thread drops it. Use this for producer-consumer
//! patterns where one thread creates objects and other threads consume them.
//!
//! There are several different ways to use global pools. You can use
//! [take](global::take) or [take_any](global::take_any) to just take objects
//! from thread local global pools. If you need better performance you can use
//! [pool](global::pool) or [pool_any](global::pool_any) and then store the pool
//! somewhere. If you don't have anywhere to store the pool you can use a static
//! [LazyLock](std::sync::LazyLock) for a truly global named pool. For example,
//!
//! ```no_run
//! use std::{sync::LazyLock, collections::HashMap};
//! use poolshark::global::{Pool, GPooled};
//!
//! type Widget = HashMap<usize, usize>;
//!
//! // create a global static widget pool that will accept up to 1024 widgets with
//! // up to 64 elements of capacity each
//! static WIDGETS: LazyLock<Pool<Widget>> = LazyLock::new(|| Pool::new(1024, 64));
//!
//! fn widget_maker() -> GPooled<Widget> {
//! let mut w = WIDGETS.take();
//! w.insert(42, 42);
//! w
//! }
//!
//! fn widget_user(w: GPooled<Widget>) {
//! drop(w) // puts the widget back in the WIDGETS pool
//! }
//! ```
//!
//! ## Local Pooling
//!
//! Local pools (see [`local::LPooled`]) always return dropped objects to a thread-local
//! pool on whichever thread drops them. They are significantly faster than global pools
//! because they avoid atomic operations. Use local pools by default unless you have
//! a cross-thread producer-consumer pattern.
//!
//! **Thread safety**: `LPooled<T>` is `Send + Sync` whenever `T` is `Send + Sync`, so you can
//! safely pass pooled objects between threads.
//!
//! Local pools require types to implement the unsafe trait [`IsoPoolable`], but all
//! standard containers (Vec, HashMap, String, etc.) already implement it.
//!
//! ```no_run
//! use poolshark::local::LPooled;
//! use std::collections::HashMap;
//!
//! type Widget = HashMap<usize, usize>;
//!
//! fn widget_maker() -> LPooled<Widget> {
//! let mut w = LPooled::<Widget>::default(); // takes from the local pool
//! w.insert(42, 42);
//! w
//! }
//!
//! fn widget_user(w: LPooled<Widget>) {
//! drop(w) // puts the widget back in the local pool
//! }
//! ```
use WeakPool;
pub use location_id;
use ;
/// A globally unique id for a source code position
///
/// use poolshark_derive::location_id!() macro to generate one
;
// msb 0 -> it's a layout
// msb 1 -> it's a size
//
// layout: 1 bit flag, 12 bit size, 3 bit align
//
// aligns
// 0x0 -> 1
// 0x1 -> 2
// 0x2 -> 4
// 0x3 -> 8
// 0x4 -> 16
//
// size: 1 bit flag, 15 bit size
;
/// Type describing the layout, alignment, and type of a container
///
/// `Discriminant` is central to the safety and performance of local pooling. It
/// describes 2 things in just 8 bytes.
///
/// - The unique location in the source code of the implementation of
/// [IsoPoolable]. This is accomplished by a proc macro that generates a global
/// table of unique location ids for cross crate source code locations. This
/// unique id ensures that different container types can't be mixed in the same
/// pool.
///
/// - The layout and alignment of all the type parameters of the
/// container. Discriminant has 3 slots that can be filled with either
/// type parameters or const SIZE parameters. If your container has
/// more parameters than that then you can't locally pool it, and you
/// can't implement [IsoPoolable]. If you try you will likely cause
/// undefined behavior.
///
/// In order to squeeze all this information into just 8 bytes there are some
/// limitations.
///
/// - You can't have more than 0xFFFF implementations of [IsoPoolable] in the
/// same project. This includes all the crates depended on by the project.
///
/// - Your type parameters must have size <= 0x0FFF bytes and
/// alignment of 1, 2, 4, 8, or 16. Alignments > 16 will be rejected.
///
/// - const SIZE parameters must be <= 0x7FFF.
///
/// If any of these constraints are violated the `Discriminant` constructors
/// will return `None`. If you desire you may panic at that point to cause a
/// compile error. If you do not panic and instead leave `DISCRIMINANT` as
/// `None` then local pool operations on that type will work just fine, but
/// nothing will be pooled. Objects will be freed when they are dropped and
/// [take](local::take) will allocate new objects each time it is called.
///
/// # Discriminant Collisions and Why They're Safe
///
/// Two different types can have the same discriminant if they have the same size and
/// alignment. For example:
///
/// ```ignore
/// #[repr(C)]
/// struct Padded1 { a: u8, _pad: [u8; 7], b: u64 } // size 16, align 8
///
/// #[repr(C)]
/// struct Padded2 { x: u64, y: u64 } // size 16, align 8
/// ```
///
/// If you pool `Vec<Padded1>` and `Vec<Padded2>`, they would get the same discriminant
/// because `Padded1` and `Padded2` have identical size and alignment. This means a
/// `Vec<Padded1>` allocation could be reused as a `Vec<Padded2>` allocation.
///
/// **This is safe** because:
///
/// 1. Containers are **always empty** when returned to pools (`reset()` ensures this)
/// 2. An empty `Vec<T>` only cares about `T`'s size and alignment for its allocation
/// 3. The actual bit patterns inside `T` don't matter when the Vec is empty
/// 4. When you take from the pool and populate it with your type, it's initialized correctly
///
/// The discriminant system is designed to ensure that different container **types** never
/// share pools (via the `LocationId`), and that the **memory layout** of type parameters
/// is compatible. As long as containers are properly emptied before pooling (which `reset()`
/// guarantees), the system is memory safe even with discriminant collisions.
/// Trait for poolable objects
/// Low level global pool trait for maximum control
///
/// Implementing this trait allows full low level control over where the pool
/// pointer is stored. For example if you are pooling an allocated data
/// structure, you could store the pool pointer in the allocation to keep the
/// size of the handle struct to a minimum. E.G. you're pooling a
/// [triomphe::ThinArc]. Or, if you have a static global pool, then you would
/// not need to keep a pool pointer at all.
///
/// The object's drop implementation should return the object to the
/// pool instead of deallocating it
///
/// Implementing this trait correctly is extremely tricky, and requires unsafe
/// code, therefore it is marked as unsafe.
///
/// Most of the time you should use the [GPooled](global::GPooled) wrapper.
pub unsafe
/// Trait for isomorphicly poolable objects.
///
/// That is objects that can safely be pooled by memory layout and container
/// type. For example two `HashMap`s, `HashMap<usize, usize>` and
/// `HashMap<ArcStr, ArcStr>` are isomorphic, their memory allocations can be
/// used interchangably so long as they are empty.
pub unsafe