Bug 185083 - Remove unneeded exception check from String.fromCharCode
Summary: Remove unneeded exception check from String.fromCharCode
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Keith Miller
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2018-04-27 10:57 PDT by Keith Miller
Modified: 2018-04-30 14:39 PDT (History)
7 users (show)

See Also:


Attachments
Patch (1.51 KB, patch)
2018-04-27 11:14 PDT, Keith Miller
no flags Details | Formatted Diff | Diff
Patch (1.61 KB, patch)
2018-04-27 11:32 PDT, Keith Miller
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Keith Miller 2018-04-27 10:57:38 PDT
Remove unneeded exception check from String.fromCharCode
Comment 1 Keith Miller 2018-04-27 11:14:49 PDT
Created attachment 339007 [details]
Patch
Comment 2 Keith Miller 2018-04-27 11:32:56 PDT
Created attachment 339008 [details]
Patch
Comment 3 Darin Adler 2018-04-30 09:41:46 PDT
Comment on attachment 339008 [details]
Patch

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

> Source/JavaScriptCore/runtime/StringConstructor.cpp:80
>          scope.release();

Can we move the declarations of "vm" and "scope" after the if statement and remove this line of code too?
Comment 4 Mark Lam 2018-04-30 09:45:43 PDT
Comment on attachment 339008 [details]
Patch

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

>> Source/JavaScriptCore/runtime/StringConstructor.cpp:80
>>          scope.release();
> 
> Can we move the declarations of "vm" and "scope" after the if statement and remove this line of code too?

Wouldn't the compiler know that vm and scope is not used until after this anyway?  scope.release() is a no-op on release builds.  I suspect that this move is unnecessary.
Comment 5 Darin Adler 2018-04-30 09:57:42 PDT
Comment on attachment 339008 [details]
Patch

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

>>> Source/JavaScriptCore/runtime/StringConstructor.cpp:80
>>>          scope.release();
>> 
>> Can we move the declarations of "vm" and "scope" after the if statement and remove this line of code too?
> 
> Wouldn't the compiler know that vm and scope is not used until after this anyway?  scope.release() is a no-op on release builds.  I suspect that this move is unnecessary.

Sure, it’s unnecessary to generate optimal code if it has no effect on the generated code.

But it’s also unnecessary to write the code the way it currently is; I think it’s clearer the way I suggest, and that’s how it was until a recent change, too. The fast case is clearly separated from the rest of the code with no exception handling code in it.
Comment 6 Mark Lam 2018-04-30 10:08:46 PDT
Comment on attachment 339008 [details]
Patch

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

>>>> Source/JavaScriptCore/runtime/StringConstructor.cpp:80
>>>>          scope.release();
>>> 
>>> Can we move the declarations of "vm" and "scope" after the if statement and remove this line of code too?
>> 
>> Wouldn't the compiler know that vm and scope is not used until after this anyway?  scope.release() is a no-op on release builds.  I suspect that this move is unnecessary.
> 
> Sure, it’s unnecessary to generate optimal code if it has no effect on the generated code.
> 
> But it’s also unnecessary to write the code the way it currently is; I think it’s clearer the way I suggest, and that’s how it was until a recent change, too. The fast case is clearly separated from the rest of the code with no exception handling code in it.

Actually, there's a need to declare the ThrowScope at the very top, and by convention we always do this where a ThrowScope is needed, unless there is a special reasons to make an exception from this rule (more on this below).

The reason for always declaring the ThrowScope at the top is because the purpose of a ThrowScope is to communicate to its caller that this function "may" throw an exception regardless which path is taken during the current invocation (fast, or slow).  This allows us to assert in the parent that a potential exception could have been thrown, and add an exception there if needed.  By declaring the ThrowScope later, we will cease to notify the caller that this function can potentially throw an exception, thereby defeating its purpose.

The only times we don't declare a ThrowScope at the top (and there should be very few of these) is if the function will actually eat / handle all exceptions that it encounters (i.e. it should have a CatchScope at the top), but in an embedded block, it needs a ThrowScope for various reasons.  There may be other reasons for not declaring the ThrowScope at the top. but this is the most obvious one I can think of at the moment.  In the case of stringFromCharCode(), I see no special reason why we should not declare ThrowScope at the top.
Comment 7 Mark Lam 2018-04-30 14:06:42 PDT
Comment on attachment 339008 [details]
Patch

r=me
Comment 8 Keith Miller 2018-04-30 14:11:31 PDT
Comment on attachment 339008 [details]
Patch

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

Thanks for the review!

>>>>> Source/JavaScriptCore/runtime/StringConstructor.cpp:80
>>>>>          scope.release();
>>>> 
>>>> Can we move the declarations of "vm" and "scope" after the if statement and remove this line of code too?
>>> 
>>> Wouldn't the compiler know that vm and scope is not used until after this anyway?  scope.release() is a no-op on release builds.  I suspect that this move is unnecessary.
>> 
>> Sure, it’s unnecessary to generate optimal code if it has no effect on the generated code.
>> 
>> But it’s also unnecessary to write the code the way it currently is; I think it’s clearer the way I suggest, and that’s how it was until a recent change, too. The fast case is clearly separated from the rest of the code with no exception handling code in it.
> 
> Actually, there's a need to declare the ThrowScope at the very top, and by convention we always do this where a ThrowScope is needed, unless there is a special reasons to make an exception from this rule (more on this below).
> 
> The reason for always declaring the ThrowScope at the top is because the purpose of a ThrowScope is to communicate to its caller that this function "may" throw an exception regardless which path is taken during the current invocation (fast, or slow).  This allows us to assert in the parent that a potential exception could have been thrown, and add an exception there if needed.  By declaring the ThrowScope later, we will cease to notify the caller that this function can potentially throw an exception, thereby defeating its purpose.
> 
> The only times we don't declare a ThrowScope at the top (and there should be very few of these) is if the function will actually eat / handle all exceptions that it encounters (i.e. it should have a CatchScope at the top), but in an embedded block, it needs a ThrowScope for various reasons.  There may be other reasons for not declaring the ThrowScope at the top. but this is the most obvious one I can think of at the moment.  In the case of stringFromCharCode(), I see no special reason why we should not declare ThrowScope at the top.

I agree with Mark here. Knowing which functions can throw exceptions is very useful. Given that the compiler will not emit any extra code in release there's really no reason to not have the ThrowScope at the top of the function.
Comment 9 WebKit Commit Bot 2018-04-30 14:38:32 PDT
Comment on attachment 339008 [details]
Patch

Clearing flags on attachment: 339008

Committed r231171: <https://trac.webkit.org/changeset/231171>
Comment 10 WebKit Commit Bot 2018-04-30 14:38:33 PDT
All reviewed patches have been landed.  Closing bug.
Comment 11 Radar WebKit Bug Importer 2018-04-30 14:39:43 PDT
<rdar://problem/39849327>