Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Unified Diff: third_party/WebKit/Source/wtf/text/CompressibleString.cpp

Issue 1389383003: WIP: Introduce CompressibleString Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase onto crrev.com/1564773002 Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/wtf/text/CompressibleString.cpp
diff --git a/third_party/WebKit/Source/wtf/text/CompressibleString.cpp b/third_party/WebKit/Source/wtf/text/CompressibleString.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..b1c4c7c752ca0f6303462ebf8cd6b232b9eb09c0
--- /dev/null
+++ b/third_party/WebKit/Source/wtf/text/CompressibleString.cpp
@@ -0,0 +1,210 @@
+// 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 "wtf/text/CompressibleString.h"
+
+#include "third_party/zlib/google/compression_utils.h"
+#include "wtf/Assertions.h"
+#include "wtf/LinkedStack.h"
+#include "wtf/Partitions.h"
+#include "wtf/PassOwnPtr.h"
+#include "wtf/WTFThreadData.h"
+#include "wtf/text/WTFString.h"
+
+namespace WTF {
+
+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)
+ {
+ m_table.add(string);
+ }
+
+ bool contains(CompressibleStringImpl* string) const
+ {
+ return m_table.contains(string);
+ }
+
+ void remove(CompressibleStringImpl* 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;
+};
+
+static inline CompressibleStringTable& compressibleStringTable()
+{
+ // Once possible we should make this non-lazy (constructed in WTFThreadData's constructor).
+ WTFThreadData& data = wtfThreadData();
+ CompressibleStringTable* table = data.compressibleStringTable();
+ if (UNLIKELY(!table))
+ table = CompressibleStringTable::create(data);
+ return *table;
+}
+
+void CompressibleStringImpl::purgeMemory()
+{
+ // TODO(hajimehoshi): Compressing for the current thread is not enough.
+ // Do the same thing on other threads.
+ compressibleStringTable().compressAll();
+}
+
+CompressibleStringImpl::CompressibleStringImpl(PassRefPtr<StringImpl> impl)
+ : m_impl(impl)
+ , m_originalLength(m_impl->length())
+ , m_compressedData(nullptr)
+ , m_compressedDataSize(0)
+ , m_is8Bit(m_impl->is8Bit())
+{
+ ASSERT(m_impl);
+ if (originalContentSizeInBytes() > 100000)
+ compressibleStringTable().add(this);
+}
+
+CompressibleStringImpl::~CompressibleStringImpl()
+{
+ if (compressibleStringTable().contains(this))
+ compressibleStringTable().remove(this);
+ if (m_compressedData)
+ Partitions::fastFree(m_compressedData);
+}
+
+unsigned CompressibleStringImpl::originalContentSizeInBytes() const
+{
+ if (is8Bit())
+ return m_originalLength * sizeof(LChar);
+ return m_originalLength * sizeof(UChar);
+}
+
+unsigned CompressibleStringImpl::currentSizeInBytes() const
+{
+ if (UNLIKELY(isCompressed()))
+ return m_compressedDataSize;
+ return originalContentSizeInBytes();
+}
+
+String CompressibleStringImpl::toString()
+{
+ if (UNLIKELY(isCompressed()))
+ uncompressString();
+ return m_impl.get();
+}
+
+const LChar* CompressibleStringImpl::characters8()
+{
+ return toString().characters8();
+}
+
+const UChar* CompressibleStringImpl::characters16()
+{
+ return toString().characters16();
+}
+
+void CompressibleStringImpl::compressString()
+{
+ // UMA
+ // Count when the tab is background
+
+ ASSERT(m_impl);
+ ASSERT(!isCompressed());
+ ASSERT(!m_compressedDataSize);
+
+ // TODO(hajimehoshi): Now components offers funcitons accepting only
+ // std::strings. This is not efficient. We should offer char* version.
+ std::string in, out;
+ if (m_is8Bit)
+ in = std::string(reinterpret_cast<const char*>(m_impl->characters8()), originalContentSizeInBytes());
+ else
+ in = std::string(reinterpret_cast<const char*>(m_impl->characters16()), originalContentSizeInBytes());
+ compression::GzipCompress(in, &out);
+
+ m_impl = nullptr;
+ m_compressedData = Partitions::fastMalloc(out.size(), "CompressibleString");
+ memcpy(m_compressedData, out.c_str(), out.size());
+ m_compressedDataSize = out.size();
+}
+
+void CompressibleStringImpl::uncompressString()
+{
+ // UMA
+ // Count when the tab is background
+
+ ASSERT(m_compressedData);
+ ASSERT(m_compressedDataSize);
+ ASSERT(!m_impl);
+
+ std::string in(static_cast<const char*>(m_compressedData), m_compressedDataSize);
+ std::string out;
+ compression::GzipUncompress(in, &out);
+
+ if (m_is8Bit) {
+ LChar* data = nullptr;
+ m_impl = StringImpl::createUninitialized(out.size() / sizeof(LChar), data);
+ memcpy(data, out.c_str(), out.size());
+ } else {
+ UChar* data = nullptr;
+ m_impl = StringImpl::createUninitialized(out.size() / sizeof(UChar), data);
+ memcpy(data, out.c_str(), out.size());
+ }
+
+ Partitions::fastFree(m_compressedData);
+ m_compressedData = nullptr;
+ m_compressedDataSize = 0;
+ ASSERT(m_is8Bit == m_impl->is8Bit());
+ ASSERT(m_originalLength == m_impl->length());
+}
+
+CompressibleString::CompressibleString()
+ : m_impl(nullptr)
+{
+}
+
+CompressibleString::CompressibleString(const CompressibleString& rhs)
+ : m_impl(rhs.m_impl)
+{
+}
+
+CompressibleString::CompressibleString(PassRefPtr<StringImpl> impl)
+ : m_impl(impl ? adoptRef(new CompressibleStringImpl(impl)) : nullptr)
+{
+}
+
+void CompressibleString::compressString() const
+{
+ m_impl->compressString();
+}
+
+void CompressibleString::uncompressString() const
+{
+ m_impl->uncompressString();
+}
+
+} // namespace WTF
« no previous file with comments | « third_party/WebKit/Source/wtf/text/CompressibleString.h ('k') | third_party/WebKit/Source/wtf/text/CompressibleStringTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698