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

Multi-address client: optimize client selection #2979

Merged
merged 8 commits into from
Jun 19, 2024

Conversation

idelpivnitskiy
Copy link
Member

@idelpivnitskiy idelpivnitskiy commented Jun 18, 2024

Motivation:

selectClient attracted my attention after I saw it can take up to 10% on the flame graph.

Modifications:

  • Change CachingKeyFactory to UrlKeyFactory;
  • Remove an extra layer of caching for UrlKeys;
  • Eagerly compute UrlKey.hashCode because it's used >1 time;
  • Remove allocation of HostAndPort from the hot path because it's required only to create a new client once;

Result:

Client selection reduced from 63.788 ± 1.268 ns/op to 14.512 ± 0.233 ns/op.


I tried a few different approaches. All of them are shown here as independent commits.

Benchmark                                                    (lowercase)  Mode  Cnt   Score   Error  Units
Original                                                            true  avgt    5  63.788 ± 1.268  ns/op
Original                                                           false  avgt    5  64.920 ± 0.517  ns/op
v1: UrlKey caches hashCode                                          true  avgt    5  44.918 ± 0.685  ns/op
v1: UrlKey caches hashCode                                         false  avgt    5  45.352 ± 0.510  ns/op
v2: remove urlKeyCache, key as a string                             true  avgt    5  81.459 ± 1.087  ns/op
v2: remove urlKeyCache, key as a string                            false  avgt    5  82.457 ± 1.015  ns/op
v3: remove urlKeyCache, key as UrlKey                               true  avgt    5  19.767 ± 0.350  ns/op
v3: remove urlKeyCache, key as UrlKey                              false  avgt    5  19.813 ± 0.048  ns/op
v4: remove urlKeyCache, key as UrlKey, don't allocate HostAndPort   true  avgt    5  14.512 ± 0.233  ns/op
v4: remove urlKeyCache, key as UrlKey, don't allocate HostAndPort  false  avgt    5  14.808 ± 0.324  ns/op

Copy link
Contributor

@bryce-anderson bryce-anderson left a comment

Choose a reason for hiding this comment

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

I like the simplification, that alone is worth it for me.

I am curious about the benchmarks: are they already committed somewhere or are they local? I'm specifically curious whether a lot of the benefit comes from the port being the first thing checked if the ports are set as random numbers vs the most common 80/443.

@idelpivnitskiy
Copy link
Member Author

I am curious about the benchmarks: are they already committed somewhere or are they local?

See commits in this PR. I added the benchmark and removed it later because it required some internal changes for multi-address client builder.

I'm specifically curious whether a lot of the benefit comes from the port being the first thing checked if the ports are set as random numbers vs the most common 80/443.

I used a fixed port bcz int check seems trivial to make any difference.

Copy link
Contributor

@bryce-anderson bryce-anderson left a comment

Choose a reason for hiding this comment

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

Looks good after you address the spotbugs complaints regarding the omission of the type and null checking in UrlKey.equals(..). (I don't care if they're ignored or added back.)

@idelpivnitskiy
Copy link
Member Author

Verified with the benchmark that it doesn't make any difference, added them back

@idelpivnitskiy idelpivnitskiy merged commit 7f8a7f4 into apple:main Jun 19, 2024
11 checks passed
@idelpivnitskiy idelpivnitskiy deleted the selectClient branch June 19, 2024 17:13
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.

3 participants