structs vs. classes for geometry data

Started by
7 comments, last by Max Piano 17 years, 4 months ago
Hi, I would like to know about your experiences concerning the difference in perfomance when using classes instead of structs? I wonder whether I should represent things like vertices, planes and so on through structs and performing operations like dot-product, intersection tests and the like by passing pointer to those structs to static functions of a utility class. How significant do you think would be the perfomance loss/memory consumption when representing the geometry data throug classes with respective member functions instead? In my particular application, there could be tens of thousands of planes for example... Thanks in advance!
Advertisement
First of all in c++ there is no inherent speed difference between struct and class, they are exactly the same except for default access method - class is private, struct is public.
The speed difference between calling a global function/static function and a class function is 0 (unless the function is virtual).
Quote:Original post by stevenmarky
The speed difference between calling a global function/static function and a class function is 0 (unless the function is virtual).


I though "this" was pushed before the call of a member function (and not for a static class or global function call).
-----Entropia 3D Engine Project, a next-gen and flexible C# 3D Engine under GPL.
Quote:I though "this" was pushed before the call of a member function (and not for a static class or global function call).

Correct, so it's not exactly the same!
I.e
class Foo{public:  void bar(void)  {  }};void s_bar(void){}voidmain(){  Foo foo;  foo.bar();  // Needs to pass the this pointer to the method, i.e one variable is hidden  s_bar();}


That said, doing something more practical, like this:

class Foo{public:  void bar(void)  {    m_i = 0;  }  int m_i;};struct FooStruct{  int m_i;};void s_bar(FooStruct& foo){  foo.m_i = 0;}voidmain(){  Foo foo;  foo.bar();  FooStruct fooStruct;  s_bar(fooStruct);}

The above would probably genereate the exact same code for the two calls.
Note that the different between "class" and "struct" isn't the same in all languages. Assuming you were talking about C++ then the only different is default access, but in a language like C#, structs are value-types and classes are object-types, which forms a huge difference in performance and usage semantics.

For me, structs typically denote small data structures that exist just as a collection of data, without too many operations. For instance, if I want to pass a collection of particle information to some renderer, where each particle has a bunch of information regarding position, color, velocity, etc., I would use a struct simply because that means to me, "I exist to make storing multiple instances of this data convenient". I would probably use classes for everything else. That's just me though.
Quote:Original post by eq
Quote:I though "this" was pushed before the call of a member function (and not for a static class or global function call).

Correct, so it's not exactly the same!
I.e
*** Source Snippet Removed ***

That said, doing something more practical, like this:

*** Source Snippet Removed ***
The above would probably genereate the exact same code for the two calls.



Also, in the first example, if the method didn't reference 'this' then the compiler may well optimize it out.
Joshua Barczak3D Application Research GroupAMD
Quote:Original post by xen2
Quote:Original post by stevenmarky
The speed difference between calling a global function/static function and a class function is 0 (unless the function is virtual).


I though "this" was pushed before the call of a member function (and not for a static class or global function call).

Which, if you're using a global function instead, is replaced by an explicit pointer to the structure. If you didn't need the this pointer, you wouldn't have made it a non-static member function in the first place.
The OP needs to answer if they are asking about C++, .NET (probably C#), or another langauge.

In .NET, a struct is a value type and is bit-by-bit copied, potentially using more bandwidth for items more than 8 or so bytes in length ... and also potentially having greater usage speed for items small enough (perhaps less than 16 or even 64 bytes in length). So in general, if you desire value semantics in .NET, but need efficiency, use a value type for items up to 8 bytes, a reference type for items over 64 bytes, and do some profiling for items in between (I usually use value up to 16-20 bytes).

In C++, struct and class are irrelevant completely.

In C++ the choice is member functions, global functions with "this" equivelent" and global functions with no need for a "this" like pointer. The first 2 are identical if not virtual ... and the later is "faster" because in this case the algorithm obviously has no need for member data, and therefore shouldn't be a "member" .. therefore it should be "static"
If we are not talking about virtual member functions (I think this is not the case) there is no loss in terms of memory or performance.

I suggest you to take a look at the Unreal Public Source Code (sorry this is the first link I found) to see how a "real" math engine can be written... years ago it was usefull for me! See core/unmath.h for a mine of tips!

If you are concerned about speed, the real optimization is to implement expensive operations (for example matrix products) via assembly. But the container should be in pure Cpp if you intend to use this language.

This topic is closed to new replies.

Advertisement