-
Notifications
You must be signed in to change notification settings - Fork 401
Lambdas cleanup #1995
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
Lambdas cleanup #1995
Conversation
adamsitnik
commented
Dec 15, 2022
- don't create unnecessary lambdas (based on feedback from @KalleOlaviNiemitalo)
- Option ctor simplification (Argument ctor will throw for null anyway)
- avoid closure allocations
I guess you mean #1968 (comment). |
@@ -210,10 +210,10 @@ void UnrecognizedArgumentError(ArgumentResult argumentResult) | |||
/// <returns>The configured argument.</returns> | |||
public Argument<T> AcceptLegalFilePathsOnly() | |||
{ | |||
var invalidPathChars = Path.GetInvalidPathChars(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't this meant to cache Path.GetInvalidPathChars()
which returns a new char[] on each call?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What we had was:
- calling
Path.GetInvalidPathChars()
for everyAcceptLegalFilePathsOnly
call - storing its result in local variable (not a static field of the class, which would allow for caching the value)
- allocating a closure (a lambda with a state)
What I am offering here is lack of closure allocation. I would prefer to avoid caching the result of Path.GetInvalidPathChars()
in a static field as long as profiling don't prove that it's needed (adding static fields can hurt startup scenarios, it's not always a free lunch)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each Argument<T>.AcceptLegalFilePathsOnly call created a separate cache, so it was only effective if the same Argument<T> was validated multiple times, which I think would only happen if the application reads and parses multiple commands. If the application has subcommands, the eager Path.GetInvalidPathChars() call could be wasteful because it happens even if the subcommand is not invoked.
If caching is desired here, I think a static field in a non-generic class could work.