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 | JSScopeProperty *).
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 { JS_ASSERT(isFunObj()); return *reinterpret_cast<JSObject
*>(v
); }
99 void setFunObj(JSObject
&obj
) { v
= reinterpret_cast<jsuword
>(&obj
); }
101 bool isSlot() const { return v
& SLOT
; }
102 uint32
toSlot() const { JS_ASSERT(isSlot()); return uint32(v
) >> 1; }
103 void setSlot(uint32 slot
) { v
= (jsuword(slot
) << 1) | SLOT
; }
105 bool isSprop() const { return (v
& TAG
) == SPROP
; }
106 JSScopeProperty
*toSprop() const { JS_ASSERT(isSprop()); return reinterpret_cast<JSScopeProperty
*>(v
& ~TAG
); }
107 void setSprop(JSScopeProperty
*sprop
) { JS_ASSERT(sprop
); v
= reinterpret_cast<jsuword
>(sprop
) | SPROP
; }
110 struct PropertyCacheEntry
112 jsbytecode
*kpc
; /* pc of cache-testing bytecode */
113 jsuword kshape
; /* shape of direct (key) object */
114 jsuword vcap
; /* value capability, see above */
115 PCVal vword
; /* value word, see PCVal above */
117 bool adding() const { return vcapTag() == 0 && kshape
!= vshape(); }
118 bool directHit() const { return vcapTag() == 0 && kshape
== vshape(); }
120 jsuword
vcapTag() const { return vcap
& PCVCAP_TAGMASK
; }
121 uint32
vshape() const { return uint32(vcap
>> PCVCAP_TAGBITS
); }
122 jsuword
scopeIndex() const { return (vcap
>> PCVCAP_PROTOBITS
) & PCVCAP_SCOPEMASK
; }
123 jsuword
protoIndex() const { return vcap
& PCVCAP_PROTOMASK
; }
125 void assign(jsbytecode
*kpc
, jsuword kshape
, jsuword vshape
,
126 uintN scopeIndex
, uintN protoIndex
, PCVal vword
) {
127 JS_ASSERT(kshape
< SHAPE_OVERFLOW_BIT
);
128 JS_ASSERT(vshape
< SHAPE_OVERFLOW_BIT
);
129 JS_ASSERT(scopeIndex
<= PCVCAP_SCOPEMASK
);
130 JS_ASSERT(protoIndex
<= PCVCAP_PROTOMASK
);
133 this->kshape
= kshape
;
134 this->vcap
= (vshape
<< PCVCAP_TAGBITS
) | (scopeIndex
<< PCVCAP_PROTOBITS
) | protoIndex
;
140 * Special value for functions returning PropertyCacheEntry * to distinguish
141 * between failure and no no-cache-fill cases.
143 #define JS_NO_PROP_CACHE_FILL ((js::PropertyCacheEntry *) NULL + 1)
145 #if defined DEBUG_brendan || defined DEBUG_brendaneich
146 #define JS_PROPERTY_CACHE_METERING 1
154 SIZE
= JS_BIT(SIZE_LOG2
),
155 MASK
= JS_BITMASK(SIZE_LOG2
)
158 PropertyCacheEntry table
[SIZE
];
160 #ifdef JS_PROPERTY_CACHE_METERING
162 PropertyCacheEntry
*pctestentry
; /* entry of the last PC-based test */
163 uint32 fills
; /* number of cache entry fills */
164 uint32 nofills
; /* couldn't fill (e.g. default get) */
165 uint32 rofills
; /* set on read-only prop can't fill */
166 uint32 disfills
; /* fill attempts on disabled cache */
167 uint32 oddfills
; /* fill attempt after setter deleted */
168 uint32 modfills
; /* fill that rehashed to a new entry */
169 uint32 brandfills
; /* scope brandings to type structural
171 uint32 noprotos
; /* resolve-returned non-proto pobj */
172 uint32 longchains
; /* overlong scope and/or proto chain */
173 uint32 recycles
; /* cache entries recycled by fills */
174 uint32 tests
; /* cache probes */
175 uint32 pchits
; /* fast-path polymorphic op hits */
176 uint32 protopchits
; /* pchits hitting immediate prototype */
177 uint32 initests
; /* cache probes from JSOP_INITPROP */
178 uint32 inipchits
; /* init'ing next property pchit case */
179 uint32 inipcmisses
; /* init'ing next property pc misses */
180 uint32 settests
; /* cache probes from JOF_SET opcodes */
181 uint32 addpchits
; /* adding next property pchit case */
182 uint32 setpchits
; /* setting existing property pchit */
183 uint32 setpcmisses
; /* setting/adding property pc misses */
184 uint32 setmisses
; /* JSOP_SET{NAME,PROP} total misses */
185 uint32 kpcmisses
; /* slow-path key id == atom misses */
186 uint32 kshapemisses
; /* slow-path key object misses */
187 uint32 vcapmisses
; /* value capability misses */
188 uint32 misses
; /* cache misses */
189 uint32 flushes
; /* cache flushes */
190 uint32 pcpurges
; /* shadowing purges on proto chain */
192 # define PCMETER(x) x
194 # define PCMETER(x) ((void)0)
198 * Add kshape rather than xor it to avoid collisions between nearby bytecode
199 * that are evolving an object by setting successive properties, incrementing
200 * the object's scope->shape on each set.
202 static inline jsuword
203 hash(jsbytecode
*pc
, jsuword kshape
)
205 return ((((jsuword(pc
) >> SIZE_LOG2
) ^ jsuword(pc
)) + kshape
) & MASK
);
208 static inline bool matchShape(JSContext
*cx
, JSObject
*obj
, uint32 shape
);
210 JS_REQUIRES_STACK JSAtom
*fullTest(JSContext
*cx
, jsbytecode
*pc
, JSObject
**objp
,
211 JSObject
**pobjp
, PropertyCacheEntry
*entry
);
216 inline void assertEmpty() {}
220 JS_ALWAYS_INLINE JS_REQUIRES_STACK
void test(JSContext
*cx
, jsbytecode
*pc
,
221 JSObject
*&obj
, JSObject
*&pobj
,
222 PropertyCacheEntry
*&entry
, JSAtom
*&atom
);
225 * Test for cached information about a property set on *objp at pc.
227 * On a fast hit, set *entryp to the entry and return true.
229 * On a slow hit, set *entryp to the entry, set *obj2p to the object that
230 * owns the property (either obj or a prototype), set *atomp to NULL, and
233 * On a miss, set *atomp to the name of the property being set and return false.
235 JS_ALWAYS_INLINE
bool testForSet(JSContext
*cx
, jsbytecode
*pc
, JSObject
*obj
,
236 PropertyCacheEntry
**entryp
, JSObject
**obj2p
,
240 * Test for cached information about creating a new own data property on obj at pc.
242 * On a hit, set *spropp to an sprop from the property tree describing the
243 * new property as well as all existing properties on obj and return
244 * true. Otherwise return false.
246 * Hit or miss, *entryp receives a pointer to the property cache entry.
248 JS_ALWAYS_INLINE
bool testForInit(JSRuntime
*rt
, jsbytecode
*pc
, JSObject
*obj
, JSScope
*scope
,
249 JSScopeProperty
**spropp
, PropertyCacheEntry
**entryp
);
252 * Fill property cache entry for key cx->fp->pc, optimized value word
253 * computed from obj and sprop, and entry capability forged from 24-bit
254 * obj->shape(), 4-bit scopeIndex, and 4-bit protoIndex.
256 * Return the filled cache entry or JS_NO_PROP_CACHE_FILL if caching was
259 JS_REQUIRES_STACK PropertyCacheEntry
*fill(JSContext
*cx
, JSObject
*obj
, uintN scopeIndex
,
260 uintN protoIndex
, JSObject
*pobj
,
261 JSScopeProperty
*sprop
, JSBool adding
= false);
263 void purge(JSContext
*cx
);
264 void purgeForScript(JSScript
*script
);
269 #endif /* jspropertycache_h___ */