Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, there are multiple places where either a LRU cache is already used or could be improved to make use of one. The cases that are already making use of an LRU cache have nearly identical specialized implementations which results in duplicated code.
Thus, this implements a new module named
lru
which provides a concurrency safe least-recently-used cache with nearly early O(1) lookups, inserts, and deletions and updates both thepeer
andserver
to make use of it. The cache is limited to a maximum number of items with eviction for the oldest entry when the limit is exceeded.There is no additional performance hit to allocations by making use of this generic version because the internal list already causes the item to be boxed anyway, so the addition of the interface for the type does not cause an additional allocation.
This can be seen by comparing this generic implementation to the existing concrete nonce implementation in peer:
A series of individual commits have been used to make the review process easier. Each commit message more thoroughly describes its purpose, but primarily they consist of the following:
lru
module with the generic LRU cachepeer
to make use of the newlru
module for known inventorypeer
to make use of the newlru
module for sent noncesserver
to make use of the newlru
module for known addresses