Class VS ref Struct
Asked Answered
S

2

13

I am programming a game using C#, thus, I am very concerned about performance.

I would like to know what are the main differences, and if possible, performance considerations of using either a Class to pass data around, or a struct passed by reference.

I wish not to copy the data around, for performance reasons (I assume passing by ref is much faster than by value here).

I know that a class is always passed by reference and that a struct is passed by value, but I talking about passing the struct by reference here.

An example of the data I wish to pass :

    public delegate void PathCompleteDelegate(List<PathFinderNode> path);
public struct PathFinderJob{
    public PathCompleteDelegate callback;
    public Vector3 start, end;
    public PathSize unitSize;
    public BoxCollider boxCollider;
}

In the previous example, would using a class make a difference? If so, what would the difference be? Would a class be faster than a struct in this example? Why?

Thank you. Joao Carlos

Septum answered 29/3, 2012 at 10:9 Comment(3)
Have you tried performance profiling both approaches? If the struct is scoped outside of a method it will be on the heap anyway (as in, contained within a class on the heap). I would suspect class or ref struct to be the same performance in terms of method calls.Downstroke
I dont have much experience profiling, and I did a quick profiling check using Unity3D's built in profiler, but, I did not see a difference, however, I dont know how good the built in profiler is with this type of "small fish", or if it would see a difference at all among all the other game related profiling happening.Septum
I wouldn't waste my time on structs while programming a game in a OO language. The problem I would face before getting performance problems is not finishing the game. Classes and objects increase the dev speed IMO.Inventor
Z
3

Your delegate receives a reference type - a List, so you're passing the entire list by reference anyway.

Passing a large structure by value is definitely most expensive than passing just the reference. When you have a large structure, it usually doesn't make sense to use it as a structure, just turn it into a class.

Anyway, are you sure you'll have a performance issue here? Seems like a very premature optimization.

Zincograph answered 29/3, 2012 at 10:13 Comment(3)
I am programming the game and also learning my way around writing faster code. That exact step isnt much of a problem, because at most, there wont be more than 1000 pathfinding requests per second (I assume). But in the pathfinders node structure, this could very well scale up to 100nodes*1000pathFindingRequests, and learning about this probably help there.Septum
Dereferencing 100 nodes 1000 times - that is, 100,000 dereferences, a second shouldn't pose any kind of trouble.Zincograph
And if it turns out that his game is lagging, profiling would probably reveal some single line of code that's responsible. Computers these days are very fast indeed.Reluctance
S
13

I know that a class is always passed by reference and that a struct is passed by value, but I talking about passing the struct by reference here.

You probably have the right idea, but this is incorrect. Everything in C# is passed by value unless you use the ref keyword.

Class instances are reference types, struct instances are value types.

When you pass a reference type by value, you pass a copy of the reference (small). When you pass a value type by value, you pass a copy of the whole data (potentially large).

Jon Skeet has a good explanation of all this here.

Sard answered 29/3, 2012 at 10:20 Comment(0)
Z
3

Your delegate receives a reference type - a List, so you're passing the entire list by reference anyway.

Passing a large structure by value is definitely most expensive than passing just the reference. When you have a large structure, it usually doesn't make sense to use it as a structure, just turn it into a class.

Anyway, are you sure you'll have a performance issue here? Seems like a very premature optimization.

Zincograph answered 29/3, 2012 at 10:13 Comment(3)
I am programming the game and also learning my way around writing faster code. That exact step isnt much of a problem, because at most, there wont be more than 1000 pathfinding requests per second (I assume). But in the pathfinders node structure, this could very well scale up to 100nodes*1000pathFindingRequests, and learning about this probably help there.Septum
Dereferencing 100 nodes 1000 times - that is, 100,000 dereferences, a second shouldn't pose any kind of trouble.Zincograph
And if it turns out that his game is lagging, profiling would probably reveal some single line of code that's responsible. Computers these days are very fast indeed.Reluctance

© 2022 - 2024 — McMap. All rights reserved.