← All reports

Private field get ICs fold to megamorphic loads

Component: JSC JIT | 567d5ec

Source/JavaScriptCore/jit/AssemblyHelpers.cpp

+ AssemblyHelpers::loadMegamorphicPrivateProperty(...)
+ // loadMegamorphicProperty와 findMegamorphicCacheEntry를 공유
+ // own-property hit인 경우에만 entry를 수용

Source/JavaScriptCore/bytecode/InlineCacheCompiler.cpp

+ tryFoldToMegamorphic: now handles GetPrivateName / GetPrivateNameById
(folds to IndexedMegamorphicLoad / LoadMegamorphic)

Source/JavaScriptCore/jit/JITOperations.cpp

+ getPrivateNameMegamorphic: slow path does not discard IC for uncacheable receivers (ProxyObject, uncacheable dictionary)

JSC의 inline cache는 Structure(JSC의 hidden class) 단위로 property access를 특화합니다. 이때 유지되는 variant는 최대 maxAccessVariantListSize개입니다. 이 한도를 넘어서면 tryFoldToMegamorphic가 IC를 global MegamorphicCache 기반의 generic lookup으로 대체하는데, 이 cache는 structure와 property 이름을 key로 사용합니다. Private field(#x) 역시 private Symbol을 key로 하는 own property로 저장됩니다. 다만 존재하지 않는 private field를 읽으면 undefined를 반환하지 않고 TypeError를 던집니다. 이 차이 때문에 지금까지 private name get은 folding 대상에서 제외되어 왔습니다.

이번 commit으로 GetPrivateName/GetPrivateNameById IC도 variant list가 가득 차면 IndexedMegamorphicLoad/LoadMegamorphic로 fold됩니다. 여기에는 새로 추가된 loadMegamorphicPrivateProperty가 사용되었습니다. 일반 load와 findMegamorphicCacheEntry를 공유하지만, own property hit만 받아들인다는 점이 다릅니다. 존재하지 않는 field는 항상 slow path로 처리되므로 throw 동작은 그대로 유지됩니다. 한편 Proxy나 uncacheable dictionary 같은 uncacheable receiver를 만나더라도 더 이상 IC가 폐기되지 않도록 변경되었습니다.

Base class method에서 여러 subclass shape에 걸쳐 #field를 읽는 코드가 더 이상 영구적인 give-up slow path에 머무르지 않으며, megamorphic microbenchmark는 약 2.35배 빨라졌습니다(182ms → 78ms). 동시에 직접 작성된 megamorphic JIT codegen이 miss semantics가 다른 새로운 access 종류까지 다루게 된 셈입니다.

이번 변경에서 실질적인 방어선은 loadMegamorphicPrivateProperty의 own-property hit 검사입니다. private symbol에는 hit했지만 own property가 아닌 경우, 또는 offset이 stale한 경우를 가정할 수 있습니다. 이 경우 type confusion이나 out-of-bounds load로 이어질 가능성이 있습니다.

먼저 dictionary structure가 flatten되거나 property가 삭제될 때 cache entry가 올바르게 무효화되는지 확인해야 합니다. 실제로 dictionary stress test에는 flattening 시 private field offset이 바뀐다는 점이 명시되어 있습니다. Proxy를 비롯한 exotic receiver도 시험해 볼 필요가 있습니다. 또한 different-symbols test처럼, 이름은 같지만 identity가 다른 private symbol을 가진 structure도 대상에 포함하는 것이 좋습니다.

이어서 존재하지 않는 field에 대해 항상 throw가 발생하는지, negative hit이나 prototype chain hit이 cache에 저장되는 경로는 없는지 점검해야 합니다. 마지막으로, uncacheable receiver에 대해 IC를 유지하도록 바뀐 동작이 repatching 상태와 어떻게 맞물리는지 살펴볼 필요가 있습니다.