C #: Should I use or revert this structure back?

I'm not sure how best to ask this question, so I'll start with an example:

public static void ConvertPoint(ref Point point, View fromView, View toView) { //Convert Point } 

This call is recursive. You pass a point, it converts it relative to fromView into a toView (as long as one is the ancestor of the other).

The call is recursive, while converting one level. I know mutable structures are bad, but the reason I use a mutable point is that I only need to create one point and pass it on a recursive call, so I use ref. Is this correct, or would it be better to use the out parameter, or just declare a method to return a point instead?

I am not very good at how structures are processed, not classes in these circumstances. This is code that is ported from Java, where the point must obviously be a class, so it makes sense to use the same time point over and over again, rather than creating a new point, which should be garbage collected on every call.

This may be a confusing question, and so that there is a bunch of some more confusion while I am, should I store a temporary static instance of Point for quick conversions, or would it be just as easy to create a new one when this method is called (it is called a lot)?

+4
source share
5 answers

Fretting on the garbage collector is never a mistake when working with short objects such as Point, provided that it is really a class. Given that this is C #, this is most likely a structure, not more than 16 bytes. In this case, you should always write a method to return the point. This is optimized at runtime, the structure fits into the processor registers.

Only someday consider passing struct by ref when they are big.

+8
source

I try to do everything to simplify things. If you find that this creates undesirable memory pressure in your application, then you should investigate a solution that includes a modified structure.

The reason I'm talking about is because GC is generally very good at optimizing collections around the needs of your application, and volatile structures are a notorious headache. Do not introduce this headache if the GC can handle the memory pressure of several objects.

+1
source

Depending on what you do and why you do it.

Structures in C # are not like classes in Java at all . They are similar to primitive types in terms of speed and semantics. (Actually, primitive types are structures in a sense.) Actually, there is no cost to pass them, so if it optimizes for something that is not a problem, I suggest you simply return the Point method.

However, it is possible that in some cases the transfer of a copy of the structure will be slower if the struct large or this method is a bottleneck, possibly because you call it many times per second (which I doubt it is); in these cases it makes sense to pass them by reference, but first you need to find out if this is really a bottleneck in the first place.

Note, for example, that in the Microsoft XNA library, the Matrix structure has two versions of the same method:

 static void CreateLookAt( ref Vector3 cameraPosition, ref Vector3 cameraTarget, ref Vector3 cameraUpVector, out Matrix result) static Matrix CreateLookAt( Vector3 cameraPosition, Vector3 cameraTarget, Vector3 cameraUpVector) 

This is purely for performance reasons, but only because this method is called many times per second, and Vector3 slightly larger than a regular struct .

So why exactly did you do this? The answer depends on why you wrote it like this in the first place, but it does not matter for typical code.

+1
source

Structures must be immutable, so I would vote to return another point from the method. If you need mutability, then you have to make it a class and create a method for the class to convert it. A by-product of the latter may be a change in recursive descent to iteratively apply transformations.

+1
source

Since a dot is a structure, I see no advantage when using ref here. It will be copied several times on the stack, so why not just return it? I think this will make the code more readable if you declare this method with Point as a return type.

0
source

Source: https://habr.com/ru/post/1346444/


All Articles