Atomize unit strings at birth (511567, r=shaver).
authorAndreas Gal <gal@mozilla.com>
Wed, 19 Aug 2009 19:37:59 -0700
changeset 31905 3d98dade7db3ca2c8f9611e9e691a9875272efbc
parent 31904 c7b5f61928098fad9c0dcf520ea01644c81e8a07
child 31906 9d5b7c57cf4295981167db5045853a66eb227c9a
push idunknown
push userunknown
push dateunknown
reviewersshaver
bugs511567
milestone1.9.3a1pre
Atomize unit strings at birth (511567, r=shaver).
js/src/jsatom.cpp
js/src/jsstr.cpp
js/src/trace-test/tests/basic/testAtomize.js
--- a/js/src/jsatom.cpp
+++ b/js/src/jsatom.cpp
@@ -668,16 +668,22 @@ js_AtomizeString(JSContext *cx, JSString
     JSDHashTable *table;
     JSAtomHashEntry *entry;
     JSString *key;
     uint32 gen;
 
     JS_ASSERT(!(flags & ~(ATOM_PINNED|ATOM_INTERNED|ATOM_TMPSTR|ATOM_NOCOPY)));
     JS_ASSERT_IF(flags & ATOM_NOCOPY, flags & ATOM_TMPSTR);
 
+    if (str->length() == 1) {
+        jschar c = str->chars()[0];
+        if (c < UNIT_STRING_LIMIT)
+            return (JSAtom*) STRING_TO_JSVAL(js_GetUnitStringForChar(cx, c));
+    }
+
     state = &cx->runtime->atomState;
     table = &state->stringAtoms;
 
     JS_LOCK(cx, &state->lock);
     entry = TO_ATOM_ENTRY(JS_DHashTableOperate(table, str, JS_DHASH_ADD));
     if (!entry)
         goto failed_hash_add;
     if (entry->keyAndFlags != 0) {
--- a/js/src/jsstr.cpp
+++ b/js/src/jsstr.cpp
@@ -2609,18 +2609,20 @@ js_GetUnitStringForChar(JSContext *cx, j
     if (!rt->unitStrings[c]) {
         JSString *str;
 
         cp = UNIT_STRING_SPACE_RT(rt);
         str = js_NewString(cx, cp + 2 * c, 1);
         if (!str)
             return NULL;
         JS_LOCK_GC(rt);
-        if (!rt->unitStrings[c])
+        if (!rt->unitStrings[c]) {
+            str->flatSetAtomized();
             rt->unitStrings[c] = str;
+        }
 #ifdef DEBUG
         else
             str->initFlat(NULL, 0);  /* avoid later assertion (bug 479381) */
 #endif
         JS_UNLOCK_GC(rt);
     }
     return rt->unitStrings[c];
 }
new file mode 100644
--- /dev/null
+++ b/js/src/trace-test/tests/basic/testAtomize.js
@@ -0,0 +1,10 @@
+function testAtomize() {
+    x = {};
+    for (var i = 0; i < 65536; ++i)
+	x[String.fromCharCode(i)] = 1;
+    var z = 0;
+    for (var p in x)
+	++z;
+    return z;
+}
+assertEq(testAtomize(), 65536)