Skip to content

Implement LRU cache for storing hashes to filter out for flood repeating#29

Open
weebl2000 wants to merge 3 commits intodevfrom
LRU_cache_filterflood
Open

Implement LRU cache for storing hashes to filter out for flood repeating#29
weebl2000 wants to merge 3 commits intodevfrom
LRU_cache_filterflood

Conversation

@weebl2000
Copy link
Owner

Replace the FIFO (cyclic index) hash table with an LRU cache using uint32_t millis() timestamps. The oldest entry is evicted instead of the next sequential slot, so frequently-seen hashes stay in the table longer.

Changes:

  • _next_idx replaced by _last_seen[MAX_PACKET_HASHES] (uint32_t timestamps)
  • On duplicate hit: refresh timestamp (LRU touch)
  • On insert: evict the entry with the oldest timestamp
  • restoreFrom(): restored hashes get fresh timestamps; empty slots stay at 0
  • saveTo(): writes a dummy _next_idx for backward-compatible file format
  • No timeout-based eviction — purely LRU

Why this helps:

Some busy nodes see more than 128 packets before a flooded packet returns. With FIFO, a hash gets evicted after 128 new packets regardless of how recently it was seen. With LRU, actively-seen hashes stay in the table while stale ones get evicted first.

For example, the Utrecht repeater sees ~658 packets/20 min with 72 neighbors. With FIFO:

  • Time to cycle 128 slots: 128 / 0.548 pkt/s ≈ 234 seconds
  • Any packet returning after ~4 minutes won't be filtered → loop

With LRU, recently-seen hashes survive even under high packet volume, so only truly stale entries get evicted.

Memory cost: +384 bytes (uint32_t[128] replaces int _next_idx)


Build firmware: Build from this branch


Mirror of meshcore-dev#1380

@weebl2000 weebl2000 force-pushed the LRU_cache_filterflood branch from a2ee202 to 74cd840 Compare February 28, 2026 18:07
liamcottle and others added 2 commits March 4, 2026 01:39
Replace the fixed-size hash table with an LRU cache using actual
timestamps instead of timeout-based eviction. Some busy nodes see
more than 128 packets before duplicates arrive, so LRU ordering
provides better eviction behavior.
@weebl2000 weebl2000 force-pushed the LRU_cache_filterflood branch from 74cd840 to 845c892 Compare March 3, 2026 14: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.

2 participants