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 static inline CompressibleStringTable& compressibleStringTable() |
| 63 { |
| 64 WTFThreadData& data = wtfThreadData(); |
| 65 CompressibleStringTable* table = data.compressibleStringTable(); |
| 66 if (UNLIKELY(!table)) |
| 67 table = CompressibleStringTable::create(data); |
| 68 return *table; |
| 69 } |
| 70 |
| 71 static const unsigned CompressibleStringImplSizeThrehold = 100000; |
| 72 |
| 73 bool CompressibleStringImpl::s_isPageBackground = false; |
| 74 |
| 75 void CompressibleStringImpl::compressAll() |
| 76 { |
| 77 compressibleStringTable().compressAll(); |
| 78 } |
| 79 |
| 80 void CompressibleStringImpl::setPageBackground(bool isPageBackground) |
| 81 { |
| 82 s_isPageBackground = isPageBackground; |
| 83 } |
| 84 |
| 85 CompressibleStringImpl::CompressibleStringImpl(PassRefPtr<StringImpl> impl) |
| 86 : m_string(impl) |
| 87 , m_isCompressed(false) |
| 88 { |
| 89 if (originalContentSizeInBytes() > CompressibleStringImplSizeThrehold) |
| 90 compressibleStringTable().add(this); |
| 91 } |
| 92 |
| 93 CompressibleStringImpl::~CompressibleStringImpl() |
| 94 { |
| 95 if (originalContentSizeInBytes() > CompressibleStringImplSizeThrehold) |
| 96 compressibleStringTable().remove(this); |
| 97 } |
| 98 |
| 99 enum CompressibleStringCountType { |
| 100 StringWasCompressedInBackgroundTab, |
| 101 StringWasDecompressedInBackgroundTab, |
| 102 StringWasDecompressedInForegroundTab, |
| 103 CompressibleStringCountTypeMax = StringWasDecompressedInForegroundTab, |
| 104 }; |
| 105 |
| 106 static void recordCompressibleStringCount(CompressibleStringCountType type) |
| 107 { |
| 108 Platform::current()->histogramEnumeration("Memory.CompressibleStringCount",
type, CompressibleStringCountTypeMax + 1); |
| 109 } |
| 110 |
| 111 // compressString does nothing but collect UMA so far. |
| 112 // TODO(hajimehoshi): Implement this. |
| 113 void CompressibleStringImpl::compressString() |
| 114 { |
| 115 ASSERT(s_isPageBackground); |
| 116 recordCompressibleStringCount(StringWasCompressedInBackgroundTab); |
| 117 ASSERT(!isCompressed()); |
| 118 m_isCompressed = true; |
| 119 } |
| 120 |
| 121 // decompressString does nothing but collect UMA so far. |
| 122 // TODO(hajimehoshi): Implement this. |
| 123 void CompressibleStringImpl::decompressString() |
| 124 { |
| 125 if (s_isPageBackground) |
| 126 recordCompressibleStringCount(StringWasDecompressedInBackgroundTab); |
| 127 else |
| 128 recordCompressibleStringCount(StringWasDecompressedInForegroundTab); |
| 129 |
| 130 ASSERT(isCompressed()); |
| 131 m_isCompressed = false; |
| 132 } |
| 133 |
| 134 } // namespace blink |
OLD | NEW |