From 1269dc7cfb7d9799a59ae75094c02fdad3ae1eb8 Mon Sep 17 00:00:00 2001 From: hui su Date: Fri, 12 Aug 2016 15:16:06 -0700 Subject: [PATCH] Refactor VP8LColorCacheContains() Return key/index if the query is found, and -1 otherwise. The benefit of this is to save a hashing computation. Change-Id: Iff056be330f5fb8204011259ac814f7677dd40fe --- src/enc/backward_references.c | 16 ++++++++++------ src/utils/color_cache.h | 3 ++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/enc/backward_references.c b/src/enc/backward_references.c index fd82563a..03246389 100644 --- a/src/enc/backward_references.c +++ b/src/enc/backward_references.c @@ -598,9 +598,10 @@ static void AddSingleLiteralWithCostModel(const uint32_t* const argb, uint16_t* const dist_array) { double cost_val = prev_cost; const uint32_t color = argb[0]; - if (use_color_cache && VP8LColorCacheContains(hashers, color)) { + const int ix = use_color_cache ? VP8LColorCacheContains(hashers, color) : -1; + if (ix >= 0) { + // use_color_cache is true and hashers contains color const double mul0 = 0.68; - const int ix = VP8LColorCacheGetIndex(hashers, color); cost_val += GetCacheCost(cost_model, ix) * mul0; } else { const double mul1 = 0.82; @@ -1410,9 +1411,11 @@ static int BackwardReferencesHashChainFollowChosenPath( i += len; } else { PixOrCopy v; - if (use_color_cache && VP8LColorCacheContains(&hashers, argb[i])) { + const int idx = + use_color_cache ? VP8LColorCacheContains(&hashers, argb[i]) : -1; + if (idx >= 0) { + // use_color_cache is true and hashers contains argb[i] // push pixel as a color cache index - const int idx = VP8LColorCacheGetIndex(&hashers, argb[i]); v = PixOrCopyCreateCacheIdx(idx); } else { if (use_color_cache) VP8LColorCacheInsert(&hashers, argb[i]); @@ -1601,8 +1604,9 @@ static int BackwardRefsWithLocalCache(const uint32_t* const argb, PixOrCopy* const v = c.cur_pos; if (PixOrCopyIsLiteral(v)) { const uint32_t argb_literal = v->argb_or_distance; - if (VP8LColorCacheContains(&hashers, argb_literal)) { - const int ix = VP8LColorCacheGetIndex(&hashers, argb_literal); + const int ix = VP8LColorCacheContains(&hashers, argb_literal); + if (ix >= 0) { + // hashers contains argb_literal *v = PixOrCopyCreateCacheIdx(ix); } else { VP8LColorCacheInsert(&hashers, argb_literal); diff --git a/src/utils/color_cache.h b/src/utils/color_cache.h index a9a9f642..d97fc708 100644 --- a/src/utils/color_cache.h +++ b/src/utils/color_cache.h @@ -53,10 +53,11 @@ static WEBP_INLINE int VP8LColorCacheGetIndex(const VP8LColorCache* const cc, return (kHashMul * argb) >> cc->hash_shift_; } +// Return the key if cc contains argb, and -1 otherwise. static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc, uint32_t argb) { const uint32_t key = (kHashMul * argb) >> cc->hash_shift_; - return (cc->colors_[key] == argb); + return (cc->colors_[key] == argb) ? (int)key : -1; } //------------------------------------------------------------------------------