Bug 118573 - Optimize the thread locks for API Shims
Summary: Optimize the thread locks for API Shims
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-07-11 11:32 PDT by Yi Shen
Modified: 2013-07-25 10:00 PDT (History)
4 users (show)

See Also:


Attachments
Test case (411 bytes, text/html)
2013-07-11 11:33 PDT, Yi Shen
no flags Details
proposal patch (3.42 KB, patch)
2013-07-11 11:53 PDT, Yi Shen
ggaren: review-
Details | Formatted Diff | Diff
Remove the thread lock from API Shims if the VM has an exclusive thread (5.72 KB, patch)
2013-07-12 15:58 PDT, Yi Shen
no flags Details | Formatted Diff | Diff
Test sample code for JSC API (50.95 KB, application/zip)
2013-07-23 16:57 PDT, Yi Shen
no flags Details
Test result (201.72 KB, image/png)
2013-07-23 16:58 PDT, Yi Shen
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Yi Shen 2013-07-11 11:32:31 PDT
Remove the thread lock from API Shims if the VM is not a APIShared type. The similar logic could be found in old revision of JSLock, which does the lock or unlock only when exec->globalData().isSharedInstance() is true. By removing the thread lock, the JSC API static function call could be 60% times faster than before.
Comment 1 Yi Shen 2013-07-11 11:33:04 PDT
Created attachment 206479 [details]
Test case
Comment 2 Yi Shen 2013-07-11 11:35:27 PDT
                 No cache        Cache
result(ms)
1.                  8000          3190
2.                  7600          3160
3.                  7987          3208
4.                  8002          3305
5.                  8155          3173
6.                  7539          3125
7.                  8562          3135
8.                  7455          3137
9.                  7936          3122
10.                 7461          3103
---------------------------------------
avg                 7869          3166
Comment 3 Yi Shen 2013-07-11 11:36:49 PDT
The first row should be     
             With lock          Without lock
(In reply to comment #2)
>                  No cache        Cache
> result(ms)
> 1.                  8000          3190
> 2.                  7600          3160
> 3.                  7987          3208
> 4.                  8002          3305
> 5.                  8155          3173
> 6.                  7539          3125
> 7.                  8562          3135
> 8.                  7455          3137
> 9.                  7936          3122
> 10.                 7461          3103
> ---------------------------------------
> avg                 7869          3166
Comment 4 Yi Shen 2013-07-11 11:53:13 PDT
Created attachment 206480 [details]
proposal patch
Comment 5 Geoffrey Garen 2013-07-11 13:24:48 PDT
Comment on attachment 206480 [details]
proposal patch

Even unshared VMs need locking, since they can be used on more than one thread.

I'm intrigued by your test case, though. Maybe there's another way to optimize this.
Comment 6 Chang Shu 2013-07-11 13:39:06 PDT
(In reply to comment #5)
> (From update of attachment 206480 [details])
> Even unshared VMs need locking, since they can be used on more than one thread.
> 
> I'm intrigued by your test case, though. Maybe there's another way to optimize this.

I opened a bug with the same test before and Yi is trying to resolve this issue through a series of patches. Your help is very appreciated.

https://bugs.webkit.org/show_bug.cgi?id=97122
Comment 7 Yi Shen 2013-07-12 11:27:59 PDT
Thanks for review. How about removing the API thread lock/unlock for the VM which has an exclusive thread? For example, WK2 is using JSC APIs within the injected bundle, which runs in the WebCore's exclusive VM. I believe it is thread safety when executing JSC APIs in the WebCore's VM.  

(In reply to comment #5)
> (From update of attachment 206480 [details])
> Even unshared VMs need locking, since they can be used on more than one thread.
> 
> I'm intrigued by your test case, though. Maybe there's another way to optimize this.
Comment 8 Geoffrey Garen 2013-07-12 11:32:13 PDT
> Thanks for review. How about removing the API thread lock/unlock for the VM which has an exclusive thread? For example, WK2 is using JSC APIs within the injected bundle, which runs in the WebCore's exclusive VM. I believe it is thread safety when executing JSC APIs in the WebCore's VM.  

I think a specialized design like that would need a more real-world benchmark to justify itself.

There are lots of opportunities for optimizing JSLock.
Comment 9 Yi Shen 2013-07-12 15:57:04 PDT
I do have a more real-world test for this. Will add a patch for review and post a real-world benchmark next week. Thanks in advanced!
(In reply to comment #8)
> > Thanks for review. How about removing the API thread lock/unlock for the VM which has an exclusive thread? For example, WK2 is using JSC APIs within the injected bundle, which runs in the WebCore's exclusive VM. I believe it is thread safety when executing JSC APIs in the WebCore's VM.  
> 
> I think a specialized design like that would need a more real-world benchmark to justify itself.
> 
> There are lots of opportunities for optimizing JSLock.
Comment 10 Yi Shen 2013-07-12 15:58:25 PDT
Created attachment 206576 [details]
Remove the thread lock from API Shims if the VM has an exclusive thread
Comment 11 Yi Shen 2013-07-23 16:57:53 PDT
Created attachment 207361 [details]
Test sample code for JSC API
Comment 12 Yi Shen 2013-07-23 16:58:32 PDT
Created attachment 207363 [details]
Test result
Comment 13 Yi Shen 2013-07-23 17:14:01 PDT
The added test code is porting from JSTweener by using JSC APIs (WebKit2 injected bundle). http://coderepos.org/share/wiki/JSTweener

The test is a rendering heavy page, so the PFS changes for tuning on/off thread lock may not be 'significant' - the average fps was increased from ~31 to ~34 on my machine after applied my patch. (I was hoping I can add a JS property accessing heavy page, but which turns out it needs a lot more work) In addition, I have run this test on my Mac for hundreds times, and no thread crash observed.
Comment 14 Geoffrey Garen 2013-07-23 17:41:56 PDT
Comment on attachment 206576 [details]
Remove the thread lock from API Shims if the VM has an exclusive thread

r=me
Comment 15 Yi Shen 2013-07-25 09:37:26 PDT
Thanks for reviewing it :)
(In reply to comment #14)
> (From update of attachment 206576 [details])
> r=me
Comment 16 WebKit Commit Bot 2013-07-25 10:00:44 PDT
Comment on attachment 206576 [details]
Remove the thread lock from API Shims if the VM has an exclusive thread

Clearing flags on attachment: 206576

Committed r153331: <http://trac.webkit.org/changeset/153331>
Comment 17 WebKit Commit Bot 2013-07-25 10:00:47 PDT
All reviewed patches have been landed.  Closing bug.