Commit a8c2e78e authored by levin@chromium.org's avatar levin@chromium.org

2009-07-30 Anton Muhin <antonm@chromium.org>

        Reviewed by David Levin.

        Cache v8 strings when converting from WebCore::String to v8 string.
        https://bugs.webkit.org/show_bug.cgi?id=27655

        * bindings/v8/V8Binding.cpp:
        (WebCore::v8String): now just immediately calls v8ExternalString
        (WebCore::enableStringImplCache): enables caching of conversions from WebCore::StringImpl to
        v8::String
        (WebCore::makeExternalString): utilty function to create external v8::String out of
        WebCore::String
        (WebCore::getStringCache): static function to access string cache
        (WebCore::cachedStringCallback): callback for weak handles of v8::Strings stored in the
        cache
        (WebCore::v8ExternalString): if caching enabled, checks if there is already v8::String for
        the given WebCore::StringImpl.  If present, returns it, otherwise creates a new v8 external
        string.
        * bindings/v8/V8Binding.h:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@46580 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent f628e80f
2009-07-30 Anton Muhin <antonm@chromium.org>
Reviewed by David Levin.
Cache v8 strings when converting from WebCore::String to v8 string.
https://bugs.webkit.org/show_bug.cgi?id=27655
* bindings/v8/V8Binding.cpp:
(WebCore::v8String): now just immediately calls v8ExternalString
(WebCore::enableStringImplCache): enables caching of conversions from WebCore::StringImpl to
v8::String
(WebCore::makeExternalString): utilty function to create external v8::String out of
WebCore::String
(WebCore::getStringCache): static function to access string cache
(WebCore::cachedStringCallback): callback for weak handles of v8::Strings stored in the
cache
(WebCore::v8ExternalString): if caching enabled, checks if there is already v8::String for
the given WebCore::StringImpl. If present, returns it, otherwise creates a new v8 external
string.
* bindings/v8/V8Binding.h:
2009-07-30 Xan Lopez <xlopez@igalia.com>
Reviewed by Maciej Stachowiak.
......
......@@ -35,7 +35,10 @@
#include "CString.h"
#include "MathExtras.h"
#include "PlatformString.h"
#include "StdLibExtras.h"
#include "StringBuffer.h"
#include "StringHash.h"
#include "Threading.h"
#include <v8.h>
......@@ -142,16 +145,72 @@ AtomicString v8ValueToAtomicWebCoreString(v8::Handle<v8::Value> v8String)
v8::Handle<v8::String> v8String(const String& string)
{
if (!string.length())
return v8::String::Empty();
return v8::String::NewExternal(new WebCoreStringResource(string));
return v8ExternalString(string);
}
static bool stringImplCacheEnabled = false;
void enableStringImplCache()
{
stringImplCacheEnabled = true;
}
static v8::Local<v8::String> makeExternalString(const String& string)
{
WebCoreStringResource* stringResource = new WebCoreStringResource(string);
v8::Local<v8::String> newString = v8::String::NewExternal(stringResource);
if (newString.IsEmpty())
delete stringResource;
return newString;
}
typedef HashMap<StringImpl*, v8::String*> StringCache;
static StringCache& getStringCache()
{
ASSERT(WTF::isMainThread());
DEFINE_STATIC_LOCAL(StringCache, mainThreadStringCache, ());
return mainThreadStringCache;
}
static void cachedStringCallback(v8::Persistent<v8::Value> wrapper, void* parameter)
{
ASSERT(WTF::isMainThread());
StringImpl* stringImpl = static_cast<StringImpl*>(parameter);
ASSERT(getStringCache().contains(stringImpl));
getStringCache().remove(stringImpl);
wrapper.Dispose();
stringImpl->deref();
}
v8::Local<v8::String> v8ExternalString(const String& string)
{
if (!string.length())
return v8::String::Empty();
return v8::String::NewExternal(new WebCoreStringResource(string));
if (!stringImplCacheEnabled)
return makeExternalString(string);
StringImpl* stringImpl = string.impl();
StringCache& stringCache = getStringCache();
v8::String* cachedV8String = stringCache.get(stringImpl);
if (cachedV8String)
return v8::Local<v8::String>(cachedV8String);
v8::Local<v8::String> newString = makeExternalString(string);
if (newString.IsEmpty())
return newString;
v8::Persistent<v8::String> wrapper = v8::Persistent<v8::String>::New(newString);
if (wrapper.IsEmpty())
return newString;
stringImpl->ref();
wrapper.MakeWeak(stringImpl, cachedStringCallback);
stringCache.set(stringImpl, *wrapper);
return newString;
}
} // namespace WebCore
......@@ -61,6 +61,11 @@ namespace WebCore {
// underlying buffer alive while the string is still live in the V8 engine.
v8::Local<v8::String> v8ExternalString(const String&);
// Enables caching v8 wrappers created for WebCore::StringImpl. Currently this cache requires
// all the calls (both to convert WebCore::String to v8::String and to GC the handle)
// to be performed on the main thread.
void enableStringImplCache();
// 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.
inline int toInt32(v8::Handle<v8::Value> value, bool& ok)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment