Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "platform/text/CompressibleString.h" | |
| 6 | |
| 7 #include "public/platform/Platform.h" | |
| 8 #include "wtf/Assertions.h" | |
| 9 #include "wtf/WTFThreadData.h" | |
| 10 #include "wtf/text/WTFString.h" | |
| 11 | |
| 12 namespace blink { | |
| 13 | |
| 14 class CompressibleStringTable { | |
| 15 WTF_MAKE_NONCOPYABLE(CompressibleStringTable); | |
| 16 public: | |
| 17 static CompressibleStringTable* create(WTFThreadData& data) | |
| 18 { | |
| 19 data.m_compressibleStringTable = new CompressibleStringTable; | |
| 20 data.m_compressibleStringTableDestructor = CompressibleStringTable::dest roy; | |
| 21 return data.m_compressibleStringTable; | |
| 22 } | |
| 23 | |
| 24 void add(CompressibleStringImpl* string) | |
| 25 { | |
| 26 ASSERT(!m_table.contains(string)); | |
| 27 m_table.add(string); | |
| 28 } | |
| 29 | |
| 30 bool contains(CompressibleStringImpl* string) const | |
| 31 { | |
| 32 return m_table.contains(string); | |
| 33 } | |
| 34 | |
| 35 void remove(CompressibleStringImpl* string) | |
| 36 { | |
| 37 ASSERT(m_table.contains(string)); | |
| 38 m_table.remove(string); | |
| 39 } | |
| 40 | |
| 41 void compressAll() | |
| 42 { | |
| 43 HashSet<CompressibleStringImpl*>::iterator end = m_table.end(); | |
| 44 for (HashSet<CompressibleStringImpl*>::iterator iter = m_table.begin(); iter != end; ++iter) { | |
| 45 CompressibleStringImpl* string = *iter; | |
| 46 if (!string->isCompressed()) | |
| 47 string->compressString(); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 private: | |
| 52 CompressibleStringTable() { } | |
| 53 | |
| 54 static void destroy(CompressibleStringTable* table) | |
| 55 { | |
| 56 delete table; | |
| 57 } | |
| 58 | |
| 59 HashSet<CompressibleStringImpl*> m_table; | |
| 60 }; | |
| 61 | |
| 62 void initializeCompressibleStringTable() | |
| 63 { | |
| 64 WTFThreadData& data = wtfThreadData(); | |
| 65 ASSERT(!data.compressibleStringTable()); | |
| 66 CompressibleStringTable::create(data); | |
| 67 } | |
| 68 | |
| 69 static inline CompressibleStringTable& compressibleStringTable() | |
| 70 { | |
| 71 WTFThreadData& data = wtfThreadData(); | |
| 72 ASSERT(data.compressibleStringTable()); | |
| 73 return *data.compressibleStringTable(); | |
| 74 } | |
| 75 | |
| 76 static const unsigned CompressibleStringImplSizeThrehold = 100000; | |
| 77 | |
| 78 bool CompressibleStringImpl::s_isPageBackground = false; | |
| 79 | |
| 80 void CompressibleStringImpl::compressAll() | |
| 81 { | |
| 82 compressibleStringTable().compressAll(); | |
| 83 } | |
| 84 | |
| 85 void CompressibleStringImpl::setPageBackground(bool isPageBackground) | |
| 86 { | |
| 87 s_isPageBackground = isPageBackground; | |
| 88 } | |
| 89 | |
| 90 CompressibleStringImpl::CompressibleStringImpl(PassRefPtr<StringImpl> impl) | |
| 91 : m_string(impl) | |
| 92 , m_isCompressed(false) | |
| 93 { | |
| 94 ASSERT(impl); | |
| 95 if (originalContentSizeInBytes() > CompressibleStringImplSizeThrehold) | |
| 96 compressibleStringTable().add(this); | |
| 97 } | |
| 98 | |
| 99 CompressibleStringImpl::~CompressibleStringImpl() | |
| 100 { | |
| 101 if (originalContentSizeInBytes() > CompressibleStringImplSizeThrehold) { | |
| 102 compressibleStringTable().remove(this); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 enum { | |
| 107 StringWasCompressedInBackgroundTab, | |
| 108 StringWasDecompressedInBackgroundTab, | |
| 109 StringWasDecompressedInForegroundTab, | |
| 110 }; | |
| 111 | |
| 112 static const char* histogramName() | |
| 113 { | |
| 114 return "Memory.CompressibleStringCount"; | |
| 115 } | |
| 116 | |
| 117 // compressString does nothing but collect UMA so far. | |
| 118 // TODO(hajimehoshi): Implement this. | |
| 119 void CompressibleStringImpl::compressString() | |
| 120 { | |
| 121 ASSERT(s_isPageBackground); | |
| 122 Platform::current()->histogramCustomCounts(histogramName(), StringWasCompres sedInBackgroundTab, 0, 10000, 50); | |
|
Ilya Sherman
2016/01/20 01:42:38
nit: I'd recommend have a wrapper method for emitt
Ilya Sherman
2016/01/20 01:42:38
Hmm, why did you choose this histogram representat
hajimehoshi
2016/01/20 10:17:17
This is because I misunderstood the usage of the A
| |
| 123 ASSERT(!isCompressed()); | |
| 124 m_isCompressed = true; | |
| 125 } | |
| 126 | |
| 127 // decompressString does nothing but collect UMA so far. | |
| 128 // TODO(hajimehoshi): Implement this. | |
| 129 void CompressibleStringImpl::decompressString() | |
| 130 { | |
| 131 if (s_isPageBackground) | |
| 132 Platform::current()->histogramCustomCounts(histogramName(), StringWasDec ompressedInBackgroundTab, 0, 10000, 50); | |
| 133 else | |
| 134 Platform::current()->histogramCustomCounts(histogramName(), StringWasDec ompressedInForegroundTab, 0, 10000, 50); | |
| 135 | |
| 136 ASSERT(isCompressed()); | |
| 137 m_isCompressed = false; | |
| 138 } | |
| 139 | |
| 140 } // namespace blink | |
| OLD | NEW |