Add readonly to members in Rect#10156
Conversation
This allows making the fields private, which will allow making relevant members readonly for perf.
This allows the compiler to generate more optimized code.
|
Point, Size, Int32Rect, and Vector would benefit from the same treatment. All structs would, but commonly used ones will have more impact. I'll create these when I get the time, or if someone else feels compelled, ping me. :) |
|
One would think that compiler could figure out that |
It might. I haven't looked at this particular case. One could get a reasonable indication by looking at sharplab.io. In any case, better to be explicit. :) |
There was a problem hiding this comment.
What's the reasoning behind removing the generated Rect, is the plan to remove more generated code ? WpfGfx currently has knowledge of Rect and is the source of truth regarding it's definition, I'm not sure if moving the source of truth to PresentationCore and separate from WpfGfx is a good idea.
There was a problem hiding this comment.
I was going to ask about that too, and the process that generates them, does it get regenerated?
There was a problem hiding this comment.
It gets generated by MilCodeGen which needs to be ran manually (Though right now we can't run it since it's been broken since WPF was open sourced but it should be fixed by #6135).
I believe the entry that generates this file is this:
There was a problem hiding this comment.
The odds of actually breaking it are pretty slim. Is it worth leaving this easy perf on the table for this? I don't think so. Add whatever comments one needs to, but adding / changing field order is going to fail spectacularly anyway.
There was a problem hiding this comment.
My PRs for fixing MilCodeGen are now merged in main so MilCodeGen is now runnable.
Merging the PR as-is would mean that the next person to run MilCodeGen would recreate the generated Rect.cs. That leaves 3 option:
- Modify MilCodeGen to stop generating Rect.
- Modify this PR to keep the generated Rect and only add readonly to the non-generated members of Rect.
- Modify MilCodeGen to add readonly to the generated methods.
Personally, I'm not a fan of option 1. There's tons of files generated from MilCodeGen, I just don't see why Rect should be "special" (Even if it's easy perf).
IMO the best option is option 3 but if you don't want to bother with modifying MilCodeGen, you could do option 2 and I can do option 3 in a separate PR (We should probably add readonly to the 13 structs using the same MilCodeGen template). Even just option 2 is good improvement because a lot of the commonly used members are not defined in the generated Rect (X, Y, Size, etc).
There was a problem hiding this comment.
Also, I should also point out that MilCodeGen does not just generate the struct. It generates the struct, its type converter and its animation. We can stop generating only the struct but since the type converter and the animation uses members from the generated struct, I guess it makes it a bit less clean in my opinion.
There was a problem hiding this comment.
I am up for updating MilCodeGen to generate readonly fields.
There was a problem hiding this comment.
Yeah option 3 was on my radar as with other modifications.
Marking members as
readonlyallows the compiler to generate more efficient code.https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/struct#readonly-instance-members
This is in two commits. The first collapses code into a single file and the second adds the
readonly. Suggest looking at the second commit for the diff and do not squash the commits.Microsoft Reviewers: Open in CodeFlow