Bug 216061

Summary: [JSC] Cache toString / valueOf / @@toPrimitive for major cases
Product: WebKit Reporter: Yusuke Suzuki <ysuzuki>
Component: JavaScriptCoreAssignee: Yusuke Suzuki <ysuzuki>
Status: RESOLVED FIXED    
Severity: Normal CC: ashvayka, ews-watchlist, keith_miller, mark.lam, msaboff, saam, tzagallo, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
See Also: https://bugs.webkit.org/show_bug.cgi?id=216262
Bug Depends on:    
Bug Blocks: 216222    
Attachments:
Description Flags
Patch
none
Patch
none
Patch
none
Patch
saam: review+
Patch
none
Patch none

Description Yusuke Suzuki 2020-09-01 18:34:41 PDT
...
Comment 1 Radar WebKit Bug Importer 2020-09-01 18:34:57 PDT
<rdar://problem/68179682>
Comment 2 Yusuke Suzuki 2020-09-01 23:48:38 PDT
Created attachment 407743 [details]
Patch
Comment 3 Yusuke Suzuki 2020-09-02 00:28:49 PDT
Created attachment 407746 [details]
Patch
Comment 4 Yusuke Suzuki 2020-09-02 00:38:46 PDT
Created attachment 407747 [details]
Patch
Comment 5 Yusuke Suzuki 2020-09-02 14:16:53 PDT
Created attachment 407811 [details]
Patch
Comment 6 Yusuke Suzuki 2020-09-02 14:33:24 PDT
Comment on attachment 407811 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=407811&action=review

> Source/JavaScriptCore/bytecode/Watchpoint.h:117
> +    macro(ObjectToStringAdaptiveStructure, CachedSpecialPropertyAdaptiveStructureWatchpoint)

Will change.
Comment 7 Saam Barati 2020-09-02 15:14:01 PDT
Comment on attachment 407811 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=407811&action=review

Nice. r=me

> Source/JavaScriptCore/runtime/CachedSpecialPropertyAdaptiveStructureWatchpoint.h:2
> + * Copyright (C) 2019 Apple Inc. All rights reserved.

2020

> Source/JavaScriptCore/runtime/StructureRareData.cpp:112
> +    WTF::storeStoreFence(); // Store to the field of this struct should be visible to the concurrent collectors after struct is visible.

from what I said on slack, I don't think this is needed, but double check

> Source/JavaScriptCore/runtime/StructureRareData.cpp:184
> +            cache.m_presentWatchpoint = makeUnique<CachedSpecialPropertyAdaptiveInferredPropertyValueWatchpoint>(equivCondition, this);

let's call "m_presentWatchpoint" m_equivalenceWatchpoint or something similar

> Source/JavaScriptCore/runtime/StructureRareData.cpp:219
> +                    continue;

continue isn't needed. Did you mean break so we continue the outer loop?

> Source/JavaScriptCore/runtime/StructureRareDataInlines.h:39
> +    Bag<CachedSpecialPropertyAdaptiveStructureWatchpoint> m_missWatchpoints;
> +    std::unique_ptr<CachedSpecialPropertyAdaptiveInferredPropertyValueWatchpoint> m_presentWatchpoint;

maybe in the future we can use ObjectPropertyConditionSet? Or is this just to save memory?
Comment 8 Saam Barati 2020-09-02 15:14:46 PDT
Comment on attachment 407811 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=407811&action=review

>> Source/JavaScriptCore/runtime/CachedSpecialPropertyAdaptiveStructureWatchpoint.h:2
>> + * Copyright (C) 2019 Apple Inc. All rights reserved.
> 
> 2020

I see you renamed this. Maybe 2019-2020
Comment 9 Yusuke Suzuki 2020-09-02 18:04:30 PDT
Comment on attachment 407811 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=407811&action=review

Thanks!

>> Source/JavaScriptCore/bytecode/Watchpoint.h:117
>> +    macro(ObjectToStringAdaptiveStructure, CachedSpecialPropertyAdaptiveStructureWatchpoint)
> 
> Will change.

Fixed.

>>> Source/JavaScriptCore/runtime/CachedSpecialPropertyAdaptiveStructureWatchpoint.h:2
>>> + * Copyright (C) 2019 Apple Inc. All rights reserved.
>> 
>> 2020
> 
> I see you renamed this. Maybe 2019-2020

