source: webkit/trunk/JavaScriptCore/kjs/JSString.cpp@ 36006

Last change on this file since 36006 was 36006, checked in by Darin Adler, 17 years ago

JavaScriptCore:

2008-08-30 Darin Adler <Darin Adler>

Reviewed by Maciej.

1.035x as fast on SunSpider overall.
1.127x as fast on SunSpider string tests.
1.910x as fast on SunSpider string-base64 test.

  • API/JSObjectRef.cpp: (JSObjectMakeFunction): Removed unneeded explicit construction of UString.
  • GNUmakefile.am: Added SmallStrings.h and SmallStrings.cpp.
  • JavaScriptCore.pri: Ditto.
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj: Ditto.
  • JavaScriptCore.xcodeproj/project.pbxproj: Ditto.
  • JavaScriptCoreSources.bkl: Ditto.
  • VM/Machine.cpp: (KJS::jsAddSlowCase): Changed to use a code path that doesn't involve a UString constructor. This avoids an extra jump caused by the "in charge" vs. "not in charge" constructors. (KJS::jsAdd): Ditto. (KJS::jsTypeStringForValue): Adopted jsNontrivialString.
  • kjs/ArrayPrototype.cpp: (KJS::arrayProtoFuncToString): Adopted jsEmptyString. (KJS::arrayProtoFuncToLocaleString): Ditto. (KJS::arrayProtoFuncJoin): Ditto.
  • kjs/BooleanPrototype.cpp: (KJS::booleanProtoFuncToString): Adopted jsNontrivialString.
  • kjs/DateConstructor.cpp: (KJS::callDate): Ditto.
  • kjs/DatePrototype.cpp: (KJS::formatLocaleDate): Adopted jsEmptyString and jsNontrivialString. (KJS::dateProtoFuncToString): Ditto. (KJS::dateProtoFuncToUTCString): Ditto. (KJS::dateProtoFuncToDateString): Ditto. (KJS::dateProtoFuncToTimeString): Ditto. (KJS::dateProtoFuncToLocaleString): Ditto. (KJS::dateProtoFuncToLocaleDateString): Ditto. (KJS::dateProtoFuncToLocaleTimeString): Ditto. (KJS::dateProtoFuncToGMTString): Ditto.
  • kjs/ErrorPrototype.cpp: (KJS::ErrorPrototype::ErrorPrototype): Ditto. (KJS::errorProtoFuncToString): Ditto.
  • kjs/JSGlobalData.h: Added SmallStrings.
  • kjs/JSString.cpp: (KJS::jsString): Eliminated the overload that takes a const char*. Added code to use SmallStrings to get strings of small sizes rather than creating a new JSString every time. (KJS::jsSubstring): Added. Used when creating a string from a substring to avoid creating a JSString in cases where the substring will end up empty or as one character. (KJS::jsOwnedString): Added the same code as in jsString.
  • kjs/JSString.h: Added new functions jsEmptyString, jsSingleCharacterString, jsSingleCharacterSubstring, jsSubstring, and jsNontrivialString for various cases where we want to create JSString, and want special handling for small strings. (KJS::JSString::JSString): Added an overload that takes a PassRefPtr of a UString::Rep so you don't have to construct a UString; PassRefPtr can be more efficient. (KJS::jsEmptyString): Added. (KJS::jsSingleCharacterString): Added. (KJS::jsSingleCharacterSubstring): Added. (KJS::jsNontrivialString): Added. (KJS::JSString::getIndex): Adopted jsSingleCharacterSubstring. (KJS::JSString::getStringPropertySlot): Ditto.
  • kjs/NumberPrototype.cpp: (KJS::numberProtoFuncToFixed): Adopted jsNontrivialString. (KJS::numberProtoFuncToExponential): Ditto. (KJS::numberProtoFuncToPrecision): Ditto.
  • kjs/ObjectPrototype.cpp: (KJS::objectProtoFuncToLocaleString): Adopted toThisJSString. (KJS::objectProtoFuncToString): Adopted jsNontrivialString.
  • kjs/RegExpConstructor.cpp: Separated the lastInput value that's used with the lastOvector to return matches from the input value that can be changed via JavaScript. They will be equal in many cases, but not all. (KJS::RegExpConstructor::performMatch): Set input. (KJS::RegExpMatchesArray::RegExpMatchesArray): Ditto. (KJS::RegExpMatchesArray::fillArrayInstance): Adopted jsSubstring. Also, use input rather than lastInput in the appropriate place. (KJS::RegExpConstructor::getBackref): Adopted jsSubstring and jsEmptyString. Added code to handle the case where there is no backref -- before this depended on range checking in UString::substr which is not present in jsSubstring. (KJS::RegExpConstructor::getLastParen): Ditto. (KJS::RegExpConstructor::getLeftContext): Ditto. (KJS::RegExpConstructor::getRightContext): Ditto. (KJS::RegExpConstructor::getValueProperty): Use input rather than lastInput. Also adopt jsEmptyString. (KJS::RegExpConstructor::putValueProperty): Ditto. (KJS::RegExpConstructor::input): Ditto.
  • kjs/RegExpPrototype.cpp: (KJS::regExpProtoFuncToString): Adopt jsNonTrivialString. Also changed to use UString::append to append single characters rather than using += and a C-style string.
  • kjs/SmallStrings.cpp: Added. (KJS::SmallStringsStorage::SmallStringsStorage): Construct the buffer and UString::Rep for all 256 single-character strings for the U+0000 through U+00FF. This covers all the values used in the base64 test as well as most values seen elsewhere on the web as well. It's possible that later we might fix this to only work for U+0000 through U+007F but the others are used quite a bit in the current version of the base64 test. (KJS::SmallStringsStorage::~SmallStringsStorage): Free memory. (KJS::SmallStrings::SmallStrings): Create a set of small strings, initially not created; created later when they are used. (KJS::SmallStrings::~SmallStrings): Deallocate. Not left compiler generated because the SmallStringsStorage class's destructor needs to be visible. (KJS::SmallStrings::mark): Mark all the strings. (KJS::SmallStrings::createEmptyString): Create a cell for the empty string. Called only the first time. (KJS::SmallStrings::createSingleCharacterString): Create a cell for one of the single-character strings. Called only the first time.
  • kjs/SmallStrings.h: Added.
  • kjs/StringConstructor.cpp: (KJS::stringFromCharCodeSlowCase): Factored out of strinFromCharCode. Only used for cases where the caller does not pass exactly one argument. (KJS::stringFromCharCode): Adopted jsSingleCharacterString. (KJS::callStringConstructor): Adopted jsEmptyString.
  • kjs/StringObject.cpp: (KJS::StringObject::StringObject): Adopted jsEmptyString.
  • kjs/StringPrototype.cpp: (KJS::stringProtoFuncReplace): Adopted jsSubstring. (KJS::stringProtoFuncCharAt): Adopted jsEmptyString and jsSingleCharacterSubstring and also added a special case when the index is an immediate number to avoid conversion to and from floating point, since that's the common case. (KJS::stringProtoFuncCharCodeAt): Ditto. (KJS::stringProtoFuncMatch): Adopted jsSubstring and jsEmptyString. (KJS::stringProtoFuncSlice): Adopted jsSubstring and jsSingleCharacterSubstring. Also got rid of some unneeded locals and removed unneeded code to set the length property of the array, since it is automatically updated as values are added to the array. (KJS::stringProtoFuncSplit): Adopted jsEmptyString. (KJS::stringProtoFuncSubstr): Adopted jsSubstring. (KJS::stringProtoFuncSubstring): Ditto.
  • kjs/collector.cpp: (KJS::Heap::collect): Added a call to mark SmallStrings.
  • kjs/ustring.cpp: (KJS::UString::expandedSize): Made this a static member function since it doesn't need to look at any data members. (KJS::UString::expandCapacity): Use a non-inline function, makeNull, to set the rep to null in failure cases. This avoids adding a PIC branch for the normal case when there is no failure. (KJS::UString::expandPreCapacity): Ditto. (KJS::UString::UString): Ditto. (KJS::concatenate): Refactored the concatenation constructor into this separate function. Calling the concatenation constructor was leading to an extra branch because of the in-charge vs. not-in-charge versions not both being inlined, and this was showing up as nearly 1% on Shark. Also added a special case for when the second string is a single character, since it's a common idiom to build up a string that way and we can do things much more quickly, without involving memcpy for example. Also adopted the non-inline function, nullRep, for the same reason given for makeNull above. (KJS::UString::append): Adopted makeNull for failure cases. (KJS::UString::operator=): Ditto. (KJS::UString::toDouble): Added a special case for converting single character strings to numbers. We're doing this a ton of times while running the base64 test. (KJS::operator==): Added special cases so we can compare single-character strings without calling memcmp. Later we might want to special case other short lengths similarly. (KJS::UString::makeNull): Added. (KJS::UString::nullRep): Added.
  • kjs/ustring.h: Added declarations for the nullRep and makeNull. Changed expandedSize to be a static member function. Added a declaration of the concatenate function. Removed the concatenation constructor. Rewrote operator+ to use the concatenate function.

WebCore:

2008-08-30 Darin Adler <Darin Adler>

Reviewed by Maciej.

  • bindings/js/JSDOMWindowBase.cpp: (WebCore::windowProtoFuncAToB): Adopted jsEmptyString. (WebCore::windowProtoFuncBToA): Ditto.
  • bindings/js/JSEventListener.cpp: (WebCore::JSLazyEventListener::eventParameterName): Adopted jsNontrivialString.
  • bindings/js/JSSVGLazyEventListener.cpp: (WebCore::JSSVGLazyEventListener::eventParameterName): Ditto.

LayoutTests:

2008-08-30 Darin Adler <Darin Adler>

Reviewed by Maciej.

  • updated incorrect results that reflected a bug in the RegExp object
  • fast/js/regexp-caching-expected.txt: Updated results to correctly show that $1 through $9, lastMatch, lastParen, leftContext, and rightContext are left alone both when a program changes the value of RegExp.input and when it performs an unsuccessful match. The new results match Gecko behavior (I tested both Firefox 2 and 3).
  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1/*
2 * Copyright (C) 1999-2002 Harri Porten ([email protected])
3 * Copyright (C) 2001 Peter Kelly ([email protected])
4 * Copyright (C) 2004, 2007, 2008 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24#include "JSString.h"
25
26#include "JSGlobalObject.h"
27#include "JSObject.h"
28#include "StringObject.h"
29#include "StringPrototype.h"
30
31namespace KJS {
32
33JSValue* JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
34{
35 return const_cast<JSString*>(this);
36}
37
38bool JSString::getPrimitiveNumber(ExecState*, double& number, JSValue*& value)
39{
40 value = this;
41 number = m_value.toDouble();
42 return false;
43}
44
45bool JSString::toBoolean(ExecState*) const
46{
47 return !m_value.isEmpty();
48}
49
50double JSString::toNumber(ExecState*) const
51{
52 return m_value.toDouble();
53}
54
55UString JSString::toString(ExecState*) const
56{
57 return m_value;
58}
59
60UString JSString::toThisString(ExecState*) const
61{
62 return m_value;
63}
64
65JSString* JSString::toThisJSString(ExecState*)
66{
67 return this;
68}
69
70inline StringObject* StringObject::create(ExecState* exec, JSString* string)
71{
72 return new (exec) StringObject(exec->lexicalGlobalObject()->stringPrototype(), string);
73}
74
75JSObject* JSString::toObject(ExecState* exec) const
76{
77 return StringObject::create(exec, const_cast<JSString*>(this));
78}
79
80JSObject* JSString::toThisObject(ExecState* exec) const
81{
82 return StringObject::create(exec, const_cast<JSString*>(this));
83}
84
85bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
86{
87 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
88 // This function should only be called by JSValue::get.
89 if (getStringPropertySlot(exec, propertyName, slot))
90 return true;
91 slot.setBase(this);
92 JSObject* object;
93 for (JSValue* prototype = exec->lexicalGlobalObject()->stringPrototype(); prototype != jsNull(); prototype = object->prototype()) {
94 ASSERT(prototype->isObject());
95 object = static_cast<JSObject*>(prototype);
96 if (object->getOwnPropertySlot(exec, propertyName, slot))
97 return true;
98 }
99 slot.setUndefined();
100 return true;
101}
102
103bool JSString::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
104{
105 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
106 // This function should only be called by JSValue::get.
107 if (getStringPropertySlot(exec, propertyName, slot))
108 return true;
109 return JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
110}
111
112bool JSString::isString() const
113{
114 // FIXME: Change JSCell::isString to a non-virtual implementation like the one in Machine::isJSString.
115 return true;
116}
117
118JSString* jsString(ExecState* exec, const UString& s)
119{
120 int size = s.size();
121 if (!size)
122 return exec->globalData().smallStrings.emptyString(exec);
123 if (size == 1) {
124 UChar c = s.data()[0];
125 if (c <= 0xFF)
126 return exec->globalData().smallStrings.singleCharacterString(exec, c);
127 }
128 return new (exec) JSString(s);
129}
130
131JSString* jsSubstring(ExecState* exec, const UString& s, unsigned offset, unsigned length)
132{
133 ASSERT(offset <= static_cast<unsigned>(s.size()));
134 ASSERT(length <= static_cast<unsigned>(s.size()));
135 ASSERT(offset + length <= static_cast<unsigned>(s.size()));
136 if (!length)
137 return exec->globalData().smallStrings.emptyString(exec);
138 if (length == 1) {
139 UChar c = s.data()[offset];
140 if (c <= 0xFF)
141 return exec->globalData().smallStrings.singleCharacterString(exec, c);
142 }
143 return new (exec) JSString(UString::Rep::create(s.rep(), offset, length));
144}
145
146JSString* jsOwnedString(ExecState* exec, const UString& s)
147{
148 int size = s.size();
149 if (!size)
150 return exec->globalData().smallStrings.emptyString(exec);
151 if (size == 1) {
152 UChar c = s.data()[0];
153 if (c <= 0xFF)
154 return exec->globalData().smallStrings.singleCharacterString(exec, c);
155 }
156 return new (exec) JSString(s, JSString::HasOtherOwner);
157}
158
159} // namespace KJS
Note: See TracBrowser for help on using the repository browser.