Skip to content

Commit

Permalink
dart2js: Optimize Function.apply for 0-3 argument calls.
Browse files Browse the repository at this point in the history
R=sra@google.com

Review URL: https://codereview.chromium.org//1032013002

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@44704 260f80e4-7a28-3924-810f-c04153c831b5
  • Loading branch information
floitschG committed Mar 25, 2015
1 parent 89daba5 commit 14ddfb7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pkg/compiler/lib/src/js_backend/namer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,10 @@ class Namer {
case JsGetName.GETTER_PREFIX: return getterPrefix;
case JsGetName.SETTER_PREFIX: return setterPrefix;
case JsGetName.CALL_PREFIX: return callPrefix;
case JsGetName.CALL_PREFIX0: return '${callPrefix}\$0';
case JsGetName.CALL_PREFIX1: return '${callPrefix}\$1';
case JsGetName.CALL_PREFIX2: return '${callPrefix}\$2';
case JsGetName.CALL_PREFIX3: return '${callPrefix}\$3';
case JsGetName.CALL_CATCH_ALL: return callCatchAllName;
case JsGetName.REFLECTABLE: return reflectableField;
case JsGetName.CLASS_DESCRIPTOR_PROPERTY:
Expand Down
37 changes: 32 additions & 5 deletions sdk/lib/_internal/compiler/js_lib/js_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1146,20 +1146,47 @@ class Primitives {

static applyFunctionWithPositionalArguments(Function function,
List positionalArguments) {
int argumentCount = 0;
List arguments;

if (positionalArguments != null) {
if (JS('bool', '# instanceof Array', positionalArguments)) {
arguments = positionalArguments;
arguments = JS('JSArray', '#', positionalArguments);
} else {
arguments = new List.from(positionalArguments);
}
argumentCount = JS('int', '#.length', arguments);
} else {
arguments = [];
}

if (arguments.length == 0) {
String selectorName = JS_GET_NAME(JsGetName.CALL_PREFIX0);
if (JS('bool', '!!#[#]', function, selectorName)) {
return JS('', '#[#]()', function, selectorName);
}
} else if (arguments.length == 1) {
String selectorName = JS_GET_NAME(JsGetName.CALL_PREFIX1);
if (JS('bool', '!!#[#]', function, selectorName)) {
return JS('', '#[#](#[0])', function, selectorName, arguments);
}
} else if (arguments.length == 2) {
String selectorName = JS_GET_NAME(JsGetName.CALL_PREFIX2);
if (JS('bool', '!!#[#]', function, selectorName)) {
return JS('', '#[#](#[0],#[1])', function, selectorName,
arguments, arguments);
}
} else if (arguments.length == 3) {
String selectorName = JS_GET_NAME(JsGetName.CALL_PREFIX3);
if (JS('bool', '!!#[#]', function, selectorName)) {
return JS('', '#[#](#[0],#[1],#[2])', function, selectorName,
arguments, arguments, arguments);
}
}
return _genericApplyFunctionWithPositionalArguments(function, arguments);
}

static _genericApplyFunctionWithPositionalArguments(Function function,
List arguments) {
int argumentCount = arguments.length;
String selectorName =
'${JS_GET_NAME(JsGetName.CALL_PREFIX)}\$$argumentCount';
var jsFunction = JS('var', '#[#]', function, selectorName);
Expand All @@ -1168,7 +1195,7 @@ class Primitives {
jsFunction = JS('', '#["call*"]', interceptor);

if (jsFunction == null) {
return functionNoSuchMethod(function, positionalArguments, null);
return functionNoSuchMethod(function, arguments, null);
}
ReflectionInfo info = new ReflectionInfo(jsFunction);
int requiredArgumentCount = info.requiredParameterCount;
Expand All @@ -1177,7 +1204,7 @@ class Primitives {
if (info.areOptionalParametersNamed ||
requiredArgumentCount > argumentCount ||
maxArgumentCount < argumentCount) {
return functionNoSuchMethod(function, positionalArguments, null);
return functionNoSuchMethod(function, arguments, null);
}
arguments = new List.from(arguments);
for (int pos = argumentCount; pos < maxArgumentCount; pos++) {
Expand Down
4 changes: 4 additions & 0 deletions sdk/lib/_internal/compiler/js_lib/shared/embedded_names.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ enum JsGetName {
GETTER_PREFIX,
SETTER_PREFIX,
CALL_PREFIX,
CALL_PREFIX0,
CALL_PREFIX1,
CALL_PREFIX2,
CALL_PREFIX3,
CALL_CATCH_ALL,
REFLECTABLE,
CLASS_DESCRIPTOR_PROPERTY,
Expand Down

0 comments on commit 14ddfb7

Please sign in to comment.