Bug 96371

Summary: Web Inspector: Avoid slice-ing listeners array in WI.Object.prototype.dispatchEventToListeners
Product: WebKit Reporter: Alexander Pavlov (apavlov) <apavlov>
Component: Web InspectorAssignee: Nobody <webkit-unassigned>
Status: RESOLVED WONTFIX    
Severity: Normal CC: apavlov, bburg, bweinstein, graouts, joepeck, keishi, loislo, oliver, pfeldman, pmuellr, rik, timothy, vsevik, webkit-bug-importer, yurys
Priority: P2 Keywords: InRadar
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   
Attachments:
Description Flags
Patch vsevik: review-

Description Alexander Pavlov (apavlov) 2012-09-11 03:44:12 PDT
Rationale: According to http://jsperf.com/slice-vs-loop-copy, loop-copying an array of 3 elements vs slice(0) is about 2.8 times faster on V8 and almost 2 times faster on JSC. The difference increases with the array length decrease. This is critical for event dispatching-intensive code paths (e.g. continuous DOM updates).

Test: While profiling Web Inspector running against a page with lots of dynamically modified attributes, the patched version turned out to be about 30% faster than the original one (3.8 sec vs. 5.1 sec).
Comment 1 Alexander Pavlov (apavlov) 2012-09-11 03:51:20 PDT
Created attachment 163325 [details]
Patch
Comment 2 Vsevolod Vlasov 2012-09-11 09:07:29 PDT
Comment on attachment 163325 [details]
Patch

This will probably regress scenarios when we have more listeners.
Comment 3 Yury Semikhatsky 2012-09-11 22:14:31 PDT
(In reply to comment #2)
> (From update of attachment 163325 [details])
> This will probably regress scenarios when we have more listeners.
We may add a check for a threshold if this optimization has a noticeable effect.
Comment 4 Pavel Feldman 2012-09-11 23:42:28 PDT
If you think performance is critical here - do copy-on-write. I.e. iterate over original and put it into the iterating-over Set. If upon addEventListener your list is in Set - clone it. Finally we put has() method into our Map and make Set implementation possible.
Comment 5 Vsevolod Vlasov 2012-09-12 00:24:53 PDT
(In reply to comment #4)
> If you think performance is critical here - do copy-on-write. I.e. iterate over original and put it into the iterating-over Set. If upon addEventListener your list is in Set - clone it. Finally we put has() method into our Map and make Set implementation possible.

This was actually all about one element listeners array case.
Apparently putting something into the set makes things even slower in this case.
Special-casing one element case might help but I don't think that we currently have a scenario where this whole issue is a problem.
Comment 6 Brian Burg 2014-12-13 15:41:29 PST
the linked jsperf shows manual copy about 75% faster than for-loop. Why can't we optimize Array.prototype.slice() instead? I'm not convinced this is a big perf issue right now, since jsperf is probably not representative of how WI code will be optimized.
Comment 7 Radar WebKit Bug Importer 2014-12-17 11:23:31 PST
<rdar://problem/19281542>
Comment 8 Timothy Hatcher 2016-02-04 19:50:09 PST
We replaced this code recently.