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

Unified Diff: third_party/WebKit/Source/bindings/core/v8/serialization/LongStringCollection.cpp

Issue 2768113002: WIP: Create a LongStringCollection that transfers long strings with fewer copies.
Patch Set: Created 3 years, 9 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/bindings/core/v8/serialization/LongStringCollection.cpp
diff --git a/third_party/WebKit/Source/bindings/core/v8/serialization/LongStringCollection.cpp b/third_party/WebKit/Source/bindings/core/v8/serialization/LongStringCollection.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..2d6f189864c8afff41c08f6ab9e76d854196c874
--- /dev/null
+++ b/third_party/WebKit/Source/bindings/core/v8/serialization/LongStringCollection.cpp
@@ -0,0 +1,93 @@
+// Copyright 2017 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 "bindings/core/v8/serialization/LongStringCollection.h"
+
+#include "bindings/core/v8/V8Binding.h"
+
+namespace blink {
+
+// Bypass the cache. This StringImpl is definitely not cached.
+static v8::Local<v8::String> makeExternalString(v8::Isolate* isolate,
+ const String& string) {
+ if (string.is8Bit()) {
+ WebCoreStringResource8* stringResource = new WebCoreStringResource8(string);
+ v8::Local<v8::String> newString;
+ if (!v8::String::NewExternalOneByte(isolate, stringResource)
+ .ToLocal(&newString)) {
+ delete stringResource;
+ return v8::String::Empty(isolate);
+ }
+ return newString;
+ }
+
+ WebCoreStringResource16* stringResource = new WebCoreStringResource16(string);
+ v8::Local<v8::String> newString;
+ if (!v8::String::NewExternalTwoByte(isolate, stringResource)
+ .ToLocal(&newString)) {
+ delete stringResource;
+ return v8::String::Empty(isolate);
+ }
+ return newString;
+}
+
+LongStringCollection::~LongStringCollection() = default;
+
+Vector<v8::Local<v8::String>> LongStringCollection::getAll(
+ v8::Isolate* isolate) const {
+#if DCHECK_IS_ON()
+ DCHECK(m_v8Strings.isEmpty() || m_isolate == isolate);
+#endif
+
+ Vector<v8::Local<v8::String>> result;
+ switch (m_policy.type()) {
+ case LongStringPolicy::kNone:
+ break;
+
+ case LongStringPolicy::kSameIsolate:
+ result.reserveInitialCapacity(m_v8Strings.size());
+ for (const auto& v8String : m_v8Strings)
+ result.uncheckedAppend(v8String.Get(isolate));
+ break;
+
+ case LongStringPolicy::kSameProcess:
+ result.reserveInitialCapacity(m_wtfStrings.size());
+ for (const auto& wtfString : m_wtfStrings) {
+ result.uncheckedAppend(makeExternalString(isolate, wtfString));
+ }
+ break;
+ }
+ return result;
+}
+
+uint32_t LongStringCollection::put(v8::Isolate* isolate,
+ v8::Local<v8::String> string) {
+ DCHECK_GE(string->Length(), m_policy.minimumLength());
+#if DCHECK_IS_ON()
+ DCHECK(!m_isolate || m_isolate == isolate);
+ m_isolate = isolate;
+#endif
+
+ switch (m_policy.type()) {
+ case LongStringPolicy::kNone:
+ NOTREACHED();
+ return 0;
+
+ case LongStringPolicy::kSameIsolate:
+ m_v8Strings.push_back(v8::Global<v8::String>(isolate, string));
+ return m_v8Strings.size() - 1;
+
+ case LongStringPolicy::kSameProcess: {
+ String wtfString =
+ v8StringToWebCoreString<String>(string, DoNotExternalize);
+ if (!wtfString.isSafeToSendToAnotherThread())
+ wtfString = wtfString.isolatedCopy();
+ DCHECK(wtfString.isSafeToSendToAnotherThread());
+ m_wtfStrings.push_back(std::move(wtfString));
+ return m_wtfStrings.size() - 1;
+ }
+ }
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698