Bug 605186 - js_NewStringFromCharBuffer should create short strings if it can (r=waldo)

This commit is contained in:
Luke Wagner 2010-10-26 16:20:21 -07:00
Родитель fa09dea8cb
Коммит 052bd1aace
2 изменённых файлов: 48 добавлений и 41 удалений

Просмотреть файл

@ -3418,6 +3418,47 @@ js_NewString(JSContext *cx, jschar *chars, size_t length)
return str;
}
static JS_ALWAYS_INLINE JSString *
NewShortString(JSContext *cx, const jschar *chars, size_t length)
{
JS_ASSERT(JSShortString::fitsIntoShortString(length));
JSShortString *str = js_NewGCShortString(cx);
if (!str)
return NULL;
jschar *storage = str->init(length);
js_short_strncpy(storage, chars, length);
storage[length] = 0;
return str->header();
}
static JSString *
NewShortString(JSContext *cx, const char *chars, size_t length)
{
JS_ASSERT(JSShortString::fitsIntoShortString(length));
JSShortString *str = js_NewGCShortString(cx);
if (!str)
return NULL;
jschar *storage = str->init(length);
if (js_CStringsAreUTF8) {
#ifdef DEBUG
size_t oldLength = length;
#endif
if (!js_InflateStringToBuffer(cx, chars, length, storage, &length))
return NULL;
JS_ASSERT(length <= oldLength);
storage[length] = 0;
str->resetLength(length);
} else {
size_t n = length;
jschar *p = storage;
while (n--)
*p++ = jschar(*chars++);
*p = 0;
}
return str->header();
}
static const size_t sMinWasteSize = 16;
JSString *
@ -3427,6 +3468,11 @@ js_NewStringFromCharBuffer(JSContext *cx, JSCharBuffer &cb)
return ATOM_TO_STRING(cx->runtime->atomState.emptyAtom);
size_t length = cb.length();
JS_STATIC_ASSERT(JSShortString::MAX_SHORT_STRING_LENGTH < JSCharBuffer::InlineLength);
if (JSShortString::fitsIntoShortString(length))
return NewShortString(cx, cb.begin(), length);
if (!cb.append('\0'))
return NULL;
@ -3520,47 +3566,6 @@ void printJSStringStats(JSRuntime *rt)
}
#endif
JSString *
NewShortString(JSContext *cx, const jschar *chars, size_t length)
{
JS_ASSERT(JSShortString::fitsIntoShortString(length));
JSShortString *str = js_NewGCShortString(cx);
if (!str)
return NULL;
jschar *storage = str->init(length);
js_short_strncpy(storage, chars, length);
storage[length] = 0;
return str->header();
}
JSString *
NewShortString(JSContext *cx, const char *chars, size_t length)
{
JS_ASSERT(JSShortString::fitsIntoShortString(length));
JSShortString *str = js_NewGCShortString(cx);
if (!str)
return NULL;
jschar *storage = str->init(length);
if (js_CStringsAreUTF8) {
#ifdef DEBUG
size_t oldLength = length;
#endif
if (!js_InflateStringToBuffer(cx, chars, length, storage, &length))
return NULL;
JS_ASSERT(length <= oldLength);
storage[length] = 0;
str->resetLength(length);
} else {
size_t n = length;
jschar *p = storage;
while (n--)
*p++ = jschar(*chars++);
*p = 0;
}
return str->header();
}
JSString *
js_NewStringCopyN(JSContext *cx, const jschar *s, size_t n)
{

Просмотреть файл

@ -314,6 +314,8 @@ class Vector : AllocPolicy
/* accessors */
enum { InlineLength = N };
size_t length() const {
return usingInlineStorage() ? inlineLength() : heapLength();
}