RESOLVED FIXED 81446
Cache the type string of JavaScript object
https://bugs.webkit.org/show_bug.cgi?id=81446
Summary Cache the type string of JavaScript object
Benjamin Poulain
Reported 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.
Attachments
Patch (16.96 KB, patch)
2012-03-17 00:43 PDT, Benjamin Poulain
no flags
Alternative (6.23 KB, patch)
2012-03-19 19:45 PDT, Benjamin Poulain
no flags
Patch (8.90 KB, patch)
2012-03-19 23:46 PDT, Benjamin Poulain
ggaren: review+
Benjamin Poulain
Comment 1 2012-03-17 00:43:49 PDT
Philippe Normand
Comment 2 2012-03-19 01:04:04 PDT
Gyuyoung Kim
Comment 3 2012-03-19 01:27:55 PDT
Early Warning System Bot
Comment 4 2012-03-19 11:13:28 PDT
Early Warning System Bot
Comment 5 2012-03-19 11:40:33 PDT
Build Bot
Comment 6 2012-03-19 12:28:00 PDT
Geoffrey Garen
Comment 7 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.
Geoffrey Garen
Comment 8 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.
Alexey Proskuryakov
Comment 9 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.
Oliver Hunt
Comment 10 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?
Geoffrey Garen
Comment 11 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.
Benjamin Poulain
Comment 12 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.
Geoffrey Garen
Comment 13 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%.
Benjamin Poulain
Comment 14 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?
Benjamin Poulain
Comment 15 2012-03-19 23:46:54 PDT
Benjamin Poulain
Comment 16 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.
Geoffrey Garen
Comment 17 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.
Benjamin Poulain
Comment 18 2012-03-20 13:23:27 PDT
Note You need to log in before you can comment on or make changes to this bug.