Fixed.

>> Source/JavaScriptCore/runtime/StructureRareData.cpp:112
>> +    WTF::storeStoreFence(); // Store to the field of this struct should be visible to the concurrent collectors after struct is visible.
> 
> from what I said on slack, I don't think this is needed, but double check

Yeah, agree. Removed.

>> Source/JavaScriptCore/runtime/StructureRareData.cpp:184
>> +            cache.m_presentWatchpoint = makeUnique<CachedSpecialPropertyAdaptiveInferredPropertyValueWatchpoint>(equivCondition, this);
> 
> let's call "m_presentWatchpoint" m_equivalenceWatchpoint or something similar

Fixed.

>> Source/JavaScriptCore/runtime/StructureRareData.cpp:219
>> +                    continue;
> 
> continue isn't needed. Did you mean break so we continue the outer loop?

Oops! Right. To simply, I'll extract this as clearCacheIfInvalidated lambda, and return instead of continue.

>> Source/JavaScriptCore/runtime/StructureRareDataInlines.h:39
>> +    std::unique_ptr<CachedSpecialPropertyAdaptiveInferredPropertyValueWatchpoint> m_presentWatchpoint;
> 
> maybe in the future we can use ObjectPropertyConditionSet? Or is this just to save memory?

Sounds good. I'll file a bug and paste a FIXME here.
Comment 10 Yusuke Suzuki 2020-09-02 18:26:30 PDT
Created attachment 407845 [details]
Patch

Patch for landing
Comment 11 Yusuke Suzuki 2020-09-02 21:27:41 PDT
Comment on attachment 407845 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=407845&action=review

> Source/JavaScriptCore/runtime/JSObject.cpp:-2208
> -    // Make sure that whatever default value methods there are on object's prototype chain are
> -    // being watched.
> -    for (const JSObject* object = this; object; object = object->structure(vm)->storedPrototypeObject(object))
> -        object->structure(vm)->startWatchingInternalPropertiesIfNecessary(vm);
> -

I revert this for now. The reason is the following,

1. DFG Graph::canOptimizeStringObjectAccess is checking both valueOf and toString status
2. However, in the normal path, valueOf of StringObject is rarely accessed (toString suceeeds, and we do not access to valueOf).
3. As a result, valueOf related structures are not watched at all. So in DFG, DFG Graph::canOptimizeStringObjectAccess failed.

This affects on JetStream2/date-format-xparb-SP. For now, I'll keep this.
Comment 12 Yusuke Suzuki 2020-09-02 21:34:46 PDT
Comment on attachment 407845 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=407845&action=review

>> Source/JavaScriptCore/runtime/JSObject.cpp:-2208
>> -
> 
> I revert this for now. The reason is the following,
> 
> 1. DFG Graph::canOptimizeStringObjectAccess is checking both valueOf and toString status
> 2. However, in the normal path, valueOf of StringObject is rarely accessed (toString suceeeds, and we do not access to valueOf).
> 3. As a result, valueOf related structures are not watched at all. So in DFG, DFG Graph::canOptimizeStringObjectAccess failed.
> 
> This affects on JetStream2/date-format-xparb-SP. For now, I'll keep this.

Ah, no. Yeah, since this is not invoked from IC, basically, without this, we do not put watchpoints. So, as a result, PropertyCondition::isWatchableWhenValid for Equavalent will fail.
Comment 13 Yusuke Suzuki 2020-09-02 21:37:30 PDT
Comment on attachment 407845 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=407845&action=review

>>> Source/JavaScriptCore/runtime/JSObject.cpp:-2208
>>> -
>> 
>> I revert this for now. The reason is the following,
>> 
>> 1. DFG Graph::canOptimizeStringObjectAccess is checking both valueOf and toString status
>> 2. However, in the normal path, valueOf of StringObject is rarely accessed (toString suceeeds, and we do not access to valueOf).
>> 3. As a result, valueOf related structures are not watched at all. So in DFG, DFG Graph::canOptimizeStringObjectAccess failed.
>> 
>> This affects on JetStream2/date-format-xparb-SP. For now, I'll keep this.
> 
> Ah, no. Yeah, since this is not invoked from IC, basically, without this, we do not put watchpoints. So, as a result, PropertyCondition::isWatchableWhenValid for Equavalent will fail.

