Commit cb4e9764 authored by Adenilson Cavalcanti's avatar Adenilson Cavalcanti Committed by Commit Bot

Specialized character type constructors to improve ShapeCache hashing

This allows to avoid copying text runs character-per-character
for 16bits text.

Also there is no need to hash per character, just hash the
whole string in one go. This also helps to save the construction
of a StringHasher object (as we use the static method to hash
the strings).

Finally, reserve in HashTable space for 500 words (this reduces
the number of rehashes in up to 4x).

Bug: 735674
Change-Id: I4c5e23bb1e21dad995d4948fae0943a2b01309e0
Reviewed-on: https://chromium-review.googlesource.com/564076Reviewed-by: default avatarAdenilson Cavalcanti <cavalcantii@chromium.org>
Reviewed-by: default avatarEmil A Eklund <eae@chromium.org>
Commit-Queue: Adenilson Cavalcanti <cavalcantii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487543}
parent 0c6d79c9
...@@ -745,6 +745,7 @@ component("platform") { ...@@ -745,6 +745,7 @@ component("platform") {
"fonts/shaping/HarfBuzzShaper.h", "fonts/shaping/HarfBuzzShaper.h",
"fonts/shaping/RunSegmenter.cpp", "fonts/shaping/RunSegmenter.cpp",
"fonts/shaping/RunSegmenter.h", "fonts/shaping/RunSegmenter.h",
"fonts/shaping/ShapeCache.cpp",
"fonts/shaping/ShapeCache.h", "fonts/shaping/ShapeCache.h",
"fonts/shaping/ShapeResult.cpp", "fonts/shaping/ShapeResult.cpp",
"fonts/shaping/ShapeResult.h", "fonts/shaping/ShapeResult.h",
......
/*
* Copyright (c) 2012, 2017 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "platform/fonts/shaping/ShapeCache.h"
#include "platform/wtf/StringHasher.h"
namespace blink {
void ShapeCache::SmallStringKey::HashString() {
// TODO(cavalcantii): replace this for a better hash function,
// see crbug.com/735674.
hash_ = StringHasher::ComputeHash(characters_, length_);
}
} // namespace blink
...@@ -33,7 +33,6 @@ ...@@ -33,7 +33,6 @@
#include "platform/wtf/HashFunctions.h" #include "platform/wtf/HashFunctions.h"
#include "platform/wtf/HashSet.h" #include "platform/wtf/HashSet.h"
#include "platform/wtf/HashTableDeletedValueType.h" #include "platform/wtf/HashTableDeletedValueType.h"
#include "platform/wtf/StringHasher.h"
#include "platform/wtf/WeakPtr.h" #include "platform/wtf/WeakPtr.h"
namespace blink { namespace blink {
...@@ -47,13 +46,13 @@ struct ShapeCacheEntry { ...@@ -47,13 +46,13 @@ struct ShapeCacheEntry {
class ShapeCache { class ShapeCache {
USING_FAST_MALLOC(ShapeCache); USING_FAST_MALLOC(ShapeCache);
WTF_MAKE_NONCOPYABLE(ShapeCache); WTF_MAKE_NONCOPYABLE(ShapeCache);
private:
// Used to optimize small strings as hash table keys. Avoids malloc'ing an // Used to optimize small strings as hash table keys. Avoids malloc'ing an
// out-of-line StringImpl. // out-of-line StringImpl.
class SmallStringKey { class SmallStringKey {
DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
void HashString();
public: public:
static unsigned Capacity() { return kCapacity; } static unsigned Capacity() { return kCapacity; }
...@@ -65,32 +64,26 @@ class ShapeCache { ...@@ -65,32 +64,26 @@ class ShapeCache {
: length_(kDeletedValueLength), : length_(kDeletedValueLength),
direction_(static_cast<unsigned>(TextDirection::kLtr)) {} direction_(static_cast<unsigned>(TextDirection::kLtr)) {}
template <typename CharacterType> SmallStringKey(const LChar* characters,
SmallStringKey(CharacterType* characters,
unsigned short length, unsigned short length,
TextDirection direction) TextDirection direction)
: length_(length), direction_(static_cast<unsigned>(direction)) { : length_(length), direction_(static_cast<unsigned>(direction)) {
DCHECK(length <= kCapacity); DCHECK(length <= kCapacity);
// Up-convert from LChar to UChar.
StringHasher hasher; for (unsigned short i = 0; i < length; ++i) {
bool remainder = length & 1;
length >>= 1;
unsigned i = 0;
while (length--) {
characters_[i] = characters[i]; characters_[i] = characters[i];
characters_[i + 1] = characters[i + 1];
hasher.AddCharactersAssumingAligned(characters[i], characters[i + 1]);
i += 2;
} }
if (remainder) { HashString();
characters_[i] = characters[i]; }
hasher.AddCharacter(characters[i]);
}
hash_ = hasher.GetHash(); SmallStringKey(const UChar* characters,
unsigned short length,
TextDirection direction)
: length_(length), direction_(static_cast<unsigned>(direction)) {
DCHECK(length <= kCapacity);
memcpy(characters_, characters, length * sizeof(UChar));
HashString();
} }
const UChar* Characters() const { return characters_; } const UChar* Characters() const { return characters_; }
...@@ -116,30 +109,12 @@ class ShapeCache { ...@@ -116,30 +109,12 @@ class ShapeCache {
UChar characters_[kCapacity]; UChar characters_[kCapacity];
}; };
struct SmallStringKeyHash {
STATIC_ONLY(SmallStringKeyHash);
static unsigned GetHash(const SmallStringKey& key) { return key.GetHash(); }
static bool Equal(const SmallStringKey& a, const SmallStringKey& b) {
return a == b;
}
// Empty and deleted values have lengths that are not equal to any valid
// length.
static const bool safe_to_compare_to_empty_or_deleted = true;
};
struct SmallStringKeyHashTraits : WTF::SimpleClassHashTraits<SmallStringKey> {
STATIC_ONLY(SmallStringKeyHashTraits);
static const bool kHasIsEmptyValueFunction = true;
static bool IsEmptyValue(const SmallStringKey& key) {
return key.IsHashTableEmptyValue();
}
static const unsigned kMinimumTableSize = 16;
};
friend bool operator==(const SmallStringKey&, const SmallStringKey&);
public: public:
ShapeCache() : weak_factory_(this), version_(0) {} ShapeCache() : weak_factory_(this), version_(0) {
// We use 5% of the maximum word cache size as start value
// for the HashTable.
short_string_map_.ReserveCapacityForSize(500);
}
ShapeCacheEntry* Add(const TextRun& run, ShapeCacheEntry entry) { ShapeCacheEntry* Add(const TextRun& run, ShapeCacheEntry entry) {
if (run.length() > SmallStringKey::Capacity()) if (run.length() > SmallStringKey::Capacity())
...@@ -184,8 +159,8 @@ class ShapeCache { ...@@ -184,8 +159,8 @@ class ShapeCache {
ShapeCacheEntry* value; ShapeCacheEntry* value;
if (length == 1) { if (length == 1) {
uint32_t key = run[0]; uint32_t key = run[0];
// All current codepointsin UTF-32 are bewteen 0x0 and 0x10FFFF, // All current codepoints in UTF-32 are bewteen 0x0 and 0x10FFFF,
// as such use bit 32 to indicate direction. // as such use bit 31 (zero-based) to indicate direction.
if (run.Direction() == TextDirection::kRtl) if (run.Direction() == TextDirection::kRtl)
key |= (1u << 31); key |= (1u << 31);
SingleCharMap::AddResult add_result = single_char_map_.insert(key, entry); SingleCharMap::AddResult add_result = single_char_map_.insert(key, entry);
...@@ -193,12 +168,13 @@ class ShapeCache { ...@@ -193,12 +168,13 @@ class ShapeCache {
value = &add_result.stored_value->value; value = &add_result.stored_value->value;
} else { } else {
SmallStringKey small_string_key; SmallStringKey small_string_key;
if (run.Is8Bit()) if (run.Is8Bit()) {
small_string_key = small_string_key =
SmallStringKey(run.Characters8(), length, run.Direction()); SmallStringKey(run.Characters8(), length, run.Direction());
else } else {
small_string_key = small_string_key =
SmallStringKey(run.Characters16(), length, run.Direction()); SmallStringKey(run.Characters16(), length, run.Direction());
}
SmallStringMap::AddResult add_result = SmallStringMap::AddResult add_result =
short_string_map_.insert(small_string_key, entry); short_string_map_.insert(small_string_key, entry);
...@@ -206,19 +182,39 @@ class ShapeCache { ...@@ -206,19 +182,39 @@ class ShapeCache {
value = &add_result.stored_value->value; value = &add_result.stored_value->value;
} }
if (!is_new_entry) if ((!is_new_entry) || (size() < kMaxSize)) {
return value;
if (size() < kMaxSize)
return value; return value;
}
// No need to be fancy: we're just trying to avoid pathological growth. // No need to be fancy: we're just trying to avoid pathological growth.
single_char_map_.clear(); single_char_map_.clear();
short_string_map_.clear(); short_string_map_.clear();
return 0; return nullptr;
} }
struct SmallStringKeyHash {
STATIC_ONLY(SmallStringKeyHash);
static unsigned GetHash(const SmallStringKey& key) { return key.GetHash(); }
static bool Equal(const SmallStringKey& a, const SmallStringKey& b) {
return a == b;
}
// Empty and deleted values have lengths that are not equal to any valid
// length.
static const bool safe_to_compare_to_empty_or_deleted = true;
};
struct SmallStringKeyHashTraits : WTF::SimpleClassHashTraits<SmallStringKey> {
STATIC_ONLY(SmallStringKeyHashTraits);
static const bool kHasIsEmptyValueFunction = true;
static bool IsEmptyValue(const SmallStringKey& key) {
return key.IsHashTableEmptyValue();
}
static const unsigned kMinimumTableSize = 16;
};
friend bool operator==(const SmallStringKey&, const SmallStringKey&);
typedef HashMap<SmallStringKey, typedef HashMap<SmallStringKey,
ShapeCacheEntry, ShapeCacheEntry,
SmallStringKeyHash, SmallStringKeyHash,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment