dart-lang / dart-lang/language

Implicit templates for performance guarantees

Open
#662 1 comment 2 reactions 0 assignees View on GitHub
request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

A polymorphic call inside a hot loop is usually suboptimal. Consider this example:
```dart
import 'dart:typed_data';

void main() {
// Create a list of lists of alternating types
final data = List>(1024);
for (var i = 0; i < data.length; ++i) {
if (i.isEven) {
data[i] = Uint32List(1024);
} else {
data[i] = Float32List(1024);
}
}

for (var i = 0; i < data.length; i++) {
processListPolymorphic(data[i]);
processListExpanded(data[i]);
}
}

void processListPolymorphic(List list) {
for (var i = 0; i < list.length; i++) {
// runtime type of value switches between int and double
final value = list[i];

// multiplication and comparison switch between ints and doubles
if (value * value > 10) {
break;
}
}
}

void processListExpanded(List list) {
if (list is List) {
for (var i = 0; i < list.length; i++) {
final value = list[i]; // always int
if (value * value > 10) { // always int
break;
}
}
} else if (list is List) {
for (var i = 0; i < list.length; i++) {
final value = list[i]; // always double
if (value * value > 10) { // always double
break;
}
}
}
}
```

Here, the loop inside `processListPolymorphic` is always polymorphic at runtime and performs worse than the manually expanded version in `processListExpanded`.

If it's impractical for the compilers to speculatively duplicate and specialize loops with polymorphic calls, maybe a language feature (like somehow enhanced generics) could be introduced to give some guarantees.

Contributor guide

Open the contributing guide

Research direction

Start with the polymorphic and manually expanded examples in the issue; no repository files, tests, or entry points are identified. Determine whether the language or compiler should provide guarantees for specializing polymorphic hot loops, and define a concrete proposal and performance criteria before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.