Bug 110972 - [JSC] Atomic-ize strings before caching them in jsStringWithCache().
Summary: [JSC] Atomic-ize strings before caching them in jsStringWithCache().
Status: RESOLVED WONTFIX
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore JavaScript (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Andreas Kling
URL:
Keywords: InRadar, Performance
Depends on:
Blocks: 111041
  Show dependency treegraph
 
Reported: 2013-02-27 04:50 PST by Andreas Kling
Modified: 2013-07-17 09:57 PDT (History)
3 users (show)

See Also:


Attachments
Possibly a patch (1.67 KB, patch)
2013-02-27 04:52 PST, Andreas Kling
koivisto: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Kling 2013-02-27 04:50:33 PST
It's quite common for jsStringWithCache() to put tons of duplicate strings in the DOMWrapperWorld's string cache.
We could avoid this by making sure that all >1 character strings are atomic strings before caching them.
Comment 1 Andreas Kling 2013-02-27 04:52:57 PST
Created attachment 190491 [details]
Possibly a patch
Comment 2 Benjamin Poulain 2013-02-27 09:53:26 PST
Related to <rdar://problem/12201108>.
Comment 3 Geoffrey Garen 2013-02-27 11:08:35 PST
Comment on attachment 190491 [details]
Possibly a patch

I think it would be even better to track these strings back to their sources, and store those sources as AtomicStrings. This has two advantages:

(1) Avoids thrashing the AtomicString table when passing strings to JavaScriptCore.

(2) Reduces memory use in the DOM.

There's nothing special about the JavaScript binding layer that causes likely string duplication -- it's some other layer in the DOM that produced all these duplicates, and hooking in at the JavaScript binding layer is just a convenient work-around.
Comment 4 Benjamin Poulain 2013-02-27 11:15:33 PST
> There's nothing special about the JavaScript binding layer that causes likely string duplication -- it's some other layer in the DOM that produced all these duplicates, and hooking in at the JavaScript binding layer is just a convenient work-around.

I don't think this help for duplicate String, but helps for duplicate JSString.

Instead of using AtomicString here, I am in favor of changing JSStringCache to use the String hash instead of the PtrHash. That way we have one cache instead of two.

Any opinion?
Comment 5 Andreas Kling 2013-02-27 11:17:39 PST
(In reply to comment #4)
> > There's nothing special about the JavaScript binding layer that causes likely string duplication -- it's some other layer in the DOM that produced all these duplicates, and hooking in at the JavaScript binding layer is just a convenient work-around.
> 
> I don't think this help for duplicate String, but helps for duplicate JSString.
> 
> Instead of using AtomicString here, I am in favor of changing JSStringCache to use the String hash instead of the PtrHash. That way we have one cache instead of two.
> 
> Any opinion?

I like that approach.

FWIW the source of 99% of duplicates on the HTML5 spec are from HTMLAnchorElement::hash() which could be changed to return an AtomicString.
Comment 6 Geoffrey Garen 2013-02-27 11:23:10 PST
> I don't think this help for duplicate String, but helps for duplicate JSString.

We can infer from the duplicate JSStrings that the DOM is wasting memory with duplicate Strings. Solving the duplicate Strings in the DOM will solve both problems in one go. That's why I think it's even better.
Comment 7 Andreas Kling 2013-02-27 11:26:27 PST
(In reply to comment #6)
> > I don't think this help for duplicate String, but helps for duplicate JSString.
> 
> We can infer from the duplicate JSStrings that the DOM is wasting memory with duplicate Strings. Solving the duplicate Strings in the DOM will solve both problems in one go. That's why I think it's even better.

In this case they are not duplicate Strings in the DOM, but rather the same String generated multiple times every time a getter is called.
Comment 8 Geoffrey Garen 2013-02-27 11:27:21 PST
> Instead of using AtomicString here, I am in favor of changing JSStringCache to use the String hash instead of the PtrHash. That way we have one cache instead of two.

<http://trac.webkit.org/changeset/119341> explains why a String hash was a bad idea at one point. Those criticisms may still apply, or not -- I didn't look deeply.
Comment 9 Geoffrey Garen 2013-02-27 22:40:21 PST
> <http://trac.webkit.org/changeset/119341> explains why a String hash was a bad idea at one point. 

So, I re-read this, and the problem with String hash is that it causes the key to be freed if you call HashMap::set(). So, if we switch to String hash, we also need to switch the key to be RefPtr<StringImpl>.
Comment 10 Benjamin Poulain 2013-02-27 22:42:16 PST
(In reply to comment #9)
> > <http://trac.webkit.org/changeset/119341> explains why a String hash was a bad idea at one point. 
> 
> So, I re-read this, and the problem with String hash is that it causes the key to be freed if you call HashMap::set(). So, if we switch to String hash, we also need to switch the key to be RefPtr<StringImpl>.

I have an idea for something clean, I'll look at that tomorrow. :)