Add WithCookies overloads for IEnumerable key-value pairs.#866
Open
tranb3r wants to merge 1 commit intotmenier:devfrom
Open
Add WithCookies overloads for IEnumerable key-value pairs.#866tranb3r wants to merge 1 commit intotmenier:devfrom
tranb3r wants to merge 1 commit intotmenier:devfrom
Conversation
Contributor
Author
|
@tmenier Hi Todd, |
Owner
|
Sure thing. I guess you could say it's in maintenance mode at best right now, I just haven't had the time/energy to work on it in a while. I'll look at this one and try and release it soonish, though I'm going on vacation in a couple days so I may not get to it until I get back. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR proposes a minor change of how cookies are handled internally when using
WithCookies, with the goal of improving performance, reducing unnecessary conversions, and improving compatibility with NativeAOT scenarios.I did not open a separate issue, so I’m documenting the full motivation and design discussion here.
Avoid unnecessary reflection and double conversions
Currently, when
WithCookiesis called with anobject, Flurl relies onToKeyValuePairs, which uses reflection extensively.In practice, this leads to an inefficient flow:
IEnumerable<KeyValuePair<string, string>inApplyCookieJar.ToKeyValuePairsis then called, using reflection to convert it back into a collectionThis double conversion is unnecessary and adds avoidable reflection overhead.
The reflection cost is especially unfortunate given that the data is already in a strongly typed form.
This PR removes that inefficiency by:
NativeAOT compatibility (e.g. .NET MAUI)
The current
ToKeyValuePairsimplementation does not work in some NativeAOT scenarios (e.g. .NET MAUI with NativeAOT enabled).Because NativeAOT severely restricts or eliminates runtime reflection, this code path fails at runtime.
While NativeAOT is still experimental, it is clearly becoming more relevant, and reflection-heavy code should be avoided where possible.
I don’t have a minimal repro project to share, but I encountered this issue in a real scenario:
Rather than trying to “fix”
ToKeyValuePairsfor NativeAOT (which would be complex and fragile), this PR takes a simpler and more robust approach: avoid calling the untyped, reflection-based extension altogether when a typed alternative can be used.Public extension vs. private helper
This PR introduces a public, type-safe extension method.
An alternative would be to keep this logic private and only use it internally (e.g. in
ApplyCookieJar).However, I believe exposing a typed extension is beneficial:
That said, I’m open to discussion on this point if you’d prefer to keep the surface area smaller.
Summary
Feedback and suggestions are very welcome — especially regarding the public API choice.
Thanks for considering this change!