Eliminate dynamic calls when getting the environment.
Statically type String variables to avoid dynamic calls for substring or concatenation.
Change-Id: If485243dc0580fd1f4e53ec4cf69cb3819a57b5d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255301
Reviewed-by: Leaf Petersen <leafp@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
diff --git a/pkg/compiler/test/analyses/api_allowed.json b/pkg/compiler/test/analyses/api_allowed.json
index 645ff86..96aac80 100644
--- a/pkg/compiler/test/analyses/api_allowed.json
+++ b/pkg/compiler/test/analyses/api_allowed.json
@@ -69,12 +69,6 @@
"org-dartlang-sdk:///lib/io/link.dart": {
"Dynamic invocation of '[]'.": 3
},
- "org-dartlang-sdk:///lib/io/platform_impl.dart": {
- "Dynamic invocation of 'indexOf'.": 1,
- "Dynamic invocation of '>'.": 1,
- "Dynamic invocation of 'substring'.": 2,
- "Dynamic invocation of '+'.": 1
- },
"org-dartlang-sdk:///lib/io/secure_server_socket.dart": {
"Dynamic update to 'dart.io::_owner'.": 1
},
diff --git a/sdk/lib/io/platform_impl.dart b/sdk/lib/io/platform_impl.dart
index 48d89ef..0fca80f 100644
--- a/sdk/lib/io/platform_impl.dart
+++ b/sdk/lib/io/platform_impl.dart
@@ -81,24 +81,29 @@
static Map<String, String> get environment {
if (_environmentCache == null) {
var env = _environment();
- if (env is! OSError) {
+ if (env is Iterable<Object?>) {
var isWindows = operatingSystem == 'windows';
var result = isWindows
? new _CaseInsensitiveStringMap<String>()
: new Map<String, String>();
- for (var str in env) {
- if (str == null) {
+ for (var environmentEntry in env) {
+ if (environmentEntry == null) {
continue;
}
+ // TODO(kallentu): [_environment()] emits Iterable<dynamic> which is
+ // why the cast and check is needed. Every element is a String,
+ // however, so refactor [_environment()] at some point to emit
+ // Iterable<String>s instead.
+ var text = environmentEntry as String;
// The Strings returned by [_environment()] are expected to be
// valid environment entries, but exceptions have been seen
// (e.g., an entry of just '=' has been seen on OS/X).
// Invalid entries (lines without a '=' or with an empty name)
// are discarded.
- var equalsIndex = str.indexOf('=');
+ var equalsIndex = text.indexOf('=');
if (equalsIndex > 0) {
- result[str.substring(0, equalsIndex)] =
- str.substring(equalsIndex + 1);
+ result[text.substring(0, equalsIndex)] =
+ text.substring(equalsIndex + 1);
}
}
_environmentCache = new UnmodifiableMapView<String, String>(result);