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

LRU library #7699

Closed
bblum opened this issue Jul 10, 2013 · 4 comments
Closed

LRU library #7699

bblum opened this issue Jul 10, 2013 · 4 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@bblum
Copy link
Contributor

bblum commented Jul 10, 2013

The standard way of representing an LRU is to have nodes be stored in a hashmap or binary search tree, with lookup-by-key, and in a linked-list which represents recency. This allows O(1) or O(logn) lookup/insert time, and constant time update/eviction with the recency list.

The representation is usually something like:

struct LRU<K,V> {
    struct node<K,V> *tree_root;
    struct node<K,V> *recency_head, *recency_tail;
}
struct node<K,V> {
    K key;
    V value;
    struct node<K,V> *tree_parent, *tree_left, *tree_right;
    struct node<K,V> *recency_prev, *recency_next;
}

This is more difficult to represent in Rust. You could do it with @mut pointers, but we all know what would go wrong there, and also that would prevent you from sharing it between tasks. You could also do it with unsafe pointers. I suggest instead representing it as a vector, and replacing the pointers with indices into the vector.

@jdm
Copy link
Contributor

jdm commented Jul 10, 2013

@bblum
Copy link
Contributor Author

bblum commented Jul 11, 2013

It has some O(n) operations, though.

@thestinger
Copy link
Contributor

This is also the same data structure as OrderedDict in python and LinkedHashMap, they provide both the LRU cache use case and also a hash map recording the insertion order.

@thestinger
Copy link
Contributor

Closing as a duplicate of #4988.

flip1995 pushed a commit to flip1995/rust that referenced this issue Nov 4, 2021
Ignore references to type aliases in ptr_arg

Works using the fact that the hir path will point to a TyAlias, rather than being resolved to the underlying type

Fixes rust-lang#7699

changelog: [`ptr_arg`] No longer lints references to type aliases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

3 participants