Skip to content

Commit 7993925

Browse files
committed
Add a JSC API to allow acquiring the JSLock
https://bugs.webkit.org/show_bug.cgi?id=219663 Reviewed by Filip Pizlo. PerformanceTests: This patch does 3 things: - Change how the geomean is calculated in api-bench to avoid overflowing - Allow linking against the local build we're benchmarking - Adopt the new JSLock API in the UpcomingAPI subtests Using the new API the score improves by ~13.5%. Here are the results for each of the "UpcomingAPI" subtests plus the total score: Before After RichardsMostlyC: 77ms 23ms RichardsMostlyObjC: 309ms 282ms RichardsMostlySwift 305ms 280ms RichardsSomeC: 101ms 95ms RichardsSomeObjC: 160ms 157ms RichardsSomeSwift: 201ms 202ms ------------------------------------------ Score: 29.5974 33.6404 * APIBench/UpcomingAPI/RichardsMostlyC/RichardsMostlyC/richards.c: (workfn): * APIBench/UpcomingAPI/RichardsMostlyObjC/RichardsMostlyObjC.xcodeproj/xcshareddata/xcschemes/RichardsMostlyObjC.xcscheme: Renamed from PerformanceTests/APIBench/UpcomingAPI/RichardsMostlyObjC/RichardsMostlyObjC.xcodeproj/xcshareddata/xcschemes/RichardsMostlySwift.xcscheme. * APIBench/UpcomingAPI/RichardsMostlyObjC/RichardsMostlyObjC/WorkerTask.m: (+[WorkerTask context]): * APIBench/UpcomingAPI/RichardsMostlySwift/RichardsMostlySwift.xcodeproj/project.pbxproj: * APIBench/UpcomingAPI/RichardsMostlySwift/RichardsMostlySwift/BridgingHeader.h: Copied from PerformanceTests/APIBench/UpcomingAPI/RichardsMostlySwift/RichardsMostlySwift/WorkerTask.swift. * APIBench/UpcomingAPI/RichardsMostlySwift/RichardsMostlySwift/WorkerTask.swift: * APIBench/UpcomingAPI/RichardsSomeC/RichardsSomeC/main.m: (main): * APIBench/UpcomingAPI/RichardsSomeObjC/RichardsSomeObjC/main.m: (main): * APIBench/UpcomingAPI/RichardsSomeSwift/RichardsSomeSwift.xcodeproj/project.pbxproj: * APIBench/UpcomingAPI/RichardsSomeSwift/RichardsSomeSwift/BridgingHeader.h: Copied from PerformanceTests/APIBench/UpcomingAPI/RichardsMostlySwift/RichardsMostlySwift/WorkerTask.swift. * APIBench/UpcomingAPI/RichardsSomeSwift/RichardsSomeSwift/main.swift: * APIBench/api-bench: Source/JavaScriptCore: Introduce two new functions to the C API: JSLock and JSUnlock. These functions allow users to take control of the JSContext's lock, which can greatly reduce the overhead of bridging between JS and native. * API/JSLockRef.cpp: Added. (JSLock): (JSUnlock): * API/JSLockRefPrivate.h: Added. * API/JSValueRef.cpp: * JavaScriptCore.xcodeproj/project.pbxproj: * Sources.txt: Canonical link: https://commits.webkit.org/232323@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@270659 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent c55acc8 commit 7993925

21 files changed

Lines changed: 261 additions & 17 deletions

File tree

PerformanceTests/APIBench/CurrentAPI/RichardsMostlyC/RichardsMostlyC/richards.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
#include <stdio.h>
1212
#include <stdlib.h>
13-
1413
#include <JavaScriptCore/JavaScriptCore.h>
15-
#import <QuartzCore/QuartzCore.h>
14+
#include <QuartzCore/QuartzCore.h>
1615

1716
#if 1
1817
#define Count 1000

PerformanceTests/APIBench/References/RichardsC/RichardsC/richards.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
#include <stdio.h>
1212
#include <stdlib.h>
13-
1413
#include <JavaScriptCore/JavaScriptCore.h>
15-
#import <QuartzCore/QuartzCore.h>
14+
#include <QuartzCore/QuartzCore.h>
1615

1716
#if 1
1817
#define Count 1000

PerformanceTests/APIBench/UpcomingAPI/RichardsMostlyC/RichardsMostlyC/richards.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
#include <stdio.h>
1212
#include <stdlib.h>
13-
13+
#include <JavaScriptCore/JSLockRefPrivate.h>
1414
#include <JavaScriptCore/JavaScriptCore.h>
15-
#import <QuartzCore/QuartzCore.h>
15+
#include <QuartzCore/QuartzCore.h>
1616

