drastically reduce unnecessary malloc calls from js_Atomize. r=brendan@mozilla.org. a=brendan@mozilla.org

This commit is contained in:
jband%netscape.com 2000-07-21 00:57:19 +00:00
parent b5027cd07a
commit 43adc72647
3 changed files with 46 additions and 8 deletions

View File

@ -545,14 +545,32 @@ js_Atomize(JSContext *cx, const char *bytes, size_t length, uintN flags)
JSAtom *atom;
char buf[2 * ALIGNMENT(JSString)];
/*
* Avoiding the malloc in js_InflateString on shorter strings saves us
* over 20,000 malloc calls on mozilla browser startup. This compares to
* only 131 calls where the string is longer than a 32 char buffer.
* The vast majority of atomized strings are already in the hashtable. So
* js_AtomizeString rarely has to copy the temp string we make.
*/
#define ATOMIZE_BUF_MAX 32
jschar inflated[ATOMIZE_BUF_MAX];
if (length < ATOMIZE_BUF_MAX) {
js_InflateStringToBuffer(inflated, bytes, length);
chars = inflated;
} else {
chars = js_InflateString(cx, bytes, length);
if (!chars)
return NULL;
flags |= ATOM_NOCOPY;
}
str = ALIGN(buf, JSString);
chars = js_InflateString(cx, bytes, length);
if (!chars)
return NULL;
str->chars = chars;
str->length = length;
atom = js_AtomizeString(cx, str, ATOM_TMPSTR | ATOM_NOCOPY | flags);
if (!atom || ATOM_TO_STRING(atom)->chars != chars)
atom = js_AtomizeString(cx, str, ATOM_TMPSTR | flags);
if (chars != inflated && (!atom || ATOM_TO_STRING(atom)->chars != chars))
JS_free(cx, chars);
return atom;
}

View File

@ -2395,6 +2395,19 @@ js_strncpy(jschar *t, const jschar *s, size_t n)
return t;
}
#define INFLATE_STRING_BODY \
for (i = 0; i < length; i++) \
chars[i] = (unsigned char) bytes[i]; \
chars[i] = 0;
void
js_InflateStringToBuffer(jschar *chars, const char *bytes, size_t length)
{
size_t i;
INFLATE_STRING_BODY
}
jschar *
js_InflateString(JSContext *cx, const char *bytes, size_t length)
{
@ -2404,9 +2417,9 @@ js_InflateString(JSContext *cx, const char *bytes, size_t length)
chars = (jschar *) JS_malloc(cx, (length + 1) * sizeof(jschar));
if (!chars)
return NULL;
for (i = 0; i < length; i++)
chars[i] = (unsigned char) bytes[i];
chars[i] = 0;
INFLATE_STRING_BODY
return chars;
}

View File

@ -280,6 +280,13 @@ js_InflateString(JSContext *cx, const char *bytes, size_t length);
extern char *
js_DeflateString(JSContext *cx, const jschar *chars, size_t length);
/*
* Inflate bytes to JS chars into a buffer.
* 'chars' must be large enough for 'length'+1 jschars.
*/
extern void
js_InflateStringToBuffer(jschar *chars, const char *bytes, size_t length);
/*
* Associate bytes with str in the deflated string cache, returning true on
* successful association, false on out of memory.