Skip to content

RedirectRule: opportunity for performance improvement #28883

Closed

Description

@paulomorgado

Summary

  1. RedirectRule.ApplyRule is comparing the request path with PathString.Empty, which ends up being a string comparison.
  2. RedirectRule.ApplyRule is boxing a PathString(1)(2).

Motivation and goals

This is in a hot path for applications that use redirections.

Detailed design

  1. Use path.HasValue instead.
  2. Use pathBase.ToUriComponent() instead.
    • A better option would be use UriHelper.BuildRelative. The current implementation doesn't encode the path; only the path base and the query string.

Activity

  1. added
    design-proposalThis issue represents a design proposal for a different issue, linked in the description
    on Dec 28, 2020
  2. Tratcher commented on Dec 28, 2020

    @Tratcher
    Member

    Have you done any profiling here? This is a class of issue that's unlikely to be addressed unless it shows up as significant in a CPU or memory profile.

    1. HasValue is a similar string comparison (string.IsNullOrEmpty).

      public bool HasValue
      {
      get { return !string.IsNullOrEmpty(Value); }

    2. Are you sure about the boxing? PathString has an implicit string converter that may kick in first. It also overrides the + operator.

      public static implicit operator string(PathString path)
      => path.ToString();

      public static string operator +(PathString left, string? right)
      {
      // This overload exists to prevent the implicit string<->PathString converter from
      // trying to call the PathString+PathString operator for things that are not path strings.
      return string.Concat(left.ToString(), right);
      }

  3. added
    Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.
    on Dec 28, 2020
  4. paulomorgado commented on Dec 28, 2020

    @paulomorgado
    ContributorAuthor

    If you follow the PathString equality operator, you'll see that it's more than just string.IsNullOrEmpty. And then there's consistency with the rest of the codebase.

    You're right about the string.Concat overload being used. It's string.Concat(string, string, string) - there are overloads up to 4 string arguments and the C# compiler prefers the implicit conversion and the overload with string argumetns.

    However, the PathString addition operator doesn't apply because the type of the next argument is a sting, not a PathString. It should be, because it's not being encoded like the rest of the components.

  5. ghost added
    Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.
    on Dec 28, 2020
  6. ghost removed
    Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.
    on Dec 28, 2020
  7. Tratcher commented on Dec 28, 2020

    @Tratcher
    Member

    However, the PathString addition operator doesn't apply because the type of the next argument is a sting, not a PathString. It should be, because it's not being encoded like the rest of the components.

    It also has an overload for PathString + string.

  8. paulomorgado commented on Dec 28, 2020

    @paulomorgado
    ContributorAuthor

    Got it!

    But that means that

    pathBase + newPath.Substring(0, split) + query.ToUriComponent()
    

    will end up being

    string.Concat(string.Concat(pathBase.ToUriComponent(), newPath.Substring(0, split)), query.ToUriComponent())
    

    Right?

    One more string than it needs to be. If we accept that newPath.Substring(0, split) is unavoidable.

  9. Tratcher commented on Dec 28, 2020

    @Tratcher
    Member

    Fair point. As you said, UriHelper.BuildRelative would address most of this.

  10. paulomorgado commented on Dec 29, 2020

    @paulomorgado
    ContributorAuthor

    Are there any unit tests for this? If so, where?

  11. paulomorgado commented on Dec 29, 2020

    @paulomorgado
    ContributorAuthor

    Found! MiddlewareTests.cs

  12. added this to the Backlog milestone on Dec 30, 2020
  13. ghost locked as resolved and limited conversation to collaborators on May 6, 2021
  14. added
    area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewares
    and removed on Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.Perfarea-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewaresdesign-proposalThis issue represents a design proposal for a different issue, linked in the description

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions