-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request to optimize js_util.callMethod when using const args (dart2js). #41617
Comments
The example here is a little different as the receiver is a primitive type, which might not be supported via For the specific case of |
@rakudrama, we convert Flutter Color to css color for canvas apis quite a bit. to profile , run the following colorToCssString(const Color(0xFF00FFFF) in a tight loop: /// Converts color to a css compatible attribute value.
String colorToCssString(ui.Color color) {
if (color == null) {
return null;
}
final int value = color.value;
if ((0xff000000 & value) == 0xff000000) {
return _colorToCssStringRgbOnly(color);
} else {
final double alpha = ((value >> 24) & 0xFF) / 255.0;
final StringBuffer sb = StringBuffer();
sb.write('rgba(');
sb.write(((value >> 16) & 0xFF).toString());
sb.write(',');
sb.write(((value >> 8) & 0xFF).toString());
sb.write(',');
sb.write((value & 0xFF).toString());
sb.write(',');
sb.write(alpha.toString());
sb.write(')');
return sb.toString();
}
}
/// Returns the CSS value of this color without the alpha component.
///
/// This is useful when painting shadows as on the Web shadow opacity combines
/// with the paint opacity.
String _colorToCssStringRgbOnly(ui.Color color) {
final int value = color.value;
final String paddedValue = '00000${value.toRadixString(16)}';
return '#${paddedValue.substring(paddedValue.length - 6)}';
} |
I did some investigation and we discussed this off-line. The performance characteristics of this code on Chrome is dominated by
The overhead of the extra checks on the path in the compiled code contribute relatively little to the run time (<5%) The original code calls The referenced change flutter/engine#17866 gains 25% mostly by masking the input to These performance characteristics to be peculiar to V8. JSC (Safari) and SM (Firefox) do better.
@verwaest @mathiasbynens @hannespayer V8 is slower than JSC and SM. We would love to get the 2x better performance that SM shows is possible since this code is hot in some Flutter web apps. |
Thanks for reporting! @LeszekSwirski will have a look. |
The 13 magic number thing is kind-of a KI, but all heuristics are bad at some point 🤷. I think asking devs to avoid unnecessary concatenations is a fair here. Looks like for radix 16 we do fall down the Number.prototype.toString slow path, which we probably don't have to for small integers. I've filed a V8 bug for it: https://crbug.com/v8/10477 As an aside, you may also consider a version 2+3, where you mask the integer with 0x1FFFFFF to maintain a known prefix (like in v2) but allow the value to be a small tagged integer (like in v3). |
Ran across while trying to optimize very low level Flutter web class.
instead of generating.
someValue.toString(16);
, checks type of someValue and calls someValue.apply(....) with array.The text was updated successfully, but these errors were encountered: