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

Don't use runtime reflection to read from data models (use source generation or IL code instead) #695

Open
4 tasks
ivarne opened this issue Jun 23, 2024 · 0 comments
Labels
feature Label Pull requests with new features. Used when generation releasenotes kind/chore quality/debt status/triage
Milestone

Comments

@ivarne
Copy link
Member

ivarne commented Jun 23, 2024

Description

Runtime reflection is known to be very slow, and with validation we use lots of it to be able to read properties of dynamic objects. Generated code that looks like this would be way faster

public class FieldAccessor<T>
{
    public object? GetValue(T root, string path, int[] indexes)
    {
        return path switch
        {
            "name" => root.Name,
            "children.name" => root.Children?[indexes[0]]?.Name,
            ///...
        }
    }
    
    public void SetValue(T root, object? value, string path, int[] indexes)
    {...}
}

Additional Information

There are two common patterns to generate code in C#

Source Generation

  • Pro
    • Actual source code is generated, which might make debugging easer, as it is possible to see the generated code while debugging.
    • Errors can be detected and notified at compile time.
    • Most performant, as it does not add to startup time.
  • Con
    • Tooling support not always the best.

IL generated code

  • Pro
    • Fully runtime solution, no dependencies on build
  • Con
    • First run will be slower in a new server app instance.
    • Harder to debug (no source will be available), but how likely are there to be issues in an auto generated property accessor.

Tasks

  • select SourceGenerator or IL generator
  • write a method that uses reflection/sg equivalent to get a list of all json style accessors for all properties to use in the switch/case (respecting [JsonPropertyName], and supporting recursive types)
  • Add the generated code
  • Use the new field accessors everywhere.

Acceptance Criterias

Use Source generation or cached IL generated code to access properties in data models using strings.

@ivarne ivarne added feature Label Pull requests with new features. Used when generation releasenotes quality/debt kind/chore labels Jun 23, 2024
@ivarne ivarne added this to the 9.0.0 milestone Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Label Pull requests with new features. Used when generation releasenotes kind/chore quality/debt status/triage
Projects
Status: No status
Development

No branches or pull requests

1 participant