Jump to content

  • Log In with Google      Sign In   
  • Create Account

* * * * * 1 votes

perfomances issues with classes in array


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 _Engine_   Members   -  Reputation: 195

Like
0Likes
Like

Posted 21 January 2013 - 02:22 AM

Hi!

 

In script we store data in angel script classes. Also we have array of classes.

 

Sample look like:

 

class StoreData

{

   int data1, data2;

   float data1, data2;

}

 

array<StoreData> data;

 

But big problem lie in fact that in memory 100 elements of array is just pointers. Actual data lies randomly in memory, so if we try to access to field data1 of 100 elements in loop we catch many many cache misses. Also we cannot binary save this array we must access to every field of every element in array.

 

So classes in array is big bottleneck.

 

Is it possible to avoid this issue?



Sponsor:

#2 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 21 January 2013 - 06:51 AM

Use a custom allocator and move data to be continuous when its time to resize the container.
Alternatively you could wrap std::vector. ISO standard dictates std::vector is guaranteed to be continuous.
I chose std::vector route.

edit:
In addition, AngelScript allows you to have your own custom malloc and free. This is not utilized in scriptarray addon.

Edited by saejox, 21 January 2013 - 06:55 AM.


#3 _Engine_   Members   -  Reputation: 195

Like
0Likes
Like

Posted 21 January 2013 - 08:35 AM

Thanks for reply!

 

But using custom allocator it is not solution. Script array already store data in one chunk. But script class itself allocated by AngelScript so array it is just array of pointers to angel script representation of class. I can not hack Angel Script to make allocate arrays of classes in one chunk.



#4 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 21 January 2013 - 09:12 AM

That's why i have recommended custom allocator.

AS_API int asSetGlobalMemoryFunctions(asALLOCFUNC_t allocFunc,	asFREEFUNC_t freeFunc)

http://www.angelcode.com/angelscript/sdk/docs/manual/angelscript_8h.html#a527ab125defc58aa40cc151a25582a31



#5 _Engine_   Members   -  Reputation: 195

Like
0Likes
Like

Posted 21 January 2013 - 10:04 AM

Hi!

 

Thanks to link leads to documentatios. But please before answer again try to understend what i want to achieve.

In c++ if i define array like this -

 

struct Data

{

    float a,b,c;

}

std::vector<Data> data;

 

when i go throught loop of each element of array access speed to element will be maximized because data lie in cache. If I repace array to

 

std::vector<Data*> data;

 

access to each element may leed to pоtention cache miss becase pointers in array may point at random places in memory. If accessed data lie all in one chank there still potentional chace miss.

 

cusom allocators from angel script is good but that mean write very complicated memory manager because i can call from script resize of array at any time and how my manager supose to understend that i must resize exetly that array? it is almost imposible without hacking angelscript.

 

i understend - angel script goes by way of sand box. But sand box means big perfomance problems. We currently work on mobile title and angel script is big big perfomance issue. I prefer get more control points to trade safety to speed.



#6 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 21 January 2013 - 10:54 AM

Sorry, i can't think anything other than custom allocators.

You can position your data any place that way. Won't be easy as you said.

There are many additional benefits using custom allocators: leak detection, memory statistics, defragmentation etc..

 

You might also consider creating a static array container. It still requires the custom allocator, but it's easier to write for sure.

AngelScript doesn't have static arrays yet.


Edited by saejox, 21 January 2013 - 10:55 AM.


#7 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
0Likes
Like

Posted 21 January 2013 - 03:02 PM

Is the StoreData class a script class or an application registered class?

 

All script classes are reference types. Multiple instances of reference types cannot be allocated in a single block, because the lifetime of each instance is determined by the last reference to the object instance and not the memory block itself. Also, you cannot move the position of a reference type to a different memory location, so a resize of the memory block wouldn't be possible.

 

If the StoreData is a registered class then you should make sure to register it as a value type. The CScriptArray add-on currently doesn't allocate value types in single memory block, only primitive types are allocated in single block at the moment. However, there is no restriction to change it to also allocate value types this way. If you do change it, I would be happy to include this change in the SDK.


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#8 _Engine_   Members   -  Reputation: 195

Like
0Likes
Like

Posted 22 January 2013 - 01:25 AM

StoreData class is a script class

I will be happy to modify CScriptArray but how i can overide angel script to allocate data in my buffer when angelscript calling CreateScriptObject? I only think about temperiory binding custom allocator but i am not shure what happens when angel script call ReleaseScriptObject. Maybe i need use some markers in allocated blocks. May be i need use some thing else?</p>

Edited by _Engine_, 22 January 2013 - 01:48 AM.


#9 Andreas Jonsson   Moderators   -  Reputation: 3455

Like
0Likes
Like

Posted 22 January 2013 - 07:49 AM

As StoreData is a script class I would recommend against trying to allocate them inline in a single memory block. As I mentioned before the script class is a reference type, and if the script holds a reference to one of the array elements you would invalidate the reference if the array is resized. Ideally the script language would support declaring value types for simple structs like this, but this is something that is still far away from being implemented.

 

Instead I'd suggest you create a custom container class. Instead of storing the actual StoreData class, it could store just the content of the class. Similarly to how the CScriptArray works the container can check the structure of the script class upon creation and decide how to store its content. For example for the StoreData class, the structure is very simple and the content can be copied with a simple memcpy from the position of the first member.


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS