Advertisement

perfomances issues with classes in array

Started by January 21, 2013 08:22 AM
7 comments, last by WitchLord 11 years, 10 months ago

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?

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.
Advertisement

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.

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

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.

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.

Advertisement

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

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>

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

This topic is closed to new replies.

Advertisement