Skip to content

Commit

Permalink
perf(redis): Don't re-encode string when computing hash (#3838)
Browse files Browse the repository at this point in the history
When caching values in redis, we compare the hash of the value
we're about to write against the value already in the cache to
avoid writing unchanged values again.

The hash function we're using is expensive as it encodes the string
to bytes before hashing it. Per the documentation of the
putString function, it is only useful for cross-language compatibility
and we should instead use putUnencodedChars for much better performance
when this is not needed.
  • Loading branch information
ezimanyi committed Jul 2, 2019
1 parent f8b434d commit 7a60959
Showing 1 changed file with 1 addition and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.netflix.spinnaker.cats.redis.cache;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -442,7 +440,7 @@ private boolean hashCheck(
boolean hasTtl) {
if (options.isHashingEnabled() && !hasTtl) {
final String hash =
Hashing.sha1().newHasher().putString(serializedValue, UTF_8).hash().toString();
Hashing.sha1().newHasher().putUnencodedChars(serializedValue).hash().toString();
final String existingHash = hashes.get(id);
if (hash.equals(existingHash)) {
return true;
Expand Down

0 comments on commit 7a60959

Please sign in to comment.