Open OJacot-Descombes opened 2 years ago
SKMatrix is not a read-only structure, passing through with "in" modifier will create a shadow copy of the structure...
@AnarchyMob, this is not how the in-keyword works in C#. It passes a reference to the original struct but disallows assignments to it:
See also the link in the "Additional context" section of my post. The Microsoft's doc clearly says
The in keyword causes arguments to be passed by reference but ensures the argument is not modified.
@OJacot-Descombes
The in keyword causes arguments to be passed by reference but ensures the argument is not modified.
True, but the compiler is often forced to emit a defensive copy to ensure this.
Without the readonly
modifier the compiler cannot know that calling a method of m
(or even a get accessor) would not mutate the struct in some way. Therefore it would be unsafe to pass a reference to the original struct.
https://learn.microsoft.com/en-us/dotnet/csharp/write-safe-efficient-code#avoid-defensive-copies
OK, I learned something. But luckily there are Readonly Instance Members. You can apply readonly
to property accessors and to methods.
private int _x;
public int X { readonly get { return _x; } set { _x = value; } }
I wouldn't mind at least being able to pass a ref to skia as that's a lot of structure copying as I'm saving and restore the matrix a lot.
Problem
As an example,
SKCanvas.Concat
is declared aspublic void Concat (ref SKMatrix m)
. This does not let me writeI get
I have to create a local copy of the
SKMatrix
struct first. But copying the struct is precisely what theref
keyword is supposed to avoid and is not good for performance.Proposed solution
Replace the
ref
keyword with thein
keyword (in all methods where this applies).Additional context
The documentation for in parameter modifier (C# Reference) says