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

Speed up LinkedHashMap remove() function to O(1) #178

Open
chaitanya0411 opened this issue Jan 7, 2022 · 2 comments · May be fixed by #264
Open

Speed up LinkedHashMap remove() function to O(1) #178

chaitanya0411 opened this issue Jan 7, 2022 · 2 comments · May be fixed by #264

Comments

@chaitanya0411
Copy link

LinkedHashMap is currently implemented using a combination of map and Doubly Linked List but takes O(n) time to remove an element. The implementation should use a reference to the node in the doubly linked list where the element is stored and should be removed in O(1). Also, the remove method first tries to find the index in O (n) and then remove the element in O(n). It can be easily optimized to do it in O(n).

@ErenDursun
Copy link

Pull request #211 closes this issue. The benchmarks look way better after the changes:

benchmarks before changes

...
BenchmarkTreeMapRemove100000-16    	       1	32919472200 ns/op	80403479216 B/op	  100201 allocs/op
...

benchmarks after changes

...
BenchmarkTreeMapRemove100000-16    	    1670	    653730 ns/op	       0 B/op	       0 allocs/op
...

@ErenDursun
Copy link

Reimplemented #211 with generics in #264.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants