Skip to content
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

urls [nfc]: Pass around Uri objects more (cut out some unneeded re-stringification) #247

Merged
merged 3 commits into from
Aug 3, 2023

Conversation

chrisbobbe
Copy link
Collaborator

Helpful anyway, I think, but this came up when thinking about avatar URLs, for #119.

Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good; one nit below. In a commit message:

The most interesting consequence of this is that _LightboxHeroTag
now compares `src` using Uri's == operator override, instead of
string equality on the result of Uri.toString(). I expect that to
produce the same result, and at least as efficiently:
  https://github.com/dart-lang/sdk/blob/9bf4af004/sdk/lib/core/uri.dart

It looks like specifically that's this:

  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is Uri &&
        scheme == other.scheme &&
        hasAuthority == other.hasAuthority &&
        userInfo == other.userInfo &&
        host == other.host &&
        port == other.port &&
        path == other.path &&
        hasQuery == other.hasQuery &&
        query == other.query &&
        hasFragment == other.hasFragment &&
        fragment == other.fragment;

That looks slower to me than a string comparison — it's doing a bunch of separate string comparisons of different pieces. But it's fine, because this shouldn't be a hot path.

@@ -205,7 +205,7 @@ class _LightboxPageState extends State<_LightboxPage> {
Route getLightboxRoute({
required BuildContext context,
required Message message,
required String src
required Uri src
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
required Uri src
required Uri src,

(since we're here)

chrisbobbe added a commit to chrisbobbe/zulip-flutter that referenced this pull request Aug 3, 2023
The most interesting consequence of this is that _LightboxHeroTag
now compares `src` using Uri's == operator override, instead of
string equality on the result of Uri.toString(). Discussion:
  zulip#247 (review)
@chrisbobbe chrisbobbe force-pushed the pr-more-uri-objects branch from 3da8c7e to f8893ec Compare August 3, 2023 00:43
@chrisbobbe
Copy link
Collaborator Author

Thanks for the review! Revision pushed.

That looks slower to me than a string comparison — it's doing a bunch of separate string comparisons of different pieces.

I guess that makes sense. Each string comparison might come with some overhead, making it slower even if all the pieces add up to make the whole string.

There's also this implementation:

  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is Uri && _uri == other.toString();
  }

That's on class _SimpleUri implements Uri, whereas the one you quoted was on class _Uri implements Uri.

chrisbobbe added a commit to chrisbobbe/zulip-flutter that referenced this pull request Aug 3, 2023
The most interesting consequence of this is that _LightboxHeroTag
now compares `src` using Uri's == operator override, instead of
string equality on the result of Uri.toString(). Discussion:
  zulip#247 (review)
@chrisbobbe chrisbobbe force-pushed the pr-more-uri-objects branch from f8893ec to 94aa9d1 Compare August 3, 2023 20:41
@chrisbobbe
Copy link
Collaborator Author

Updated to change the type of RealmContentNetworkImageChecks.src, added in #246.

@gnprice
Copy link
Member

gnprice commented Aug 3, 2023

I guess that makes sense. Each string comparison might come with some overhead, making it slower even if all the pieces add up to make the whole string.

Right, exactly. And even if the overhead is negligible, it won't be negative ­— the multiple-strings version won't be faster.

There's also this implementation:

Hmm, yeah. Studying the parser a bit, it looks like most URLs we encounter are likely to end up as that _SimpleUri. (In particular _createTables describes which URLs the implementation is designed to treat as "simple".) That's good, then.

Still not any faster than the string comparison, as the heart of it is just the same string comparison.

This pushes the `toString` out to callers, so they can pass the
string form to whatever other code is asking for it.

...Actually, though, in all the cases where that other code has been
asking for the string form, it could easily be changed to accept the
Uri form. So we'll do that, coming up.
The most interesting consequence of this is that _LightboxHeroTag
now compares `src` using Uri's == operator override, instead of
string equality on the result of Uri.toString(). Discussion:
  zulip#247 (review)
@gnprice
Copy link
Member

gnprice commented Aug 3, 2023

Thanks! Looks good; merging.

@gnprice gnprice force-pushed the pr-more-uri-objects branch from 94aa9d1 to 135cec1 Compare August 3, 2023 22:10
@gnprice gnprice merged commit 135cec1 into zulip:main Aug 3, 2023
@chrisbobbe chrisbobbe deleted the pr-more-uri-objects branch August 3, 2023 22:10
@chrisbobbe
Copy link
Collaborator Author

Thanks!

@chrisbobbe
Copy link
Collaborator Author

Updated to change the type of RealmContentNetworkImageChecks.src, added in #246.

Hmm, did you accidentally clobber this part in the revision you merged?

RealmContentNetworkImageChecks should read:

extension RealmContentNetworkImageChecks on Subject<RealmContentNetworkImage> {
  Subject<Uri> get src => has((i) => i.src, 'src');
  // TODO others
}

(with Subject<Uri>, not Subject<String>).

@gnprice
Copy link
Member

gnprice commented Aug 3, 2023

Hmm, did you accidentally clobber this part in the revision you merged?

Oops, yeah. I must have not re-fetched after your last revision, and then not rerun tests after rebase. Please go ahead and send a fix, sorry.

chrisbobbe added a commit to chrisbobbe/zulip-flutter that referenced this pull request Aug 3, 2023
This was accidentally clobbered in the merge of zulip#247; see
  zulip#247 (comment)
@chrisbobbe
Copy link
Collaborator Author

Makes sense; just sent #257.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants