Changeset 243617 in webkit
- Timestamp:
- Mar 28, 2019, 12:37:58 PM (7 years ago)
- Location:
- trunk/Source/JavaScriptCore
- Files:
-
- 6 edited
-
API/JSContext.mm (modified) (5 diffs)
-
API/JSVirtualMachine.mm (modified) (7 diffs)
-
API/JSVirtualMachineInternal.h (modified) (1 diff)
-
ChangeLog (modified) (1 diff)
-
runtime/JSGlobalObject.h (modified) (2 diffs)
-
runtime/VM.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/API/JSContext.mm
r242980 r243617 86 86 87 87 [self ensureWrapperMap]; 88 [m_virtualMachine addContext:self forGlobalContextRef:m_context]; 88 89 toJSGlobalObject(m_context)->setAPIWrapper((__bridge void*)self); 89 90 90 91 return self; … … 93 94 - (void)dealloc 94 95 { 96 toJSGlobalObject(m_context)->setAPIWrapper((__bridge void*)nil); 95 97 m_exception.clear(); 96 98 JSGlobalContextRelease(m_context); … … 309 311 }; 310 312 311 [m_virtualMachine addContext:self forGlobalContextRef:m_context];313 toJSGlobalObject(m_context)->setAPIWrapper((__bridge void*)self); 312 314 313 315 return self; … … 359 361 - (JSWrapperMap *)wrapperMap 360 362 { 361 return toJS (m_context)->lexicalGlobalObject()->wrapperMap();363 return toJSGlobalObject(m_context)->wrapperMap(); 362 364 } 363 365 … … 370 372 + (JSContext *)contextWithJSGlobalContextRef:(JSGlobalContextRef)globalContext 371 373 { 372 JSVirtualMachine *virtualMachine = [JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&toJS(globalContext)->vm())]; 373 JSContext *context = [virtualMachine contextForGlobalContextRef:globalContext]; 374 JSContext *context = (__bridge JSContext *)toJSGlobalObject(globalContext)->apiWrapper(); 374 375 if (!context) 375 376 context = [[[JSContext alloc] initWithGlobalContextRef:globalContext] autorelease]; -
trunk/Source/JavaScriptCore/API/JSVirtualMachine.mm
r242301 r243617 42 42 #import <wtf/Lock.h> 43 43 44 static NSMapTable *globalWrapperCache = 0;45 46 static Lock wrapperCacheMutex;47 48 static void initWrapperCache()49 {50 ASSERT(!globalWrapperCache);51 NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality;52 NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;53 globalWrapperCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0];54 }55 56 static NSMapTable *wrapperCache()57 {58 if (!globalWrapperCache)59 initWrapperCache();60 return globalWrapperCache;61 }62 63 @interface JSVMWrapperCache : NSObject64 + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group;65 + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group;66 @end67 68 @implementation JSVMWrapperCache69 70 + (void)addWrapper:(JSVirtualMachine *)wrapper forJSContextGroupRef:(JSContextGroupRef)group71 {72 std::lock_guard<Lock> lock(wrapperCacheMutex);73 NSMapInsert(wrapperCache(), group, (__bridge void*)wrapper);74 }75 76 + (JSVirtualMachine *)wrapperForJSContextGroupRef:(JSContextGroupRef)group77 {78 std::lock_guard<Lock> lock(wrapperCacheMutex);79 return (__bridge JSVirtualMachine *)NSMapGet(wrapperCache(), group);80 }81 82 @end83 84 44 @implementation JSVirtualMachine { 85 45 JSContextGroupRef m_group; 86 46 Lock m_externalDataMutex; 87 NSMapTable *m_contextCache;88 47 NSMapTable *m_externalObjectGraph; 89 48 NSMapTable *m_externalRememberedSet; … … 107 66 m_group = JSContextGroupRetain(group); 108 67 109 NSPointerFunctionsOptions keyOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsOpaquePersonality;110 NSPointerFunctionsOptions valueOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality;111 m_contextCache = [[NSMapTable alloc] initWithKeyOptions:keyOptions valueOptions:valueOptions capacity:0];112 113 68 NSPointerFunctionsOptions weakIDOptions = NSPointerFunctionsWeakMemory | NSPointerFunctionsObjectPersonality; 114 69 NSPointerFunctionsOptions strongIDOptions = NSPointerFunctionsStrongMemory | NSPointerFunctionsObjectPersonality; … … 117 72 NSPointerFunctionsOptions integerOptions = NSPointerFunctionsOpaqueMemory | NSPointerFunctionsIntegerPersonality; 118 73 m_externalRememberedSet = [[NSMapTable alloc] initWithKeyOptions:weakIDOptions valueOptions:integerOptions capacity:0]; 119 120 [JSVMWrapperCache addWrapper:self forJSContextGroupRef:group];74 75 toJS(group)->m_apiWrapper = (__bridge void*)self; 121 76 122 77 return self; … … 125 80 - (void)dealloc 126 81 { 82 toJS(m_group)->m_apiWrapper = (__bridge void*)nil; 127 83 JSContextGroupRelease(m_group); 128 [m_contextCache release];129 84 [m_externalObjectGraph release]; 130 85 [m_externalRememberedSet release]; … … 238 193 + (JSVirtualMachine *)virtualMachineWithContextGroupRef:(JSContextGroupRef)group 239 194 { 240 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:group]; 195 auto* vm = toJS(group); 196 JSVirtualMachine *virtualMachine = (__bridge JSVirtualMachine *)vm->m_apiWrapper; 241 197 if (!virtualMachine) 242 198 virtualMachine = [[[JSVirtualMachine alloc] initWithContextGroupRef:group] autorelease]; 243 199 return virtualMachine; 244 }245 246 - (JSContext *)contextForGlobalContextRef:(JSGlobalContextRef)globalContext247 {248 return (__bridge JSContext *)NSMapGet(m_contextCache, globalContext);249 }250 251 - (void)addContext:(JSContext *)wrapper forGlobalContextRef:(JSGlobalContextRef)globalContext252 {253 NSMapInsert(m_contextCache, globalContext, (__bridge void*)wrapper);254 200 } 255 201 … … 313 259 { 314 260 @autoreleasepool { 315 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:toRef(&vm)];261 JSVirtualMachine *virtualMachine = (__bridge JSVirtualMachine *)vm.m_apiWrapper; 316 262 if (!virtualMachine) 317 263 return; … … 351 297 { 352 298 @autoreleasepool { 353 JSVirtualMachine *virtualMachine = [JSVMWrapperCache wrapperForJSContextGroupRef:toRef(&vm)];299 JSVirtualMachine *virtualMachine = (__bridge JSVirtualMachine *)vm.m_apiWrapper; 354 300 if (!virtualMachine) 355 301 return; -
trunk/Source/JavaScriptCore/API/JSVirtualMachineInternal.h
r240511 r243617 45 45 + (JSVirtualMachine *)virtualMachineWithContextGroupRef:(JSContextGroupRef)group; 46 46 47 - (JSContext *)contextForGlobalContextRef:(JSGlobalContextRef)globalContext;48 - (void)addContext:(JSContext *)wrapper forGlobalContextRef:(JSGlobalContextRef)globalContext;49 47 - (JSC::VM&)vm; 50 48 -
trunk/Source/JavaScriptCore/ChangeLog
r243609 r243617 1 2019-03-27 Yusuke Suzuki <ysuzuki@apple.com> 2 3 [JSC] Drop VM and Context cache map in JavaScriptCore.framework 4 https://bugs.webkit.org/show_bug.cgi?id=196341 5 6 Reviewed by Saam Barati. 7 8 Previously, we created Objective-C weak map to maintain JSVirtualMachine and JSContext wrappers corresponding to VM and JSGlobalObject. 9 But Objective-C weak map is really memory costly. Even if the entry is only one, it consumes 2.5KB per weak map. Since we can modify 10 JSC intrusively for JavaScriptCore.framework (and we already did it, like, holding JSWrapperMap in JSGlobalObject), we can just hold 11 a pointer to a wrapper in VM and JSGlobalObject. 12 13 This patch adds void* members to VM and JSGlobalObject, which holds a non-strong reference to a wrapper. When a wrapper is gone, we 14 clear this pointer too. This removes unnecessary two Objective-C weak maps, and save 5KB. 15 16 * API/JSContext.mm: 17 (-[JSContext initWithVirtualMachine:]): 18 (-[JSContext dealloc]): 19 (-[JSContext initWithGlobalContextRef:]): 20 (-[JSContext wrapperMap]): 21 (+[JSContext contextWithJSGlobalContextRef:]): 22 * API/JSVirtualMachine.mm: 23 (-[JSVirtualMachine initWithContextGroupRef:]): 24 (-[JSVirtualMachine dealloc]): 25 (+[JSVirtualMachine virtualMachineWithContextGroupRef:]): 26 (scanExternalObjectGraph): 27 (scanExternalRememberedSet): 28 (initWrapperCache): Deleted. 29 (wrapperCache): Deleted. 30 (+[JSVMWrapperCache addWrapper:forJSContextGroupRef:]): Deleted. 31 (+[JSVMWrapperCache wrapperForJSContextGroupRef:]): Deleted. 32 (-[JSVirtualMachine contextForGlobalContextRef:]): Deleted. 33 (-[JSVirtualMachine addContext:forGlobalContextRef:]): Deleted. 34 * API/JSVirtualMachineInternal.h: 35 * runtime/JSGlobalObject.h: 36 (JSC::JSGlobalObject::setAPIWrapper): 37 (JSC::JSGlobalObject::apiWrapper const): 38 * runtime/VM.h: 39 1 40 2019-03-28 Tadeu Zagallo <tzagallo@apple.com> 2 41 -
trunk/Source/JavaScriptCore/runtime/JSGlobalObject.h
r243051 r243617 1006 1006 JSWrapperMap* wrapperMap() const { return m_wrapperMap.get(); } 1007 1007 void setWrapperMap(JSWrapperMap* map) { m_wrapperMap = map; } 1008 void setAPIWrapper(void* apiWrapper) { m_apiWrapper = apiWrapper; } 1009 void* apiWrapper() const { return m_apiWrapper; } 1008 1010 #endif 1009 1011 #ifdef JSC_GLIB_API_ENABLED … … 1048 1050 #if JSC_OBJC_API_ENABLED 1049 1051 RetainPtr<JSWrapperMap> m_wrapperMap; 1052 void* m_apiWrapper { nullptr }; 1050 1053 #endif 1051 1054 #ifdef JSC_GLIB_API_ENABLED -
trunk/Source/JavaScriptCore/runtime/VM.h
r243312 r243617 805 805 #endif 806 806 807 #if JSC_OBJC_API_ENABLED 808 void* m_apiWrapper { nullptr }; 809 #endif 810 807 811 JS_EXPORT_PRIVATE void resetDateCache(); 808 812
Note:
See TracChangeset
for help on using the changeset viewer.