bluele / gcache

An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC
MIT License
2.6k stars 271 forks source link

Improve the efficiency of Keys(), GetALL(), and Len(). #54

Closed sean- closed 5 years ago

sean- commented 6 years ago

Preallocate the maximum number of available items, including expired items. While this isn't optimal, it is a reasonable guess for the length.

Even though this approach results in one extra mutex acquisition, in simple go test benchmarking, this improves the test time from ~1.444s to ~1.332s. Presumably this improvement is the result of two things:

  1. reduced GC pressure on the GC
  2. reduced time that c.mu is locked while the GC copies data around

There is still room to be had by reducing the number of times c.mu is exclusively locked, but this is an easy performance improvement.

Fixes: #43 Replaces: #50

SchumacherFM commented 5 years ago

@bluele Sorry for asking but is there a special reason for not merging this improvement?

LorenzoS92 commented 5 years ago

Could you please merge it? The efficiency of Len() is very needed. It's very aggressive to preallocate all items when getting length of keys.

napolux commented 5 years ago

Please merge this!

Please

bluele commented 5 years ago

Sorry for late response.

60 resolves this issue.

If you pass false to checkExpired, you can avoid checking if each items are expired.

bluele commented 5 years ago

@sean- Thank you a lot.