2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkChecksum.h"
13 #include "SkTemplates.h"
15 #include "gUniqueGlyphIDs.h"
16 #define gUniqueGlyphIDs_Sentinel 0xFFFF
18 static int count_glyphs(const uint16_t start[]) {
19 const uint16_t* curr = start;
20 while (*curr != gUniqueGlyphIDs_Sentinel) {
23 return static_cast<int>(curr - start);
26 class FontCacheBench : public Benchmark {
31 const char* onGetName() override {
35 void onDraw(const int loops, SkCanvas* canvas) override {
37 this->setupPaint(&paint);
38 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
40 const uint16_t* array = gUniqueGlyphIDs;
41 while (*array != gUniqueGlyphIDs_Sentinel) {
42 int count = count_glyphs(array);
43 for (int i = 0; i < loops; ++i) {
44 paint.measureText(array, count * sizeof(uint16_t));
46 array += count + 1; // skip the sentinel
51 typedef Benchmark INHERITED;
54 ///////////////////////////////////////////////////////////////////////////////
56 static uint32_t rotr(uint32_t value, unsigned bits) {
57 return (value >> bits) | (value << (32 - bits));
60 typedef uint32_t (*HasherProc)(uint32_t);
62 static uint32_t hasher0(uint32_t value) {
63 value = value ^ (value >> 16);
64 return value ^ (value >> 8);
71 { "hasher0", hasher0 },
72 { "hasher2", SkChecksum::Mix },
75 #define kMaxHashBits 12
76 #define kMaxHashCount (1 << kMaxHashBits)
78 static int count_collisions(const uint16_t array[], int count, HasherProc proc,
80 char table[kMaxHashCount];
81 sk_bzero(table, sizeof(table));
84 for (int i = 0; i < count; ++i) {
85 int index = proc(array[i]) & hashMask;
86 collisions += table[index];
92 static void dump_array(const uint16_t array[], int count) {
93 for (int i = 0; i < count; ++i) {
94 SkDebugf(" %d,", array[i]);
99 class FontCacheEfficiency : public Benchmark {
101 FontCacheEfficiency() {
102 if (false) dump_array(NULL, 0);
103 if (false) rotr(0, 0);
107 const char* onGetName() override {
108 return "fontefficiency";
111 void onDraw(const int loops, SkCanvas* canvas) override {
118 for (int hashBits = 6; hashBits <= 12; hashBits += 1) {
119 int hashMask = ((1 << hashBits) - 1);
120 for (int limit = 32; limit <= 1024; limit <<= 1) {
121 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
124 const uint16_t* array = gUniqueGlyphIDs;
125 while (*array != gUniqueGlyphIDs_Sentinel) {
126 int count = SkMin32(count_glyphs(array), limit);
127 collisions += count_collisions(array, count, gRec[i].fHasher, hashMask);
129 array += count + 1; // skip the sentinel
131 SkDebugf("hashBits [%d] limit [%d] collisions [%d / %d = %1.2g%%] using %s\n", hashBits, limit, collisions, glyphs,
132 collisions * 100.0 / glyphs, gRec[i].fName);
139 typedef Benchmark INHERITED;
142 ///////////////////////////////////////////////////////////////////////////////
144 DEF_BENCH( return new FontCacheBench(); )
146 // undefine this to run the efficiency test
147 //DEF_BENCH( return new FontCacheEfficiency(); )