hash tables' application in cache
I read the wiki-page about hash-table. then I saw something like this:
开发者_如何学GoHash tables can be used to implement caches, auxiliary data tables that are used to speed up the access to data that is primarily stored in slower media.
I wonder how does hash speed the cache? Can anybody tell me about it ?
Each access to the cache will require the hash code of the lookup key to be computed, and each addition to the cache will likewise need to know the hash code. A smart cache which combines the two operations ("lookup by key; if it doesn't exist then fetch the right value and cache it") could potentially avoid having to compute the hash twice.
Usually hashes are relatively cheap to compute - and much cheaper than accessing the underlying resource which is being cached... and it's not like all the keys in the cache have to be hashed on every lookup.
In some cases hashes themselves can be cached - in Java, for example, the String
class caches the hashcode when it's first computed. Whether or not this is actually beneficial depends on your situation, of course.
精彩评论