And in this path, we will put this watchpoint for toString, but not for valueOf since we are not accessing it at runtime actually. Then, Graph::canOptimizeStringObjectAccess almost always fail.
We should do in the meantime.
Comment 14 Yusuke Suzuki 2020-09-02 21:39:09 PDT
Comment on attachment 407845 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=407845&action=review

>>>> Source/JavaScriptCore/runtime/JSObject.cpp:-2208
>>>> -
>>> 
>>> I revert this for now. The reason is the following,
>>> 
>>> 1. DFG Graph::canOptimizeStringObjectAccess is checking both valueOf and toString status
>>> 2. However, in the normal path, valueOf of StringObject is rarely accessed (toString suceeeds, and we do not access to valueOf).
>>> 3. As a result, valueOf related structures are not watched at all. So in DFG, DFG Graph::canOptimizeStringObjectAccess failed.
>>> 
>>> This affects on JetStream2/date-format-xparb-SP. For now, I'll keep this.
>> 
>> Ah, no. Yeah, since this is not invoked from IC, basically, without this, we do not put watchpoints. So, as a result, PropertyCondition::isWatchableWhenValid for Equavalent will fail.
> 
> And in this path, we will put this watchpoint for toString, but not for valueOf since we are not accessing it at runtime actually. Then, Graph::canOptimizeStringObjectAccess almost always fail.
> We should do in the meantime.

Ideally, DFG should not check toString & valueOf. DFG should check either of that by checking the context. And after that, we can safely remove it since our new code already puts watchpoints appropriately.
Comment 15 Yusuke Suzuki 2020-09-02 21:52:52 PDT
Comment on attachment 407845 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=407845&action=review

>>>>> Source/JavaScriptCore/runtime/JSObject.cpp:-2208
>>>>> -
>>>> 
>>>> I revert this for now. The reason is the following,
>>>> 
>>>> 1. DFG Graph::canOptimizeStringObjectAccess is checking both valueOf and toString status
>>>> 2. However, in the normal path, valueOf of StringObject is rarely accessed (toString suceeeds, and we do not access to valueOf).
>>>> 3. As a result, valueOf related structures are not watched at all. So in DFG, DFG Graph::canOptimizeStringObjectAccess failed.
>>>> 
>>>> This affects on JetStream2/date-format-xparb-SP. For now, I'll keep this.
>>> 
>>> Ah, no. Yeah, since this is not invoked from IC, basically, without this, we do not put watchpoints. So, as a result, PropertyCondition::isWatchableWhenValid for Equavalent will fail.
>> 
>> And in this path, we will put this watchpoint for toString, but not for valueOf since we are not accessing it at runtime actually. Then, Graph::canOptimizeStringObjectAccess almost always fail.
>> We should do in the meantime.
> 
> Ideally, DFG should not check toString & valueOf. DFG should check either of that by checking the context. And after that, we can safely remove it since our new code already puts watchpoints appropriately.

Filed https://bugs.webkit.org/show_bug.cgi?id=216117
Maybe, I'll do that for StringObject first since it is super simple.
Comment 16 Yusuke Suzuki 2020-09-02 22:52:43 PDT
Created attachment 407871 [details]
Patch

Patch for landing
Comment 17 Yusuke Suzuki 2020-09-03 18:03:46 PDT
Committed r266567: <https://trac.webkit.org/changeset/266567>
Comment 18 Alexey Shvayka 2020-09-05 14:17:21 PDT
(In reply to Yusuke Suzuki from comment #17)
> Committed r266567: <https://trac.webkit.org/changeset/266567>

JSTests/test262/test/language/expressions/dynamic-import/custom-primitive.js started to fail: https://build.webkit.org/builders/Apple-Catalina-Debug-Test262-Tests/builds/7201.
Comment 19 Yusuke Suzuki 2020-09-05 23:54:08 PDT
(In reply to Alexey Shvayka from comment #18)
> (In reply to Yusuke Suzuki from comment #17)
> > Committed r266567: <https://trac.webkit.org/changeset/266567>
> 
> JSTests/test262/test/language/expressions/dynamic-import/custom-primitive.js
> started to fail:
> https://build.webkit.org/builders/Apple-Catalina-Debug-Test262-Tests/builds/
> 7201.

Will be fixed in https://bugs.webkit.org/show_bug.cgi?id=216222