Skip to content

Commit 11ea8c9

Browse files
browsquare-tombstudio
authored
perf(mobile): skip avatar URL parsing for non-HTTP URLs (#9448)
Every avatar build checks its URL: `parseAnimatedAvatarUrl` scans for the `#buzz-anim=` fragment and parses both halves, and `usesInsetAgentArtwork` also runs `Uri.tryParse` and a regex. For an ordinary URL that's a few microseconds. Some profile pictures are `data:image/png;base64,...` URLs, though, and those cost about 0.7 ms per check for 100 KB on a Mac, because the fragment search and `Uri` normalization read the whole string. Channel screens, the native navigation bar payload and the DM header portrait run these checks on every build, so a channel with such members pays for them frame after frame. Both functions can only answer yes for an HTTP(S) URL, so they now first look at the first six characters and return early unless the URL starts with `http:` or `https:` (in any case). A data URL is turned away without reading the rest of it. The authority is still checked by `Uri` as before, which also accepts backslashes there (`https:\\host`). ### Validation Added test pinning the answers this check must keep: an `https://` preset poster is inset artwork in any case and with backslash authorities (`https:\\`, `https:/\`, `https:\/`), while `http://`, a leading space, a data URL, an empty string and null are not; an animated avatar URL still is. Existing animated avatar tests (including a data-URL poster with a `#buzz-anim=` fragment) pass; full mobile test suite and `flutter analyze` pass. On an iPhone 12 mini signed in to Block's Builderlab (instrumented profile build), opening a channel whose members include two data-URL avatars (100 KB and 150 KB): over the first 7 seconds, the CPU profile spent 69 ms in these checks before this change and none with it (0 of 647 samples). ### Regressions None expected. Every URL the new check turns away already got the same answer before: `_isHttpUrl` and the preset check both need scheme `http` or `https`, which `Uri` only reports for a string that starts with `http:` or `https:` (it lowercases the scheme, so case doesn't matter, and a leading space already parsed to no scheme). Reviewers compared the old and new functions on several thousand generated URLs (case, whitespace, IDN, punycode, backslash forms) and found no differences. --------- Signed-off-by: Tom Brow <[email protected]> Co-authored-by: Tom Brow <[email protected]> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
1 parent 5e52fa9 commit 11ea8c9

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎mobile/lib/shared/animated_avatar.dart‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ String buildAnimatedAvatarUrl(String posterUrl, String animationUrl) =>
3131
/// Returns `null` when the poster or animation URL is missing, malformed, or
3232
/// does not use HTTP(S).
3333
AnimatedAvatarDescriptor? parseAnimatedAvatarUrl(String? url) {
34-
if (url == null || url.isEmpty) return null;
34+
if (url == null || !_startsWithHttp(url)) return null;
3535

3636
final separatorIndex = url.indexOf(_animatedAvatarSeparator);
3737
if (separatorIndex <= 0) return null;
@@ -58,6 +58,15 @@ AnimatedAvatarDescriptor? parseAnimatedAvatarUrl(String? url) {
5858
);
5959
}
6060

61+
/// Whether [url] has an HTTP(S) scheme, checked without reading the rest of
62+
/// it. Avatars are checked on every build, and a `data:` profile picture of
63+
/// 100 KB costs about a millisecond to scan and parse. The authority is left to
64+
/// `Uri`, which also accepts backslashes there.
65+
bool _startsWithHttp(String url) {
66+
final head = url.substring(0, url.length < 6 ? url.length : 6).toLowerCase();
67+
return head.startsWith('http:') || head.startsWith('https:');
68+
}
69+
6170
bool _isHttpUrl(String value) {
6271
final uri = Uri.tryParse(value);
6372
return uri != null &&
@@ -68,8 +77,9 @@ bool _isHttpUrl(String value) {
6877
/// Character presets can be saved as plain poster URLs without buzz-anim.
6978
/// Keep photographs and ordinary emoji avatars at their existing size.
7079
bool usesInsetAgentArtwork(String? url) {
80+
if (url == null || !_startsWithHttp(url)) return false;
7181
if (parseAnimatedAvatarUrl(url) != null) return true;
72-
final uri = url == null ? null : Uri.tryParse(url);
82+
final uri = Uri.tryParse(url);
7383
return uri != null &&
7484
uri.scheme == 'https' &&
7585
uri.host == 'dwwgwmfqqjotj.cloudfront.net' &&

‎mobile/test/shared/animated_avatar_test.dart‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,25 @@ void main() {
4040
isNull,
4141
);
4242
});
43+
44+
test('only HTTP(S) URLs, in any case, can be preset artwork', () {
45+
const preset = 'dwwgwmfqqjotj.cloudfront.net/avatars/a/poster/b/c.png';
46+
expect(usesInsetAgentArtwork('https://$preset'), isTrue);
47+
expect(usesInsetAgentArtwork('HTTPS://$preset'), isTrue);
48+
expect(usesInsetAgentArtwork(r'https:\\' + preset), isTrue);
49+
expect(usesInsetAgentArtwork(r'https:/\' + preset), isTrue);
50+
expect(usesInsetAgentArtwork(r'https:\/' + preset), isTrue);
51+
expect(usesInsetAgentArtwork('http://$preset'), isFalse);
52+
expect(usesInsetAgentArtwork(' https://$preset'), isFalse);
53+
expect(
54+
usesInsetAgentArtwork('data:image/png;base64,${'A' * 1000}'),
55+
isFalse,
56+
);
57+
expect(usesInsetAgentArtwork(''), isFalse);
58+
expect(usesInsetAgentArtwork(null), isFalse);
59+
expect(
60+
usesInsetAgentArtwork(buildAnimatedAvatarUrl(posterUrl, animationUrl)),
61+
isTrue,
62+
);
63+
});
4364
}

0 commit comments

Comments
 (0)