Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi: Introduce lru module. #1683

Merged
merged 6 commits into from
Mar 18, 2019
Merged

Conversation

davecgh
Copy link
Member

@davecgh davecgh commented Mar 18, 2019

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 the peer and server 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:

BenchmarkCache           1 allocs/op
BenchmarkLruNonceList    1 allocs/op

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:

  • Implement a new lru module with the generic LRU cache
  • In addition to the implementation, it includes:
    • Comprehensive tests
    • Benchmark
    • Full package documentation
    • A basic usage example
  • Update the project docs for the new module
  • Update peer to make use of the new lru module for known inventory
  • Update peer to make use of the new lru module for sent nonces
  • Update server to make use of the new lru module for known addresses

@davecgh davecgh added this to the 1.5.0 milestone Mar 18, 2019
This adds the go 1.11 directive to all of the modules in order to
clearly mark they build and work with that version.  Go 1.12 modified
the tools such that tidy will automatically add the new version to
modules that do not already have a directive and that would prevent
builds on Go 1.11 through Go 1.11.3 which is not desirable.
davecgh added 5 commits March 18, 2019 10:08
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.  The cache is limited to a maximum number of items with
eviction for the oldest entry when the limit is exceeded.

This will allow the various concrete implementations to be updated to
make use of this shared module instead of duplicating code.  There is no
additional performance hit to 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
alloc.

This can be seen by comparing this generic implementation to the
existing concrete nonce implementation in peer:

BenchmarkCache   	 1 allocs/op
BenchmarkLruNonceList    1 allocs/op

It also includes comprehensive tests, a benchmark, full package
documentation, and a basic usage example.
This updates the docs/README.md file, module hierarchy graphviz, and
module hierarchy diagram to reflect the new lru module.
This updates the concrete peer known inventory LRU cache to make use of
the new generic LRU cache provided by the lru module.

It also removes the nor longer necessary specialized implementation.
This updates the concrete peer sent nonces LRU cache to make use of the
new generic LRU cache provided by the lru module.

It also removes the nor longer necessary specialized implementation.
This updates the server known address cache to make use of the new
generic LRU cache provided by the lru module.
@davecgh davecgh merged commit 0173adf into decred:master Mar 18, 2019
@davecgh davecgh deleted the multi_lru_module branch March 18, 2019 15:29
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.

3 participants