1717
#if 1
1818
#define Count 1000
@@ -322,6 +322,7 @@ struct task *workfn(struct packet *pkt)
322322
static dispatch_once_t onceToken;
323323
dispatch_once(&onceToken, ^{
324324
ctx = JSGlobalContextCreate(NULL);
325+
JSLock(ctx);
325326
JSObjectRef globalObject = JSContextGetGlobalObject(ctx);
326327

327328
JSStringRef source = readFile("richards.js");

PerformanceTests/APIBench/UpcomingAPI/RichardsMostlyObjC/RichardsMostlyObjC.xcodeproj/xcshareddata/xcschemes/RichardsMostlySwift.xcscheme renamed to PerformanceTests/APIBench/UpcomingAPI/RichardsMostlyObjC/RichardsMostlyObjC.xcodeproj/xcshareddata/xcschemes/RichardsMostlyObjC.xcscheme

File renamed without changes.

PerformanceTests/APIBench/UpcomingAPI/RichardsMostlyObjC/RichardsMostlyObjC/WorkerTask.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#import "WorkerTask.h"
2727

28+
#import <JavaScriptCore/JSLockRefPrivate.h>
2829
#import <JavaScriptCore/JavaScriptCore.h>
2930

3031
@implementation WorkerTask {
@@ -44,6 +45,7 @@ + (JSContext *)context {
4445
static dispatch_once_t onceToken;
4546
dispatch_once(&onceToken, ^{
4647
context = [[JSContext alloc] init];
48+
JSLock(context.JSGlobalContextRef);
4749
[context setExceptionHandler:^(JSContext *context, JSValue *exception) {
4850
NSLog(@"%@", exception.toString);
4951
exit(2);

PerformanceTests/APIBench/UpcomingAPI/RichardsMostlySwift/RichardsMostlySwift.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
140AAC3F249B01D10001F11E /* DeviceTask.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DeviceTask.swift; sourceTree = "<group>"; };
4545
1415AD222499BC5C005BB2E4 /* RichardsMostlySwift */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = RichardsMostlySwift; sourceTree = BUILT_PRODUCTS_DIR; };
4646
1415AD252499BC5C005BB2E4 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
47+
145087E92580581900D94197 /* BridgingHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BridgingHeader.h; sourceTree = "<group>"; };
4748
/* End PBXFileReference section */
4849

4950
/* Begin PBXFrameworksBuildPhase section */
@@ -76,6 +77,7 @@
7677
1415AD242499BC5C005BB2E4 /* RichardsMostlySwift */ = {
7778
isa = PBXGroup;
7879
children = (
80+
145087E92580581900D94197 /* BridgingHeader.h */,
7981
140AAC3F249B01D10001F11E /* DeviceTask.swift */,
8082
140AAC3D249B01D10001F11E /* HandlerTask.swift */,
8183
140AAC3C249B01D10001F11E /* IdleTask.swift */,
@@ -282,6 +284,7 @@
282284
buildSettings = {
283285
CODE_SIGN_STYLE = Automatic;
284286
PRODUCT_NAME = "$(TARGET_NAME)";
287+
SWIFT_OBJC_BRIDGING_HEADER = RichardsMostlySwift/BridgingHeader.h;
285288
SWIFT_VERSION = 5.0;
286289
};
287290
name = Debug;
@@ -291,6 +294,7 @@
291294
buildSettings = {
292295
CODE_SIGN_STYLE = Automatic;
293296
PRODUCT_NAME = "$(TARGET_NAME)";
297+
SWIFT_OBJC_BRIDGING_HEADER = RichardsMostlySwift/BridgingHeader.h;
294298
SWIFT_VERSION = 5.0;
295299
};
296300
name = Release;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2020 Apple Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
* 1. Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
*
13+
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
* THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
#pragma once
27+
28+
#include <JavaScriptCore/JSLockRefPrivate.h>

PerformanceTests/APIBench/UpcomingAPI/RichardsMostlySwift/RichardsMostlySwift/WorkerTask.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class WorkerTask : Task {
3131
private static let context : JSContext = {
3232
let context = JSContext()!
3333
let source = try! String(contentsOfFile: "richards.js")
34+
JSLock(context.jsGlobalContextRef)
3435
context.evaluateScript(source)
3536
context.exceptionHandler = { context, exception in
3637
print(exception!.toString()!)

PerformanceTests/APIBench/UpcomingAPI/RichardsSomeC/RichardsSomeC/main.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
#import <Foundation/Foundation.h>
27+
#import <JavaScriptCore/JSLockRefPrivate.h>
2728
#import <JavaScriptCore/JavaScriptCore.h>
2829
#import <QuartzCore/QuartzCore.h>
2930

@@ -132,7 +133,7 @@ int main()
132133
JSGlobalContextRef context = JSGlobalContextCreate(NULL);
133134
if (!context)
134135
exit(1);
135-
136+
JSLock(context);
136137
JSValueRef exception = NULL;
137138
JSObjectRef globalObject = JSContextGetGlobalObject(context);
138139

PerformanceTests/APIBench/UpcomingAPI/RichardsSomeObjC/RichardsSomeObjC/main.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
#import <Foundation/Foundation.h>
27+
#import <JavaScriptCore/JSLockRefPrivate.h>
2728
#import <JavaScriptCore/JavaScriptCore.h>
2829
#import <QuartzCore/QuartzCore.h>
2930

@@ -93,7 +94,7 @@ int main()
9394
JSContext *context = [[JSContext alloc] init];
9495
if (!context)
9596
exit(1);
96-
97+
JSLock(context.JSGlobalContextRef);
9798
[context setObject:WorkerTask.self forKeyedSubscript:@"WorkerTask"];
9899
[context setExceptionHandler:^(JSContext *context, JSValue *exception) {
99100
NSLog(@"%@", exception.toString);

0 commit comments

Comments
 (0)