Bug 81446

Summary: Cache the type string of JavaScript object
Product: WebKit Reporter: Benjamin Poulain <benjamin>
Component: JavaScriptCoreAssignee: Benjamin Poulain <benjamin>
Status: RESOLVED FIXED    
Severity: Normal CC: ap, barraclough, ggaren, gustavo, pnormand, xan.lopez
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Alternative
none
Patch ggaren: review+

Description Benjamin Poulain 2012-03-17 00:35:27 PDT
Currently, we create a new string for every time we need the type string, we can do better than that.
Comment 1 Benjamin Poulain 2012-03-17 00:43:49 PDT
Created attachment 132452 [details]
Patch
Comment 2 Philippe Normand 2012-03-19 01:04:04 PDT
Comment on attachment 132452 [details]
Patch

Attachment 132452 [details] did not pass gtk-ews (gtk):
Output: http://queues.webkit.org/results/11988018
Comment 3 Gyuyoung Kim 2012-03-19 01:27:55 PDT
Comment on attachment 132452 [details]
Patch

Attachment 132452 [details] did not pass efl-ews (efl):
Output: http://queues.webkit.org/results/11982591
Comment 4 Early Warning System Bot 2012-03-19 11:13:28 PDT
Comment on attachment 132452 [details]
Patch

Attachment 132452 [details] did not pass qt-ews (qt):
Output: http://queues.webkit.org/results/11989193
Comment 5 Early Warning System Bot 2012-03-19 11:40:33 PDT
Comment on attachment 132452 [details]
Patch

Attachment 132452 [details] did not pass qt-wk2-ews (qt):
Output: http://queues.webkit.org/results/11980877
Comment 6 Build Bot 2012-03-19 12:28:00 PDT
Comment on attachment 132452 [details]
Patch

Attachment 132452 [details] did not pass win-ews (win):
Output: http://queues.webkit.org/results/11989243
Comment 7 Geoffrey Garen 2012-03-19 13:36:31 PDT
Comment on attachment 132452 [details]
Patch

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

Approach looks good. r- because it doesn't build on all platforms.

Also, one comment below.

> Source/JavaScriptCore/runtime/JSValue.cpp:NaN
>  JSString* JSValue::toStringSlowCase(ExecState* exec) const

Please add common strings for all the JSValue::toString strings. These can be very common just like the type string case.
Comment 8 Geoffrey Garen 2012-03-19 13:38:33 PDT
... Did you consider use the SmallStrings storage instead? That has the nice side-benefit of using weak pointers, so its allocations don't fragment the heap.
Comment 9 Alexey Proskuryakov 2012-03-19 13:52:45 PDT
Will we have more trouble with profiling/Web Inspector, as we're getting additional "leaking" objects?

SmallStrings used to confuse people looking at Caches debug window.
Comment 10 Oliver Hunt 2012-03-19 14:00:44 PDT
Comment on attachment 132452 [details]
Patch

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

> Source/JavaScriptCore/runtime/CommonIdentifiers.h:75
> -    macro(displayName) \
> -    macro(undefined)
> +    macro(displayName)

Why remove undefined from the common identifiers?
Comment 11 Geoffrey Garen 2012-03-19 14:09:38 PDT
> Will we have more trouble with profiling/Web Inspector, as we're getting additional "leaking" objects?
> 
> SmallStrings used to confuse people looking at Caches debug window.

Like I said, SmallStrings are weak, so they disappear if not in use.
Comment 12 Benjamin Poulain 2012-03-19 14:11:21 PDT
(In reply to comment #8)
> ... Did you consider use the SmallStrings storage instead? That has the nice side-benefit of using weak pointers, so its allocations don't fragment the heap.

I did, but then we keep trashing the strings. I also considered CommonIdentifier + smallstrings. In the end, I went for the fastest in the benchmarks.

> > Source/JavaScriptCore/runtime/JSValue.cpp:NaN
> >  JSString* JSValue::toStringSlowCase(ExecState* exec) const
> 
> Please add common strings for all the JSValue::toString strings. These can be very common just like the type string case.

I have a regression related to JSValue::toString() and I wanted to see that function separately. Your idea does not affect the regression so I can add that.

> > Source/JavaScriptCore/runtime/CommonIdentifiers.h:75
> > -    macro(displayName) \
> > -    macro(undefined)
> > +    macro(displayName)
> 
> Why remove undefined from the common identifiers?

It is unused with this patch. The cached JSString is used instead.
Comment 13 Geoffrey Garen 2012-03-19 14:24:09 PDT
> > ... Did you consider use the SmallStrings storage instead? That has the nice side-benefit of using weak pointers, so its allocations don't fragment the heap.
> 
> I did, but then we keep trashing the strings.

Can you elaborate on this? Using this technique, how much benchmark time is spent creating strings?

If we assume that small strings occupy 1KB of space (which is pretty generous), and that the heap is only 512KB in size, the maximum time you'd ever expect to spend allocating small strings is on the order of 1/512 = 0.002%.
Comment 14 Benjamin Poulain 2012-03-19 19:45:48 PDT
Created attachment 132749 [details]
Alternative

> Can you elaborate on this? Using this technique, how much benchmark time is spent creating strings?
> 
> If we assume that small strings occupy 1KB of space (which is pretty generous), and that the heap is only 512KB in size, the maximum time you'd ever expect to spend allocating small strings is on the order of 1/512 = 0.002%.

Here is a patch for the other version.

The trashing of strings was just an assumption. Profiling does not show any difference in the top functions between those two patches (fastFree() and fastMalloc() included).

I have no clue why this is slower than the previous patch (by around 1.7%). I think that could just be my system, I am tempted to go with this because it is simpler... Any opinion?
Comment 15 Benjamin Poulain 2012-03-19 23:46:54 PDT
Created attachment 132769 [details]
Patch
Comment 16 Benjamin Poulain 2012-03-19 23:49:24 PDT
Comment on attachment 132769 [details]
Patch

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

> Source/JavaScriptCore/runtime/SmallStrings.h:81
> +        JSString* name##Keyword(JSGlobalData* globalData) const \

I suffixed with "Keyword" to avoid name clash with the C++ keywords.
I considered the suffix "String", but "nullString" is too ambiguous in my opinion.

Any suggestion for a better name is welcome.
Comment 17 Geoffrey Garen 2012-03-20 09:37:25 PDT
Comment on attachment 132769 [details]
Patch

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

r=me

Hate to leave 1.7% on the table, but I do prefer the approach here for memory reasons, and the overall speedup is still quite large. I think it's reasonable to take the speedup and move on.

>> Source/JavaScriptCore/runtime/SmallStrings.h:81
>> +        JSString* name##Keyword(JSGlobalData* globalData) const \
> 
> I suffixed with "Keyword" to avoid name clash with the C++ keywords.
> I considered the suffix "String", but "nullString" is too ambiguous in my opinion.
> 
> Any suggestion for a better name is welcome.

I'd prefer "name##String" because not all of these strings are, in fact, keywords.
Comment 18 Benjamin Poulain 2012-03-20 13:23:27 PDT
Committed r111433: <http://trac.webkit.org/changeset/111433>