Bug 117386 - JSString should remember AtomicString
Summary: JSString should remember AtomicString
Status: REOPENED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Bindings (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Ryosuke Niwa
URL:
Keywords: InRadar
Depends on: 118651
Blocks:
  Show dependency treegraph
 
Reported: 2013-06-09 14:58 PDT by Ryosuke Niwa
Modified: 2013-07-13 23:08 PDT (History)
17 users (show)

See Also:


Attachments
Work in progress (10.19 KB, patch)
2013-06-09 15:34 PDT, Ryosuke Niwa
no flags Details | Formatted Diff | Diff
Fixe the bug (17.34 KB, patch)
2013-06-09 17:36 PDT, Ryosuke Niwa
no flags Details | Formatted Diff | Diff
Updated for ToT (12.59 KB, patch)
2013-06-09 17:37 PDT, Ryosuke Niwa
no flags Details | Formatted Diff | Diff
Add Element.idl changes (17.94 KB, patch)
2013-06-09 18:00 PDT, Ryosuke Niwa
no flags Details | Formatted Diff | Diff
Fixed the roping issue (18.00 KB, patch)
2013-06-10 12:59 PDT, Ryosuke Niwa
no flags Details | Formatted Diff | Diff
Patch for landing (22.03 KB, patch)
2013-06-24 21:39 PDT, Ryosuke Niwa
no flags Details | Formatted Diff | Diff
Addressed Geoff's comment (22.04 KB, patch)
2013-06-24 22:08 PDT, Ryosuke Niwa
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ryosuke Niwa 2013-06-09 14:58:45 PDT
We've found out that we're constantly converting the same String to AtomicString in the binding code.
Make JSString smarter and remember the conversion so that the subsequent calls to the same DOM API will not result in another String-to-AtomicString conversion.
Comment 1 Radar WebKit Bug Importer 2013-06-09 15:16:02 PDT
<rdar://problem/14102739>
Comment 2 Ryosuke Niwa 2013-06-09 15:34:57 PDT
Created attachment 204123 [details]
Work in progress
Comment 3 Benjamin Poulain 2013-06-09 17:22:01 PDT
I like the way this is going! :)
Comment 4 Ryosuke Niwa 2013-06-09 17:36:03 PDT
Created attachment 204126 [details]
Fixe the bug
Comment 5 Ryosuke Niwa 2013-06-09 17:37:33 PDT
Created attachment 204127 [details]
Updated for ToT
Comment 6 Sam Weinig 2013-06-09 17:46:32 PDT
Is this a speedup on any benchmarks (normal or micro)?
Comment 7 Ryosuke Niwa 2013-06-09 18:00:53 PDT
Created attachment 204129 [details]
Add Element.idl changes
Comment 8 Geoffrey Garen 2013-06-09 18:17:48 PDT
Side note: This is good evidence that we should merge the Identifier and AtomicString tables. Most of these strings are probably Identifiers to start with.
Comment 9 Ryosuke Niwa 2013-06-09 18:20:03 PDT
(In reply to comment #8)
> Side note: This is good evidence that we should merge the Identifier and AtomicString tables. Most of these strings are probably Identifiers to start with.

Right.  Oliver suggested that when we were discussing it on Friday but I think this is a good starting at least for now.
Comment 10 Gavin Barraclough 2013-06-09 19:55:35 PDT
(In reply to comment #9)
> (In reply to comment #8)
> > Side note: This is good evidence that we should merge the Identifier and AtomicString tables. Most of these strings are probably Identifiers to start with.
> 
> Right.  Oliver suggested that when we were discussing it on Friday but I think this is a good starting at least for now.

Last time I tried this it was a regression, but that was a few years back & won't necessarily still be the case (especially if there are opportunities, as here, to take advantage of it).
Comment 11 Ryosuke Niwa 2013-06-09 20:04:18 PDT
(In reply to comment #10)
> (In reply to comment #9)
> > (In reply to comment #8)
> > > Side note: This is good evidence that we should merge the Identifier and AtomicString tables. Most of these strings are probably Identifiers to start with.
> > 
> > Right.  Oliver suggested that when we were discussing it on Friday but I think this is a good starting at least for now.
> 
> Last time I tried this it was a regression, but that was a few years back & won't necessarily still be the case (especially if there are opportunities, as here, to take advantage of it).

That's good to know.  FWIW, I didn't tried that so it'll be good to get data again.

Somewhat related to this, it might make sense to add a special member function like toAtomicString() on String to generalize this change so that other parts of WebKit can benefit from the same optimization.

A much more aggressive approach will be to online-swap String::m_impl inside AtomicString(const String&) but that may be tricky since doing so might create a stable pointer to StringImpl* elsewhere.
Comment 12 Oliver Hunt 2013-06-09 22:51:02 PDT
Comment on attachment 204129 [details]
Add Element.idl changes

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

I kind of like this approach as an intermediate step.  I think the real problem with Atomic vs. Identifier (aside from thread semantics, etc) is the intended lifetime - a constant string in source code is always going to last as long as the page is live, but selectors, or ids, etc may not.

That said, i find manual annotation of DOMStrings to be somewhat saddening but i'm not sure how to avoid that in the interim

> Source/JavaScriptCore/runtime/JSString.h:378
> +    if (isRope())
> +        static_cast<const JSRopeString*>(this)->resolveRope(exec);

This can fail, so you'll need to handle that with an exception check
Comment 13 Ryosuke Niwa 2013-06-10 00:11:05 PDT
Comment on attachment 204129 [details]
Add Element.idl changes

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

>> Source/JavaScriptCore/runtime/JSString.h:378
>> +        static_cast<const JSRopeString*>(this)->resolveRope(exec);
> 
> This can fail, so you'll need to handle that with an exception check

Can you think of a concrete case where this would fail?  Ideally, I’d like to add a test case for it.
Comment 14 Geoffrey Garen 2013-06-10 08:02:05 PDT
> > This can fail, so you'll need to handle that with an exception check
> 
> Can you think of a concrete case where this would fail?  Ideally, I’d like to add a test case for it.

The two failures cases are 

(1) Out of memory: A rope that fails to malloc when resolved.

(2) Too long: A rope whose length is > 2^32-1 when resolved.

On a 64bit system, it's not practical to trigger (1), but you may be able to trigger (2).
Comment 15 Ryosuke Niwa 2013-06-10 09:49:10 PDT
(In reply to comment #14)
> The two failures cases are 
> 
> (1) Out of memory: A rope that fails to malloc when resolved.
> 
> (2) Too long: A rope whose length is > 2^32-1 when resolved.
> 
> On a 64bit system, it's not practical to trigger (1), but you may be able to trigger (2).

Sounds like I shouldn't necessary make a test case either way... :(  Will just fix the code.
Comment 16 Oliver Hunt 2013-06-10 10:11:20 PDT
(In reply to comment #15)
> (In reply to comment #14)
> > The two failures cases are 
> > 
> > (1) Out of memory: A rope that fails to malloc when resolved.
> > 
> > (2) Too long: A rope whose length is > 2^32-1 when resolved.
> > 
> > On a 64bit system, it's not practical to trigger (1), but you may be able to trigger (2).
> 
> Sounds like I shouldn't necessary make a test case either way... :(  Will just fix the code.

It may be possible to create a large enough rope to fail without actually relying on memory usage:

var str = "a"
for (var i = 0; i < 31; i++)
    str = str + str

str = str + "a"

atomicify(str)
Comment 17 Ryosuke Niwa 2013-06-10 12:59:41 PDT
Created attachment 204178 [details]
Fixed the roping issue
Comment 18 Geoffrey Garen 2013-06-16 12:43:33 PDT
Comment on attachment 204178 [details]
Fixed the roping issue

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

r=me

> Source/JavaScriptCore/runtime/JSString.h:380
> +    if (exec->hadException())
> +        return nullAtom;

You can move this exception check inside the isRope() block, to tell the compiler that an exception can only happen if we have to resolve a rope.
Comment 19 Ryosuke Niwa 2013-06-20 01:09:11 PDT
Unfortunately, this patch doesn't seem to have any impact on html5-full-render.html load time.
Comment 20 Ryosuke Niwa 2013-06-24 21:39:56 PDT
Created attachment 205363 [details]
Patch for landing
Comment 21 Ryosuke Niwa 2013-06-24 22:08:37 PDT
Created attachment 205365 [details]
Addressed Geoff's comment
Comment 22 Ryosuke Niwa 2013-06-25 11:16:46 PDT
Dromaeo results:
r151951 with patch: http://dromaeo.com/?id=197766
r151951 without patch: http://dromaeo.com/?id=197769
Comment 23 WebKit Commit Bot 2013-06-25 20:48:41 PDT
Comment on attachment 205365 [details]
Addressed Geoff's comment

Clearing flags on attachment: 205365

Committed r151978: <http://trac.webkit.org/changeset/151978>
Comment 24 WebKit Commit Bot 2013-06-25 20:48:48 PDT
All reviewed patches have been landed.  Closing bug.
Comment 25 Brady Eidson 2013-07-13 18:08:13 PDT
This change broke 3 sites that we know of already:
http://sigalert.com
http://www.ipernity.com/doc/ndlinegeek
http://io9.com/this-movie-would-have-been-perfect-if-not-for-that-awf-736457142

With how quickly those 3 have been discovered, it's somewhat frightening.

Should we role this out while the regressions are revisited?
Comment 26 Ryosuke Niwa 2013-07-13 18:29:43 PDT
(In reply to comment #25)
> This change broke 3 sites that we know of already:
> http://sigalert.com
> http://www.ipernity.com/doc/ndlinegeek
> http://io9.com/this-movie-would-have-been-perfect-if-not-for-that-awf-736457142
> 
> With how quickly those 3 have been discovered, it's somewhat frightening.
> 
> Should we role this out while the regressions are revisited?

Oops, are there bugs filed about these failures?  Is the kind of failures known?
Comment 27 Gavin Barraclough 2013-07-13 22:53:24 PDT
(In reply to comment #26)
> Oops, are there bugs filed about these failures?  Is the kind of failures known?

This is in radar: <rdar://problem/14408012>
Comment 28 Ryosuke Niwa 2013-07-13 23:04:35 PDT
(In reply to comment #27)
> (In reply to comment #26)
> > Oops, are there bugs filed about these failures?  Is the kind of failures known?
> 
> This is in radar: <rdar://problem/14408012>

Okay, sorry about that. We should roll this patch out because
1. It wasn't a huge win on PLT3 and our Dromaeo score doesn't improve that much either.
2. We should merge Identifier and AtomicString instead.
Comment 29 WebKit Commit Bot 2013-07-13 23:08:47 PDT
Re-opened since this is blocked by bug 118651