Bug 209398

Summary: [JSC] Add JSC::keepAlive(JSValue)
Product: WebKit Reporter: Yusuke Suzuki <ysuzuki>
Component: New BugsAssignee: Yusuke Suzuki <ysuzuki>
Status: RESOLVED FIXED    
Severity: Normal CC: darin, ews-watchlist, ggaren, keith_miller, mark.lam, msaboff, saam, tzagallo, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch mark.lam: review+

Description Yusuke Suzuki 2020-03-22 04:39:34 PDT
[JSC] Add JSC::keepAlive(JSValue)
Comment 1 Yusuke Suzuki 2020-03-22 04:40:31 PDT
Created attachment 394212 [details]
Patch
Comment 2 Yusuke Suzuki 2020-03-22 04:40:46 PDT
Will be used in <rdar://problem/60541567>
Comment 3 Mark Lam 2020-03-22 08:05:30 PDT
Comment on attachment 394212 [details]
Patch

r=me.  It would have been nice if there‘s some way to share the implementation instead of having 2 copies, but I couldn’t think of a clean way to express that: probably more hassle than it’s worth.
Comment 4 Yusuke Suzuki 2020-03-22 17:39:10 PDT
(In reply to Mark Lam from comment #3)
> Comment on attachment 394212 [details]
> Patch
> 
> r=me.  It would have been nice if there‘s some way to share the
> implementation instead of having 2 copies, but I couldn’t think of a clean
> way to express that: probably more hassle than it’s worth.

Yeah, I was thinking about sharing implementation, but there is many conditions like CPU(ADDRESS64), USE(JSVALUE64) etc. And I think just writing this simple function here would be clear than sharing the implementation by putting a lot of ifdefs.
Comment 5 Yusuke Suzuki 2020-03-22 17:40:59 PDT
Committed r258824: <https://trac.webkit.org/changeset/258824>
Comment 6 Radar WebKit Bug Importer 2020-03-22 17:41:15 PDT
<rdar://problem/60752174>
Comment 7 Geoffrey Garen 2020-03-22 20:20:59 PDT
Comment on attachment 394212 [details]
Patch

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

> Source/JavaScriptCore/runtime/JSCJSValue.h:641
> +ALWAYS_INLINE void keepAlive(JSValue value)

This function does not seem to keep 'value' alive, so "keep alive" is not a great name for it.

It appears that you're trying to show the compiler that a given value is used at the point of the call. A better name for that might simply be "use" or "compilerUse".
Comment 8 Yusuke Suzuki 2020-03-22 20:31:55 PDT
Comment on attachment 394212 [details]
Patch

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

>> Source/JavaScriptCore/runtime/JSCJSValue.h:641
>> +ALWAYS_INLINE void keepAlive(JSValue value)
> 
> This function does not seem to keep 'value' alive, so "keep alive" is not a great name for it.
> 
> It appears that you're trying to show the compiler that a given value is used at the point of the call. A better name for that might simply be "use" or "compilerUse".

I think this "keepAlive" is keeping 'value' alive.
While the implementation of this is `compilerUse` or `use`, the effect and goal of this function is keeping 'value' alive by keeping it in conservative roots at the calling point.
So, this "keepAlive" name is describing the purpose and effect of this function, no?
Comment 9 Yusuke Suzuki 2020-03-22 20:36:32 PDT
Comment on attachment 394212 [details]
Patch

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

>>> Source/JavaScriptCore/runtime/JSCJSValue.h:641
>>> +ALWAYS_INLINE void keepAlive(JSValue value)
>> 
>> This function does not seem to keep 'value' alive, so "keep alive" is not a great name for it.
>> 
>> It appears that you're trying to show the compiler that a given value is used at the point of the call. A better name for that might simply be "use" or "compilerUse".
> 
> I think this "keepAlive" is keeping 'value' alive.
> While the implementation of this is `compilerUse` or `use`, the effect and goal of this function is keeping 'value' alive by keeping it in conservative roots at the calling point.
> So, this "keepAlive" name is describing the purpose and effect of this function, no?

Note that FTL has the same feature. https://github.com/WebKit/webkit/blob/master/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp#L18347-L18355
Comment 10 Geoffrey Garen 2020-03-22 20:52:02 PDT
Most confusing to me about the phrase "keepAlive" is when the function will stop keeping the value alive. 

If the goal is to create a root for conservative GC, maybe a better name is "gcRoot" or "conservativeRoot".
Comment 11 Yusuke Suzuki 2020-03-22 20:58:54 PDT
(In reply to Geoffrey Garen from comment #10)
> Most confusing to me about the phrase "keepAlive" is when the function will
> stop keeping the value alive. 
> 
> If the goal is to create a root for conservative GC, maybe a better name is
> "gcRoot" or "conservativeRoot".

I think `gcRoot` and `conservativeRoot` look ambiguous given that we already have `root` function in WebCore, and it is used for completely different purpose: returning opaque-root for the given pointer. Since "root" function is also used in GC's context, I think `gcRoot` / `conservativeRoot` look like the similar / same to `root` function in WebCore. But they are different.

How about using `keepAliveAtThisPoint` `ensureReachableFromGCRootAtThisPoint` instead? They clarify that this keep-alive continue until this point.
Comment 12 Yusuke Suzuki 2020-03-22 21:01:16 PDT
(In reply to Yusuke Suzuki from comment #11)
> (In reply to Geoffrey Garen from comment #10)
> > Most confusing to me about the phrase "keepAlive" is when the function will
> > stop keeping the value alive. 
> > 
> > If the goal is to create a root for conservative GC, maybe a better name is
> > "gcRoot" or "conservativeRoot".
> 
> I think `gcRoot` and `conservativeRoot` look ambiguous given that we already
> have `root` function in WebCore, and it is used for completely different
> purpose: returning opaque-root for the given pointer. Since "root" function
> is also used in GC's context, I think `gcRoot` / `conservativeRoot` look
> like the similar / same to `root` function in WebCore. But they are
> different.
> 
> How about using `keepAliveAtThisPoint`
> `ensureReachableFromGCRootAtThisPoint` instead? They clarify that this
> keep-alive continue until this point.

Example of root function in WebCore. https://github.com/WebKit/webkit/blob/master/Source/WebCore/bindings/js/JSCSSRuleCustom.h#L33-L42 This is not used for keeping value alive in conservative root. It is a function returning opaque-root for the given pointer.
Comment 13 Yusuke Suzuki 2020-03-22 21:06:46 PDT
(In reply to Yusuke Suzuki from comment #12)
> (In reply to Yusuke Suzuki from comment #11)
> > (In reply to Geoffrey Garen from comment #10)
> > > Most confusing to me about the phrase "keepAlive" is when the function will
> > > stop keeping the value alive. 
> > > 
> > > If the goal is to create a root for conservative GC, maybe a better name is
> > > "gcRoot" or "conservativeRoot".
> > 
> > I think `gcRoot` and `conservativeRoot` look ambiguous given that we already
> > have `root` function in WebCore, and it is used for completely different
> > purpose: returning opaque-root for the given pointer. Since "root" function
> > is also used in GC's context, I think `gcRoot` / `conservativeRoot` look
> > like the similar / same to `root` function in WebCore. But they are
> > different.
> > 
> > How about using `keepAliveAtThisPoint`
> > `ensureReachableFromGCRootAtThisPoint` instead? They clarify that this
> > keep-alive continue until this point.
> 
> Example of root function in WebCore.
> https://github.com/WebKit/webkit/blob/master/Source/WebCore/bindings/js/
> JSCSSRuleCustom.h#L33-L42 This is not used for keeping value alive in
> conservative root. It is a function returning opaque-root for the given
> pointer.

If user uses `root` function instead of `gcRoot` / `conservativeRoot` functions to keep value alive, this is wrong. And since both `root` and `gcRoot` will accept void*, it would be difficult to notice. So I think we should pick clear name here. Maybe, "ensureReachableFromGCRootAtThisPoint" would be the best.
Comment 14 Mark Lam 2020-03-22 21:42:30 PDT
(In reply to Yusuke Suzuki from comment #13)
> If user uses `root` function instead of `gcRoot` / `conservativeRoot`
> functions to keep value alive, this is wrong. And since both `root` and
> `gcRoot` will accept void*, it would be difficult to notice. So I think we
> should pick clear name here. Maybe, "ensureReachableFromGCRootAtThisPoint"
> would be the best.

How about something shorter like "ensureStillAliveHere"?  I like "ensure" better than "keep", and I think it is a better description of what this function is trying to do.
Comment 15 Yusuke Suzuki 2020-03-22 21:52:51 PDT
(In reply to Mark Lam from comment #14)
> (In reply to Yusuke Suzuki from comment #13)
> > If user uses `root` function instead of `gcRoot` / `conservativeRoot`
> > functions to keep value alive, this is wrong. And since both `root` and
> > `gcRoot` will accept void*, it would be difficult to notice. So I think we
> > should pick clear name here. Maybe, "ensureReachableFromGCRootAtThisPoint"
> > would be the best.
> 
> How about something shorter like "ensureStillAliveHere"?  I like "ensure"
> better than "keep", and I think it is a better description of what this
> function is trying to do.

Sounds fine! I'll rename it to ensureStillAliveHere.
Comment 16 Yusuke Suzuki 2020-03-22 21:57:12 PDT
Committed r258825: <https://trac.webkit.org/changeset/258825>
Comment 17 Darin Adler 2020-03-23 10:13:58 PDT
Nice! Way better name!
Comment 18 Keith Miller 2020-03-23 10:17:21 PDT
Comment on attachment 394212 [details]
Patch

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

>>>> Source/JavaScriptCore/runtime/JSCJSValue.h:641
>>>> +ALWAYS_INLINE void keepAlive(JSValue value)
>>> 
>>> This function does not seem to keep 'value' alive, so "keep alive" is not a great name for it.
>>> 
>>> It appears that you're trying to show the compiler that a given value is used at the point of the call. A better name for that might simply be "use" or "compilerUse".
>> 
>> I think this "keepAlive" is keeping 'value' alive.
>> While the implementation of this is `compilerUse` or `use`, the effect and goal of this function is keeping 'value' alive by keeping it in conservative roots at the calling point.
>> So, this "keepAlive" name is describing the purpose and effect of this function, no?
> 
> Note that FTL has the same feature. https://github.com/WebKit/webkit/blob/master/Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp#L18347-L18355

I think it might have been clearer if this was a C++ class that does this when being destructed.