diff options
author | iposva@chromium.org <iposva@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-17 04:59:14 +0000 |
---|---|---|
committer | iposva@chromium.org <iposva@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-17 04:59:14 +0000 |
commit | 001093ee6111ab841b56b620a20c9d5ce9e1a524 (patch) | |
tree | 1c33d7692484d3d34aa6125842771c46cb14c703 /webkit/port | |
parent | 982371ec6aebb5459d2465036aa86dbb816e5640 (diff) | |
download | chromium_src-001093ee6111ab841b56b620a20c9d5ce9e1a524.zip chromium_src-001093ee6111ab841b56b620a20c9d5ce9e1a524.tar.gz chromium_src-001093ee6111ab841b56b620a20c9d5ce9e1a524.tar.bz2 |
To avoid recreating WebCore::String objects constantly, we now transform V8 strings to external strings when they get handed out to WebKit the first time.
Review URL: http://codereview.chromium.org/20121
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9871 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/port')
-rw-r--r-- | webkit/port/bindings/v8/v8_binding.h | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/webkit/port/bindings/v8/v8_binding.h b/webkit/port/bindings/v8/v8_binding.h index 4a011cf..553364c 100644 --- a/webkit/port/bindings/v8/v8_binding.h +++ b/webkit/port/bindings/v8/v8_binding.h @@ -7,6 +7,7 @@ #include "config.h" +#include "AtomicString.h" #include "CString.h" #include "MathExtras.h" #include "PlatformString.h" @@ -43,10 +44,12 @@ class WebCoreStringResource: public v8::String::ExternalStringResource { // Keeps the string buffer alive until the V8 engine garbage collects it. String external_string_; }; - -// TODO: converting between WebCore::String and V8 string is expensive. -// Optimize it !!! -inline String ToWebCoreString(v8::Handle<v8::Value> obj) { + +// Convert a v8::String to a WebCore::String. If the V8 string is not already +// an external string then it is transformed into an external string at this +// point to avoid repeated conversions. +inline String ToWebCoreString(v8::Handle<v8::Value> obj, + bool atomic = false) { v8::TryCatch block; v8::Local<v8::String> v8_str = obj->ToString(); if (v8_str.IsEmpty()) @@ -59,9 +62,28 @@ inline String ToWebCoreString(v8::Handle<v8::Value> obj) { } int length = v8_str->Length(); + if (length == 0) { + // Avoid trying to morph empty strings, as they do not have enough room to + // contain the external reference. + return ""; + } + + // Copy the characters from the v8::String into a WebCore::String and allocate + // an external resource which will be attached to the v8::String. StringBuffer buf(length); v8_str->Write(reinterpret_cast<uint16_t*>(buf.characters()), 0, length); - return String::adopt(buf); + String result; + if (atomic) { + result = AtomicString(buf.characters(), length); + } else { + result = StringImpl::adopt(buf); + } + WebCoreStringResource* resource = new WebCoreStringResource(result); + if (!v8_str->MakeExternal(resource)) { + // In case of a failure delete the external resource as it was not used. + delete resource; + } + return result; } inline String valueToStringWithNullCheck(v8::Handle<v8::Value> value) { @@ -78,7 +100,7 @@ inline String valueToStringWithNullOrUndefinedCheck( // Convert a value to a 32-bit integer. The conversion fails if the // value cannot be converted to an integer or converts to nan or to an // infinity. -// FIXME: Rename to toWebCorString() once V8 bindings migration is complete. +// FIXME: Rename to toInt32() once V8 bindings migration is complete. inline int ToInt32(v8::Handle<v8::Value> value, bool& ok) { ok = true; |