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
/*! Avoid double indirection in nested smart pointers.
The [`Pierce`] stuct allows you to cache the deref result of doubly-nested smart pointers.
# Quick Example
```
# use std::sync::Arc;
# use pierce::Pierce;
let vec: Vec<i32> = vec![1, 2, 3];
let arc_vec = Arc::new(vec);
let pierce = Pierce::new(arc_vec);
// Here, the execution jumps directly to the slice to call `.get(...)`.
// Without Pierce it would have to jump to the Vec first,
// than from the Vec to the slice.
pierce.get(0).unwrap();
```
# Nested Smart Pointers
Smart Pointers can be nested to - in a way - combine their functionalities.
For example, with `Arc<Vec<i32>>`, a slice of i32 is managed by the wrapping [`Vec`] that is wrapped again by the [`Arc`][std::sync::Arc].
However, nesting comes at the cost of **double indirection**:
when we want to access the underlying data,
we must first follow the outer pointer to where the inner pointer lies,
then follow the inner pointer to where the underlying data is. Two [Deref]-ings. Two jumps.
```
# use std::sync::Arc;
let vec: Vec<i32> = vec![1, 2, 3];
let arc_vec = Arc::new(vec);
// Here, the `Arc<Vec<i32>>` is first dereferenced to the `Vec<i32>`,
// then the Vec is dereferenced to the underlying i32 slice,
// on which `.get(...)` is called.
arc_vec.get(0).unwrap();
```
# Pierce
The [`Pierce`] struct, provided by this crate,
can help reduce the performance cost of nesting smart pointers by **caching the deref result**.
We double-deref the nested smart pointer at the start, storing the address where the inner pointer points to.
We can then access the underlying data by just jumping to the stored address. One jump.
Here's a diagram of what it *might* look like.
```text
┌───────────────────────────┬───────────────────────────────┬──────────────────────────────────────────┐
│ Stack │ Heap │ Heap │
┌────────────┼───────────────────────────┼───────────────────────────────┼──────────────────────────────────────────┤
│ T │ │ │ │
│ │ ┌──────────────────┐ │ ┌───────────────────┐ │ ┌──────────────────────────────────┐ │
│ │ │Outer Pointer │ │ │Inner Pointer │ │ │Target │ │
│ │ │ │ │ │ │ │ │ │ │
│ │ │ T ────────────────────────► T::Target ─────────────────► <T::Target as Deref>::Target │ │
│ │ │ │ │ │ │ │ │ │ │
│ │ └──────────────────┘ │ └───────────────────┘ │ └──────────────────────────────────┘ │
│ │ │ │ │
├────────────┼───────────────────────────┼───────────────────────────────┼──────────────────────────────────────────┤
│ Pierce<T> │ │ │ │
│ │ ┌──────────────────┐ │ ┌───────────────────┐ │ ┌──────────────────────────────────┐ │
│ │ │Outer Pointer │ │ │Inner Pointer │ │ │Target │ │
│ │ │ │ │ │ │ │ │ │ │
│ │ │ T ────────────────────────► T::Target ─────────────────► <T::Target as Deref>::Target │ │
│ │ │ │ │ │ │ │ │ ▲ │ │
│ │ ├──────────────────┤ │ └───────────────────┘ │ └────────────────│─────────────────┘ │
│ │ │Cache │ │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ ptr ───────────────────────────────────────────────────────────────────┘ │
│ │ │ │ │ │ │
│ │ └──────────────────┘ │ │ │
│ │ │ │ │
└────────────┴───────────────────────────┴───────────────────────────────┴──────────────────────────────────────────┘
```
# Usage
`Pierce<T>` can be created with `Pierce::new(...)`. `T` should be a doubly-nested pointer (e.g. `Arc<Vec<_>>`, `Box<Box<_>>`).
[deref][Deref::deref]-ing a `Pierce<T>` returns `&<T::Target as Deref>::Target`,
i.e. the deref target of the deref target of `T` (the outer pointer that is wrapped by Pierce),
i.e. the deref target of the inner pointer.
You can also obtain a borrow of just `T` (the outer pointer) using `.borrow_inner()`.
See the docs at [`Pierce`] for more details.
## Deeper Nesting
A `Pierce` reduces two jumps to one.
If you have deeper nestings, you can wrap it multiple times.
```
# use pierce::Pierce;
let triply_nested: Box<Box<Box<i32>>> = Box::new(Box::new(Box::new(42)));
assert_eq!(***triply_nested, 42); // <- Three jumps!
let pierce_twice = Pierce::new(Pierce::new(triply_nested));
assert_eq!(*pierce_twice, 42); // <- Just one jump!
```
# Benchmarks
These benchmarks probably won't represent your use case at all because:
* They are engineered to make Pierce look good.
* Compiler optimizations are hard to control.
* CPU caches and predictions are hard to control. (I bet the figures will be very different on your CPU.)
* Countless other reasons why you shouldn't trust synthetic benchmarks.
*Do your own benchmarks on real-world uses*.
That said, here are my results:
**Benchmark 1**: Read items from a `Box<Vec<usize>>`, with simulated memory fragmentation.
**Benchmark 2**: Read items from a `SlowBox<Vec<usize>>`. `SlowBox` deliberately slow down `deref()` call greatly.
**Benchmark 3**: Read several `Box<Box<i64>>`.
Time taken by `Pierce<T>` version compared to `T` version.
| Run | Benchmark 1 | Benchmark 2 | Benchmark 3 |
|-----------|-------------------|-------------------|-------------------|
| 1 | -40.23% | -99.69% | -5.68% |
| 2 | -40.59% | -99.69% | -5.16% |
| 3 | -40.70% | -99.68% | +2.69% |
| 4 | -39.85% | -99.68% | -5.35% |
| 5 | -38.90% | -99.71% | -5.02% |
| 6 | -39.12% | -99.69% | -5.53% |
| 7 | -40.51% | -99.69% | -6.09% |
| 8 | -26.99% | -99.71% | -6.43% |
See the benchmarks' code [here](https://github.com/wishawa/pierce/tree/main/src/bin/benchmark/main.rs).
# Limitations
## Immutable Only
Pierce only work with immutable data.
**Mutability is not supported at all** because I'm pretty sure it would be impossible to implement soundly.
(If you have an idea please share.)
## Requires `StableDeref`
Pointer wrapped by Pierce must be [`StableDeref`].
If your pointer type meets the conditions required, you can `unsafe impl StableDeref for T {}` on it.
The trait is re-exported at `pierce::StableDeref`.
The vast majority of pointers are `StableDeref`,
including [Box], [Vec], [String], [Rc][std::rc::Rc], [Arc][std::sync::Arc].
*/
use ;
pub use StableDeref;
/** Cache doubly-nested pointers.
A `Pierce<T>` stores `T` along with a cached pointer to `<T::Target as Deref>::Target`.
*/
unsafe
unsafe
unsafe