summaryrefslogtreecommitdiffstats
path: root/webkit/port/bindings/v8/npruntime.cpp
diff options
context:
space:
mode:
authormichaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-11 21:06:49 +0000
committermichaeln@google.com <michaeln@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-11 21:06:49 +0000
commit401a26101f9f42fe71be074e0fce175016ab978f (patch)
tree2947c91cc243c368d42d503db6304cc2bdd19279 /webkit/port/bindings/v8/npruntime.cpp
parentff66fd2380ccdd1bfad3cecb6deade5a6f66f93a (diff)
downloadchromium_src-401a26101f9f42fe71be074e0fce175016ab978f.zip
chromium_src-401a26101f9f42fe71be074e0fce175016ab978f.tar.gz
chromium_src-401a26101f9f42fe71be074e0fce175016ab978f.tar.bz2
Removed dependency on class StringPiece.
Review URL: http://codereview.chromium.org/12621 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6821 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/port/bindings/v8/npruntime.cpp')
-rw-r--r--webkit/port/bindings/v8/npruntime.cpp51
1 files changed, 34 insertions, 17 deletions
diff --git a/webkit/port/bindings/v8/npruntime.cpp b/webkit/port/bindings/v8/npruntime.cpp
index ca0c45a..78c5323 100644
--- a/webkit/port/bindings/v8/npruntime.cpp
+++ b/webkit/port/bindings/v8/npruntime.cpp
@@ -31,7 +31,6 @@
#include <string>
#include <v8.h>
-#include "base/string_piece.h"
#include "bindings/npruntime.h"
#include "ChromiumBridge.h"
#include "np_v8object.h"
@@ -51,7 +50,29 @@ using namespace v8;
// Need a platform abstraction which we can use.
// static Lock StringIdentifierMapLock;
-typedef std::map<StringPiece, PrivateIdentifier*> StringIdentifierMap;
+namespace {
+
+// We use StringKey here as the key-type to avoid a string copy to
+// construct the map key and for faster comparisons than strcmp.
+struct StringKey {
+ StringKey(const char* str) : string(str), length(strlen(str)) {}
+ const char* string;
+ const size_t length;
+};
+
+inline bool operator<(const StringKey& x, const StringKey& y) {
+ // Shorter strings are less than longer strings, memcmp breaks ties.
+ if (x.length < y.length)
+ return true;
+ else if (x.length > y.length)
+ return false;
+ else
+ return memcmp(x.string, y.string, y.length) < 0;
+}
+
+} // namespace
+
+typedef std::map<const StringKey, PrivateIdentifier*> StringIdentifierMap;
static StringIdentifierMap* getStringIdentifierMap() {
static StringIdentifierMap* stringIdentifierMap = 0;
@@ -68,7 +89,7 @@ typedef std::map<int, PrivateIdentifier*> IntIdentifierMap;
static IntIdentifierMap* getIntIdentifierMap() {
static IntIdentifierMap* intIdentifierMap = 0;
if (!intIdentifierMap)
- intIdentifierMap = new IntIdentifierMap;
+ intIdentifierMap = new IntIdentifierMap();
return intIdentifierMap;
}
@@ -80,26 +101,23 @@ NPIdentifier NPN_GetStringIdentifier(const NPUTF8* name) {
if (name) {
// AutoLock safeLock(StringIdentifierMapLock);
+ StringKey key(name);
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);
+ StringIdentifierMap::iterator iter = identMap->find(key);
if (iter != identMap->end())
return static_cast<NPIdentifier>(iter->second);
- size_t nameLen = nameStr.length();
+ size_t nameLen = key.length;
- // We never release identifier names, so this dictionary will grow, as
- // will the memory for the identifier name strings.
+ // We never release identifiers, so this dictionary will grow.
PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(
malloc(sizeof(PrivateIdentifier) + nameLen + 1));
- memcpy(identifier + 1, name, nameLen + 1);
+ char* nameStorage = reinterpret_cast<char*>(identifier + 1);
+ memcpy(nameStorage, name, nameLen + 1);
identifier->isString = true;
- identifier->value.string = reinterpret_cast<NPUTF8*>(identifier + 1);
- (*identMap)[StringPiece(identifier->value.string, nameLen)] =
- identifier;
+ identifier->value.string = reinterpret_cast<NPUTF8*>(nameStorage);
+ key.string = nameStorage;
+ (*identMap)[key] = identifier;
return (NPIdentifier)identifier;
}
@@ -120,14 +138,13 @@ NPIdentifier NPN_GetIntIdentifier(int32_t intid) {
// AutoLock safeLock(IntIdentifierMapLock);
IntIdentifierMap* identMap = getIntIdentifierMap();
-
IntIdentifierMap::iterator iter = identMap->find(intid);
if (iter != identMap->end())
return static_cast<NPIdentifier>(iter->second);
+ // We never release identifiers, so this dictionary will grow.
PrivateIdentifier* identifier = reinterpret_cast<PrivateIdentifier*>(
malloc(sizeof(PrivateIdentifier)));
- // We never release identifier names, so this dictionary will grow.
identifier->isString = false;
identifier->value.number = intid;
(*identMap)[intid] = identifier;