Skip to content

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

Merged
merged 3 commits into from
Dec 15, 2022
Merged

Lambdas cleanup #1995

merged 3 commits into from
Dec 15, 2022

Conversation

adamsitnik
Copy link
Member

  • don't create unnecessary lambdas (based on feedback from @KalleOlaviNiemitalo)
  • Option ctor simplification (Argument ctor will throw for null anyway)
  • avoid closure allocations

@adamsitnik adamsitnik requested a review from jozkee December 15, 2022 16:03
@KalleOlaviNiemitalo
Copy link

(based on feedback from @KalleOlaviNiemitalo)

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();
Copy link
Member

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?

Copy link
Member Author

@adamsitnik adamsitnik Dec 15, 2022

Choose a reason for hiding this comment

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

What we had was:

  1. calling Path.GetInvalidPathChars() for every AcceptLegalFilePathsOnly call
  2. storing its result in local variable (not a static field of the class, which would allow for caching the value)
  3. 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)

Copy link

@KalleOlaviNiemitalo KalleOlaviNiemitalo Dec 15, 2022

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.

@adamsitnik adamsitnik merged commit edd256b into dotnet:main Dec 15, 2022
@adamsitnik adamsitnik deleted the lambdas branch December 15, 2022 18:30
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