Commit 3932d63
committed
perf(cache): optimize inline cache to use single struct array (8→4 entries)
**Problem with Previous Implementation:**
The 8-entry cache used TWO separate arrays per lookup:
```csharp
// Before: Double array access (poor cache locality)
if (actualTypeHandle == CachedTypeHandles[cacheSlot]) // Array 1
CachedSerializers[cacheSlot](val, ref writer); // Array 2
```
This caused:
- **2x memory indirections** per cache lookup
- **2x array bounds checks** (even if JIT-optimized away)
- **Poor cache locality** - type handle and delegate stored separately
- **More pressure on L1 cache** - 8 entries = larger working set
**Solution: Single Struct Array with Reduced Size**
Introduced `CacheEntry` structs that pack type handle/ID and delegate together:
```csharp
// CachedSerializer<T>:
private struct CacheEntry
{
public IntPtr TypeHandle;
public SerializeDelegate<T> Serializer;
}
internal static readonly CacheEntry[] Cache = new CacheEntry[4];
// Usage - single array access:
ref CacheEntry entry = ref Cache[cacheSlot];
if (actualTypeHandle == entry.TypeHandle)
entry.Serializer(val, ref writer);
```
**Key Changes:**
1. **CachedSerializer<T>**: Single `CacheEntry[4]` array replaces two separate arrays
2. **CachedDeserializer<T>**: Two structs for separate out/ref overloads:
- `CacheEntry[4]` for out parameter deserialization
- `CacheEntryRef[4]` for ref parameter deserialization
3. **Cache size reduced**: 8 entries → 4 entries per type
4. **Index calculation**: `& 7` → `& 3` (modulo 4 instead of modulo 8)
**Performance Benefits:**
✅ **Single array access** - One memory dereference instead of two
✅ **Better cache locality** - Type handle/ID and delegate loaded together
✅ **CPU cache line efficiency** - Both values in same cache line
✅ **Smaller working set** - 4 entries instead of 8 reduces memory footprint
✅ **Simpler code** - `ref CacheEntry` eliminates separate index tracking
**Trade-offs:**
1 parent 2f18f40 commit 3932d63
2 files changed
Lines changed: 66 additions & 32 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
192 | 192 | | |
193 | 193 | | |
194 | 194 | | |
195 | | - | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
196 | 210 | | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
201 | 225 | | |
202 | 226 | | |
203 | 227 | | |
| |||
396 | 420 | | |
397 | 421 | | |
398 | 422 | | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
403 | 428 | | |
404 | | - | |
| 429 | + | |
405 | 430 | | |
406 | 431 | | |
407 | 432 | | |
408 | 433 | | |
409 | 434 | | |
410 | 435 | | |
411 | | - | |
412 | | - | |
413 | | - | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
414 | 439 | | |
415 | 440 | | |
416 | 441 | | |
| |||
467 | 492 | | |
468 | 493 | | |
469 | 494 | | |
470 | | - | |
471 | | - | |
472 | | - | |
473 | | - | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
474 | 500 | | |
475 | | - | |
| 501 | + | |
476 | 502 | | |
477 | 503 | | |
478 | 504 | | |
479 | 505 | | |
480 | 506 | | |
481 | 507 | | |
482 | | - | |
483 | | - | |
484 | | - | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
485 | 511 | | |
486 | 512 | | |
487 | 513 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
191 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
192 | 200 | | |
193 | | - | |
194 | | - | |
| 201 | + | |
195 | 202 | | |
196 | 203 | | |
197 | 204 | | |
| |||
309 | 316 | | |
310 | 317 | | |
311 | 318 | | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
316 | 324 | | |
317 | | - | |
| 325 | + | |
318 | 326 | | |
319 | 327 | | |
320 | 328 | | |
321 | 329 | | |
322 | 330 | | |
323 | 331 | | |
324 | | - | |
325 | | - | |
326 | | - | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
327 | 335 | | |
328 | 336 | | |
329 | 337 | | |
| |||
0 commit comments