Skip to content

Conversation

@mgyoo86
Copy link
Collaborator

@mgyoo86 mgyoo86 commented Dec 6, 2025

Summary

Implement N-way set associative cache for unsafe_acquire! to prevent thrashing when alternating between different dimension patterns.

Changes

Core Implementation

  • N-way cache: Each slot can cache up to CACHE_WAYS dimension patterns (default: 4)
  • Round-robin replacement: Cyclic eviction on cache miss
  • Pointer validation: Automatic cache invalidation when backing vector resizes

Performance

Scenario Before (1-way) After (4-way)
Single dims repeated 0 bytes 0 bytes
2 dims alternating 112 bytes/call 0 bytes
4 dims alternating 112 bytes/call 0 bytes
5 dims alternating 112 bytes/call 112 bytes/call (cache eviction)

API Comparison

Function Returns N-way Cache Allocation
acquire! ReshapedArray Not needed Always 0 bytes
unsafe_acquire! Array Used 0 bytes when ≤ CACHE_WAYS patterns

Configuration (Preferences.jl)

Option 1: Programmatic

using AdaptiveArrayPools
set_cache_ways!(8)  # Requires Julia restart

Option 2: LocalPreferences.toml

[AdaptiveArrayPools]
cache_ways = 8

- Add CACHE_WAYS=4 constant for N-way set associative cache
- Remove nd_views field (ReshapedArray needs no caching)
- Add nd_next_way field for round-robin replacement counter
- Implement linear search + round-robin replacement in get_nd_array!
- Update empty! to handle new cache structure

N-way cache prevents thrashing when alternating between different
array dimensions in the same slot. Each slot can cache up to 4
different dimension patterns, returning cached Array instances
instead of calling unsafe_wrap (which allocates 112 bytes).
- Add test_nway_cache.jl with thrashing prevention tests
- Verify type checks (ReshapedArray vs Array returns)
- Test cache invalidation on vector resize
- Confirm CACHE_WAYS constant is accessible
- Include new test file in runtests.jl
- Replace constant CACHE_WAYS with Preferences-based configuration
- Add set_cache_ways!(n) function for runtime configuration
- Support range 1-16 with validation and helpful error messages
- Export CACHE_WAYS and set_cache_ways! from module
- Add comprehensive tests for validation logic

Users can now configure N-way cache size via:
1. AdaptiveArrayPools.set_cache_ways!(n) (requires restart)
2. LocalPreferences.toml: [AdaptiveArrayPools] cache_ways = n
- Add 4-way alternating zero-allocation test
- Add 5-way acquire! test (ReshapedArray always 0 alloc)
- Add 5-way unsafe_acquire! test (cache eviction causes alloc)
- Add multiple slots with N-way cache test
- Reorganize into 'N-way Cache' and 'N-way Zero-Allocation' testsets
@codecov
Copy link

codecov bot commented Dec 6, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.27%. Comparing base (0351a59) to head (52dcd30).
⚠️ Report is 11 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master       #5      +/-   ##
==========================================
+ Coverage   97.16%   97.27%   +0.10%     
==========================================
  Files           6        6              
  Lines         424      440      +16     
==========================================
+ Hits          412      428      +16     
  Misses         12       12              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements an N-way set associative cache for unsafe_acquire! to eliminate allocation thrashing when alternating between different array dimension patterns. The implementation adds a configurable cache (default 4 ways) that allows each pool slot to cache multiple dimension patterns using round-robin eviction.

Key Changes

  • N-way set associative cache with configurable ways (default: 4) prevents cache thrashing when alternating between dimension patterns
  • Preferences.jl integration for runtime configuration with validation (valid range: 1-16)
  • Comprehensive test suite demonstrating zero-allocation behavior for patterns within cache capacity

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/types.jl Adds CACHE_WAYS configuration with Preferences.jl, updates TypedPool structure with N-way cache fields (nd_arrays, nd_dims, nd_ptrs, nd_next_way), removes obsolete nd_views field
src/core.jl Implements N-way cache logic in get_nd_array! with linear search for cache hits and round-robin replacement for misses, updates empty! to clear new cache fields
src/AdaptiveArrayPools.jl Exports CACHE_WAYS constant and set_cache_ways! function for public API
test/test_nway_cache.jl Adds comprehensive tests for N-way cache functionality, configuration validation, allocation behavior, cache invalidation, and multi-slot scenarios
test/runtests.jl Includes new test file in test suite

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mgyoo86 mgyoo86 merged commit 1b0aad3 into master Dec 6, 2025
8 checks passed
@mgyoo86 mgyoo86 deleted the feat/Nway_cache branch December 6, 2025 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant