source: webkit/trunk/JavaScriptCore/kjs/JSString.h@ 36693

Last change on this file since 36693 was 36368, checked in by [email protected], 17 years ago

2008-09-12 Cameron Zwarich <[email protected]>

Reviewed by Maciej Stachowiak.

Bug 20819: JSValue::isObject() is slow
<https://bugs.webkit.org/show_bug.cgi?id=20819>

Optimize JSCell::isObject() and JSCell::isString() by making them
non-virtual calls that rely on the StructureID type information.

This is a 0.7% speedup on SunSpider and a 1.0% speedup on the V8
benchmark suite.

  • JavaScriptCore.exp:
  • kjs/JSCell.cpp:
  • kjs/JSCell.h: (JSC::JSCell::isObject): (JSC::JSCell::isString):
  • kjs/JSObject.cpp:
  • kjs/JSObject.h:
  • kjs/JSString.cpp:
  • kjs/JSString.h: (JSC::JSString::JSString):
  • kjs/StructureID.h: (JSC::StructureID::type):
  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1/*
2 * Copyright (C) 1999-2001 Harri Porten ([email protected])
3 * Copyright (C) 2001 Peter Kelly ([email protected])
4 * Copyright (C) 2003, 2004, 2005, 2006, 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#ifndef JSString_h
24#define JSString_h
25
26#include "CommonIdentifiers.h"
27#include "ExecState.h"
28#include "JSNumberCell.h"
29#include "PropertySlot.h"
30#include "identifier.h"
31
32namespace JSC {
33
34 class JSString;
35
36 JSString* jsEmptyString(ExecState*);
37 JSString* jsString(ExecState*, const UString&); // returns empty string if passed null string
38
39 JSString* jsSingleCharacterString(ExecState*, UChar);
40 JSString* jsSingleCharacterSubstring(ExecState*, const UString&, unsigned offset);
41 JSString* jsSubstring(ExecState*, const UString&, unsigned offset, unsigned length);
42
43 // Non-trivial strings are two or more characters long.
44 // These functions are faster than just calling jsString.
45 JSString* jsNontrivialString(ExecState*, const UString&);
46 JSString* jsNontrivialString(ExecState*, const char*);
47
48 // Should be used for strings that are owned by an object that will
49 // likely outlive the JSValue this makes, such as the parse tree or a
50 // DOM object that contains a UString
51 JSString* jsOwnedString(ExecState*, const UString&);
52
53 class JSString : public JSCell {
54 friend class CTI;
55 friend class Machine;
56
57 public:
58 JSString(ExecState* exec, const UString& value)
59 : JSCell(exec->globalData().stringStructureID.get())
60 , m_value(value)
61 {
62 Heap::heap(this)->reportExtraMemoryCost(value.cost());
63 }
64
65 enum HasOtherOwnerType { HasOtherOwner };
66 JSString(ExecState* exec, const UString& value, HasOtherOwnerType)
67 : JSCell(exec->globalData().stringStructureID.get())
68 , m_value(value)
69 {
70 }
71 JSString(ExecState* exec, PassRefPtr<UString::Rep> value, HasOtherOwnerType)
72 : JSCell(exec->globalData().stringStructureID.get())
73 , m_value(value)
74 {
75 }
76
77 const UString& value() const { return m_value; }
78
79 bool getStringPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
80 bool getStringPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
81
82 bool canGetIndex(unsigned i) { return i < static_cast<unsigned>(m_value.size()); }
83 JSString* getIndex(ExecState*, unsigned);
84
85 private:
86 enum VPtrStealingHackType { VPtrStealingHack };
87 JSString(VPtrStealingHackType)
88 : JSCell(0)
89 {
90 }
91
92 virtual JSValue* toPrimitive(ExecState*, PreferredPrimitiveType) const;
93 virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value);
94 virtual bool toBoolean(ExecState*) const;
95 virtual double toNumber(ExecState*) const;
96 virtual JSObject* toObject(ExecState*) const;
97 virtual UString toString(ExecState*) const;
98
99 virtual JSObject* toThisObject(ExecState*) const;
100 virtual UString toThisString(ExecState*) const;
101 virtual JSString* toThisJSString(ExecState*);
102
103 // Actually getPropertySlot, not getOwnPropertySlot (see JSCell).
104 virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
105 virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
106
107 UString m_value;
108 };
109
110 inline JSString* jsEmptyString(ExecState* exec)
111 {
112 return exec->globalData().smallStrings.emptyString(exec);
113 }
114
115 inline JSString* jsSingleCharacterString(ExecState* exec, UChar c)
116 {
117 if (c <= 0xFF)
118 return exec->globalData().smallStrings.singleCharacterString(exec, c);
119 return new (exec) JSString(exec, UString(&c, 1));
120 }
121
122 inline JSString* jsSingleCharacterSubstring(ExecState* exec, const UString& s, unsigned offset)
123 {
124 ASSERT(offset < static_cast<unsigned>(s.size()));
125 UChar c = s.data()[offset];
126 if (c <= 0xFF)
127 return exec->globalData().smallStrings.singleCharacterString(exec, c);
128 return new (exec) JSString(exec, UString::Rep::create(s.rep(), offset, 1));
129 }
130
131 inline JSString* jsNontrivialString(ExecState* exec, const char* s)
132 {
133 ASSERT(s);
134 ASSERT(s[0]);
135 ASSERT(s[1]);
136 return new (exec) JSString(exec, s);
137 }
138
139 inline JSString* jsNontrivialString(ExecState* exec, const UString& s)
140 {
141 ASSERT(s.size() > 1);
142 return new (exec) JSString(exec, s);
143 }
144
145 inline JSString* JSString::getIndex(ExecState* exec, unsigned i)
146 {
147 ASSERT(canGetIndex(i));
148 return jsSingleCharacterSubstring(exec, m_value, i);
149 }
150
151 ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
152 {
153 if (propertyName == exec->propertyNames().length) {
154 slot.setValue(jsNumber(exec, m_value.size()));
155 return true;
156 }
157
158 bool isStrictUInt32;
159 unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);
160 if (isStrictUInt32 && i < static_cast<unsigned>(m_value.size())) {
161 slot.setValue(jsSingleCharacterSubstring(exec, m_value, i));
162 return true;
163 }
164
165 return false;
166 }
167
168 ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
169 {
170 if (propertyName < static_cast<unsigned>(m_value.size())) {
171 slot.setValue(jsSingleCharacterSubstring(exec, m_value, propertyName));
172 return true;
173 }
174
175 return false;
176 }
177
178 // --- JSValue inlines ----------------------------
179
180 inline JSString* JSValue::toThisJSString(ExecState* exec)
181 {
182 return JSImmediate::isImmediate(this) ? jsString(exec, JSImmediate::toString(this)) : asCell()->toThisJSString(exec);
183 }
184
185} // namespace JSC
186
187#endif // JSString_h
Note: See TracBrowser for help on using the repository browser.