1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=98:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is Mozilla Communicator client code, released
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef jspropertycache_h___
42 #define jspropertycache_h___
51 * Property cache with structurally typed capabilities for invalidation, for
52 * polymorphic callsite method/get/set speedups. For details, see
53 * <https://developer.mozilla.org/en/SpiderMonkey/Internals/Property_cache>.
56 /* Property cache value capabilities. */
59 PCVCAP_PROTOSIZE
= JS_BIT(PCVCAP_PROTOBITS
),
60 PCVCAP_PROTOMASK
= JS_BITMASK(PCVCAP_PROTOBITS
),
63 PCVCAP_SCOPESIZE
= JS_BIT(PCVCAP_SCOPEBITS
),
64 PCVCAP_SCOPEMASK
= JS_BITMASK(PCVCAP_SCOPEBITS
),
66 PCVCAP_TAGBITS
= PCVCAP_PROTOBITS
+ PCVCAP_SCOPEBITS
,
67 PCVCAP_TAGMASK
= JS_BITMASK(PCVCAP_TAGBITS
)
70 const uint32 SHAPE_OVERFLOW_BIT
= JS_BIT(32 - PCVCAP_TAGBITS
);
73 * Property cache value. This is simply a tagged union:
74 * PCVal = (JSObject * | uint32 | js::Shape *).
75 * It is the type of PropertyCacheEntry::vword and combines with the tag bits
76 * of PropertyCacheEntry::vcap to tell how to get or set the property, once a
77 * property cache hit is validated.
79 * PropertyCache::purge depends on the bit-pattern of a null PCVal being 0.
94 bool isNull() const { return v
== 0; }
95 void setNull() { v
= 0; }
97 bool isFunObj() const { return (v
& TAG
) == OBJECT
; }
98 JSObject
&toFunObj() const {
99 JS_ASSERT(isFunObj());
100 return *reinterpret_cast<JSObject
*>(v
);
102 void setFunObj(JSObject
&obj
) {
103 v
= reinterpret_cast<jsuword
>(&obj
);
106 bool isSlot() const { return v
& SLOT
; }
107 uint32
toSlot() const { JS_ASSERT(isSlot()); return uint32(v
) >> 1; }
108 void setSlot(uint32 slot
) { v
= (jsuword(slot
) << 1) | SLOT
; }
110 bool isShape() const { return (v
& TAG
) == SHAPE
; }
111 const js::Shape
*toShape() const {
112 JS_ASSERT(isShape());
113 return reinterpret_cast<js::Shape
*>(v
& ~TAG
);
115 void setShape(const js::Shape
*shape
) {
117 v
= reinterpret_cast<jsuword
>(shape
) | SHAPE
;
121 struct PropertyCacheEntry
123 jsbytecode
*kpc
; /* pc of cache-testing bytecode */
124 jsuword kshape
; /* shape of direct (key) object */
125 jsuword vcap
; /* value capability, see above */
126 PCVal vword
; /* value word, see PCVal above */
128 bool adding() const { return vcapTag() == 0 && kshape
!= vshape(); }
129 bool directHit() const { return vcapTag() == 0 && kshape
== vshape(); }
131 jsuword
vcapTag() const { return vcap
& PCVCAP_TAGMASK
; }
132 uint32
vshape() const { return uint32(vcap
>> PCVCAP_TAGBITS
); }
133 jsuword
scopeIndex() const { return (vcap
>> PCVCAP_PROTOBITS
) & PCVCAP_SCOPEMASK
; }
134 jsuword
protoIndex() const { return vcap
& PCVCAP_PROTOMASK
; }
136 void assign(jsbytecode
*kpc
, jsuword kshape
, jsuword vshape
,
137 uintN scopeIndex
, uintN protoIndex
, PCVal vword
) {
138 JS_ASSERT(kshape
< SHAPE_OVERFLOW_BIT
);
139 JS_ASSERT(vshape
< SHAPE_OVERFLOW_BIT
);
140 JS_ASSERT(scopeIndex
<= PCVCAP_SCOPEMASK
);
141 JS_ASSERT(protoIndex
<= PCVCAP_PROTOMASK
);
144 this->kshape
= kshape
;
145 this->vcap
= (vshape
<< PCVCAP_TAGBITS
) | (scopeIndex
<< PCVCAP_PROTOBITS
) | protoIndex
;
151 * Special value for functions returning PropertyCacheEntry * to distinguish
152 * between failure and no no-cache-fill cases.
154 #define JS_NO_PROP_CACHE_FILL ((js::PropertyCacheEntry *) NULL + 1)
156 #if defined DEBUG_brendan || defined DEBUG_brendaneich
157 #define JS_PROPERTY_CACHE_METERING 1
165 SIZE
= JS_BIT(SIZE_LOG2
),
166 MASK
= JS_BITMASK(SIZE_LOG2
)
169 PropertyCacheEntry table
[SIZE
];
171 #ifdef JS_PROPERTY_CACHE_METERING
173 PropertyCacheEntry
*pctestentry
; /* entry of the last PC-based test */
174 uint32 fills
; /* number of cache entry fills */
175 uint32 nofills
; /* couldn't fill (e.g. default get) */
176 uint32 rofills
; /* set on read-only prop can't fill */
177 uint32 disfills
; /* fill attempts on disabled cache */
178 uint32 oddfills
; /* fill attempt after setter deleted */
179 uint32 add2dictfills
; /* fill attempt on dictionary object */
180 uint32 modfills
; /* fill that rehashed to a new entry */
181 uint32 brandfills
; /* scope brandings to type structural
183 uint32 noprotos
; /* resolve-returned non-proto pobj */
184 uint32 longchains
; /* overlong scope and/or proto chain */
185 uint32 recycles
; /* cache entries recycled by fills */
186 uint32 tests
; /* cache probes */
187 uint32 pchits
; /* fast-path polymorphic op hits */
188 uint32 protopchits
; /* pchits hitting immediate prototype */
189 uint32 initests
; /* cache probes from JSOP_INITPROP */
190 uint32 inipchits
; /* init'ing next property pchit case */
191 uint32 inipcmisses
; /* init'ing next property pc misses */
192 uint32 settests
; /* cache probes from JOF_SET opcodes */
193 uint32 addpchits
; /* adding next property pchit case */
194 uint32 setpchits
; /* setting existing property pchit */
195 uint32 setpcmisses
; /* setting/adding property pc misses */
196 uint32 setmisses
; /* JSOP_SET{NAME,PROP} total misses */
197 uint32 kpcmisses
; /* slow-path key id == atom misses */
198 uint32 kshapemisses
; /* slow-path key object misses */
199 uint32 vcapmisses
; /* value capability misses */
200 uint32 misses
; /* cache misses */
201 uint32 flushes
; /* cache flushes */
202 uint32 pcpurges
; /* shadowing purges on proto chain */
204 # define PCMETER(x) x
206 # define PCMETER(x) ((void)0)
210 * Add kshape rather than xor it to avoid collisions between nearby bytecode
211 * that are evolving an object by setting successive properties, incrementing
212 * the object's shape on each set.
214 static inline jsuword
215 hash(jsbytecode
*pc
, jsuword kshape
)
217 return ((((jsuword(pc
) >> SIZE_LOG2
) ^ jsuword(pc
)) + kshape
) & MASK
);
220 static inline bool matchShape(JSContext
*cx
, JSObject
*obj
, uint32 shape
);
222 JS_REQUIRES_STACK JSAtom
*fullTest(JSContext
*cx
, jsbytecode
*pc
, JSObject
**objp
,
223 JSObject
**pobjp
, PropertyCacheEntry
*entry
);
228 inline void assertEmpty() {}
232 JS_ALWAYS_INLINE JS_REQUIRES_STACK
void test(JSContext
*cx
, jsbytecode
*pc
,
233 JSObject
*&obj
, JSObject
*&pobj
,
234 PropertyCacheEntry
*&entry
, JSAtom
*&atom
);
237 * Test for cached information about a property set on *objp at pc.
239 * On a hit, set *entryp to the entry and return true.
241 * On a miss, set *atomp to the name of the property being set and return false.
243 JS_ALWAYS_INLINE
bool testForSet(JSContext
*cx
, jsbytecode
*pc
, JSObject
*obj
,
244 PropertyCacheEntry
**entryp
, JSObject
**obj2p
,
248 * Test for cached information about creating a new own data property on obj at pc.
250 * On a hit, set *shapep to an shape from the property tree describing the
251 * new property as well as all existing properties on obj and return
252 * true. Otherwise return false.
254 * Hit or miss, *entryp receives a pointer to the property cache entry.
256 JS_ALWAYS_INLINE
bool testForInit(JSRuntime
*rt
, jsbytecode
*pc
, JSObject
*obj
,
257 const js::Shape
**shapep
, PropertyCacheEntry
**entryp
);
260 * Fill property cache entry for key cx->fp->pc, optimized value word
261 * computed from obj and shape, and entry capability forged from 24-bit
262 * obj->shape(), 4-bit scopeIndex, and 4-bit protoIndex.
264 * Return the filled cache entry or JS_NO_PROP_CACHE_FILL if caching was
267 JS_REQUIRES_STACK PropertyCacheEntry
*fill(JSContext
*cx
, JSObject
*obj
, uintN scopeIndex
,
268 uintN protoIndex
, JSObject
*pobj
,
269 const js::Shape
*shape
, JSBool adding
= false);
271 void purge(JSContext
*cx
);
272 void purgeForScript(JSScript
*script
);
277 #endif /* jspropertycache_h___ */