Chromium Code Reviews| Index: third_party/WebKit/Source/platform/text/CompressibleString.cpp |
| diff --git a/third_party/WebKit/Source/platform/text/CompressibleString.cpp b/third_party/WebKit/Source/platform/text/CompressibleString.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..23866efcebfc8fda58b72fcb1e0546dd710ebc79 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/platform/text/CompressibleString.cpp |
| @@ -0,0 +1,140 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "platform/text/CompressibleString.h" |
| + |
| +#include "public/platform/Platform.h" |
| +#include "wtf/Assertions.h" |
| +#include "wtf/WTFThreadData.h" |
| +#include "wtf/text/WTFString.h" |
| + |
| +namespace blink { |
| + |
| +class CompressibleStringTable { |
| + WTF_MAKE_NONCOPYABLE(CompressibleStringTable); |
| +public: |
| + static CompressibleStringTable* create(WTFThreadData& data) |
| + { |
| + data.m_compressibleStringTable = new CompressibleStringTable; |
| + data.m_compressibleStringTableDestructor = CompressibleStringTable::destroy; |
| + return data.m_compressibleStringTable; |
| + } |
| + |
| + void add(CompressibleStringImpl* string) |
| + { |
| + ASSERT(!m_table.contains(string)); |
| + m_table.add(string); |
| + } |
| + |
| + bool contains(CompressibleStringImpl* string) const |
| + { |
| + return m_table.contains(string); |
| + } |
| + |
| + void remove(CompressibleStringImpl* string) |
| + { |
| + ASSERT(m_table.contains(string)); |
| + m_table.remove(string); |
| + } |
| + |
| + void compressAll() |
| + { |
| + HashSet<CompressibleStringImpl*>::iterator end = m_table.end(); |
| + for (HashSet<CompressibleStringImpl*>::iterator iter = m_table.begin(); iter != end; ++iter) { |
| + CompressibleStringImpl* string = *iter; |
| + if (!string->isCompressed()) |
| + string->compressString(); |
| + } |
| + } |
| + |
| +private: |
| + CompressibleStringTable() { } |
| + |
| + static void destroy(CompressibleStringTable* table) |
| + { |
| + delete table; |
| + } |
| + |
| + HashSet<CompressibleStringImpl*> m_table; |
| +}; |
| + |
| +void initializeCompressibleStringTable() |
| +{ |
| + WTFThreadData& data = wtfThreadData(); |
| + ASSERT(!data.compressibleStringTable()); |
| + CompressibleStringTable::create(data); |
| +} |
| + |
| +static inline CompressibleStringTable& compressibleStringTable() |
| +{ |
| + WTFThreadData& data = wtfThreadData(); |
| + ASSERT(data.compressibleStringTable()); |
| + return *data.compressibleStringTable(); |
| +} |
| + |
| +static const unsigned CompressibleStringImplSizeThrehold = 100000; |
| + |
| +bool CompressibleStringImpl::s_isPageBackground = false; |
| + |
| +void CompressibleStringImpl::compressAll() |
| +{ |
| + compressibleStringTable().compressAll(); |
| +} |
| + |
| +void CompressibleStringImpl::setPageBackground(bool isPageBackground) |
| +{ |
| + s_isPageBackground = isPageBackground; |
| +} |
| + |
| +CompressibleStringImpl::CompressibleStringImpl(PassRefPtr<StringImpl> impl) |
| + : m_string(impl) |
| + , m_isCompressed(false) |
| +{ |
| + ASSERT(impl); |
| + if (originalContentSizeInBytes() > CompressibleStringImplSizeThrehold) |
| + compressibleStringTable().add(this); |
| +} |
| + |
| +CompressibleStringImpl::~CompressibleStringImpl() |
| +{ |
| + if (originalContentSizeInBytes() > CompressibleStringImplSizeThrehold) { |
| + compressibleStringTable().remove(this); |
| + } |
| +} |
| + |
| +enum { |
| + StringWasCompressedInBackgroundTab, |
| + StringWasDecompressedInBackgroundTab, |
| + StringWasDecompressedInForegroundTab, |
| +}; |
| + |
| +static const char* histogramName() |
| +{ |
| + return "Memory.CompressibleStringCount"; |
| +} |
| + |
| +// compressString does nothing but collect UMA so far. |
| +// TODO(hajimehoshi): Implement this. |
| +void CompressibleStringImpl::compressString() |
| +{ |
| + ASSERT(s_isPageBackground); |
| + Platform::current()->histogramCustomCounts(histogramName(), StringWasCompressedInBackgroundTab, 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
|
| + ASSERT(!isCompressed()); |
| + m_isCompressed = true; |
| +} |
| + |
| +// decompressString does nothing but collect UMA so far. |
| +// TODO(hajimehoshi): Implement this. |
| +void CompressibleStringImpl::decompressString() |
| +{ |
| + if (s_isPageBackground) |
| + Platform::current()->histogramCustomCounts(histogramName(), StringWasDecompressedInBackgroundTab, 0, 10000, 50); |
| + else |
| + Platform::current()->histogramCustomCounts(histogramName(), StringWasDecompressedInForegroundTab, 0, 10000, 50); |
| + |
| + ASSERT(isCompressed()); |
| + m_isCompressed = false; |
| +} |
| + |
| +} // namespace blink |