summaryrefslogtreecommitdiffstats
path: root/webkit/port
diff options
context:
space:
mode:
authordarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-06 00:16:55 +0000
committerdarin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-06 00:16:55 +0000
commit9b4099e7bdeeb875b218022d7aa387a50aa79dd7 (patch)
treee703de08cb29254d894393c65c439b06e5afde6e /webkit/port
parentb47610c32597b728ab6975c8e0e2e0f21ef39fc2 (diff)
downloadchromium_src-9b4099e7bdeeb875b218022d7aa387a50aa79dd7.zip
chromium_src-9b4099e7bdeeb875b218022d7aa387a50aa79dd7.tar.gz
chromium_src-9b4099e7bdeeb875b218022d7aa387a50aa79dd7.tar.bz2
Nearly eliminate heap allocations required to resolve a NPIdentifier from a V8 string.
I'd like to / hope to put together some automated tests to focus on NPRuntime scripting. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@410 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/port')
-rw-r--r--webkit/port/bindings/v8/npruntime.cpp37
-rw-r--r--webkit/port/bindings/v8/v8_np_utils.cpp21
2 files changed, 40 insertions, 18 deletions
diff --git a/webkit/port/bindings/v8/npruntime.cpp b/webkit/port/bindings/v8/npruntime.cpp
index 074fa46..f47790a 100644
--- a/webkit/port/bindings/v8/npruntime.cpp
+++ b/webkit/port/bindings/v8/npruntime.cpp
@@ -31,6 +31,7 @@
#include <string>
#include <v8.h>
#include "base/stats_counters.h"
+#include "base/string_piece.h"
#include "bindings/npruntime.h"
#include "np_v8object.h"
#include "npruntime_priv.h"
@@ -49,7 +50,7 @@ using namespace v8;
// Need a platform abstraction which we can use.
// static Lock StringIdentifierMapLock;
-typedef std::map<std::string, PrivateIdentifier*> StringIdentifierMap;
+typedef std::map<StringPiece, PrivateIdentifier*> StringIdentifierMap;
static StringIdentifierMap* getStringIdentifierMap() {
static StringIdentifierMap* stringIdentifierMap = 0;
@@ -78,18 +79,26 @@ NPIdentifier NPN_GetStringIdentifier(const NPUTF8* name) {
if (name) {
// AutoLock safeLock(StringIdentifierMapLock);
- StringIdentifierMap::iterator iter =
- getStringIdentifierMap()->find(std::string(name));
- if (iter != getStringIdentifierMap()->end())
+ StringIdentifierMap* identMap = getStringIdentifierMap();
+
+ // We use StringPiece here as the key-type to avoid a string copy to
+ // construct the map key.
+ StringPiece nameStr(name);
+ StringIdentifierMap::iterator iter = identMap->find(nameStr);
+ if (iter != identMap->end())
return static_cast<NPIdentifier>(iter->second);
- PrivateIdentifier* identifier = reinterpret_cast<PrivateIdentifier*>(
- malloc(sizeof(PrivateIdentifier)));
- // We never release identifier names, so this dictionary will grow,
- // as will the memory for the identifier name strings.
+ size_t nameLen = nameStr.length();
+
+ // We never release identifier names, so this dictionary will grow, as
+ // will the memory for the identifier name strings.
+ PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(
+ malloc(sizeof(PrivateIdentifier) + nameLen + 1));
+ memcpy(identifier + 1, name, nameLen + 1);
identifier->isString = true;
- identifier->value.string = strdup(name);
- (*getStringIdentifierMap())[std::string(name)] = identifier;
+ identifier->value.string = reinterpret_cast<NPUTF8*>(identifier + 1);
+ (*identMap)[StringPiece(identifier->value.string, nameLen)] =
+ identifier;
return (NPIdentifier)identifier;
}
@@ -109,8 +118,10 @@ void NPN_GetStringIdentifiers(const NPUTF8** names, int32_t nameCount,
NPIdentifier NPN_GetIntIdentifier(int32_t intid) {
// AutoLock safeLock(IntIdentifierMapLock);
- IntIdentifierMap::iterator iter = getIntIdentifierMap()->find(intid);
- if (iter != getIntIdentifierMap()->end())
+ IntIdentifierMap* identMap = getIntIdentifierMap();
+
+ IntIdentifierMap::iterator iter = identMap->find(intid);
+ if (iter != identMap->end())
return static_cast<NPIdentifier>(iter->second);
PrivateIdentifier* identifier = reinterpret_cast<PrivateIdentifier*>(
@@ -118,7 +129,7 @@ NPIdentifier NPN_GetIntIdentifier(int32_t intid) {
// We never release identifier names, so this dictionary will grow.
identifier->isString = false;
identifier->value.number = intid;
- (*getIntIdentifierMap())[intid] = identifier;
+ (*identMap)[intid] = identifier;
return (NPIdentifier)identifier;
}
diff --git a/webkit/port/bindings/v8/v8_np_utils.cpp b/webkit/port/bindings/v8/v8_np_utils.cpp
index 06e02b8..3887460 100644
--- a/webkit/port/bindings/v8/v8_np_utils.cpp
+++ b/webkit/port/bindings/v8/v8_np_utils.cpp
@@ -31,6 +31,7 @@
#include "v8_np_utils.h"
+#include "base/scoped_ptr.h"
#include "base/string_util.h"
#include "DOMWindow.h"
#include "Frame.h"
@@ -118,9 +119,19 @@ v8::Handle<v8::Value> ConvertNPVariantToV8Object(const NPVariant* variant,
// Helper function to create an NPN String Identifier from a v8 string.
NPIdentifier GetStringIdentifier(v8::Handle<v8::String> str) {
- char *buf = new char[str->Length() + 1];
- str->WriteAscii(buf);
- NPIdentifier ident = NPN_GetStringIdentifier(buf);
- delete[] buf;
- return ident;
+ const int kStackBufSize = 100;
+
+ int buf_len = str->Length() + 1;
+ if (buf_len <= kStackBufSize) {
+ // Use local stack buffer to avoid heap allocations for small strings.
+ // Here we should only use the stack space for stack_buf when it's used,
+ // not when we use the heap.
+ char stack_buf[kStackBufSize];
+ str->WriteAscii(stack_buf);
+ return NPN_GetStringIdentifier(stack_buf);
+ }
+
+ scoped_array<char> heap_buf(new char[buf_len]);
+ str->WriteAscii(heap_buf.get());
+ return NPN_GetStringIdentifier(heap_buf.get());
}