0

Currently I am using XNA Game Studio 4.0 with C# Visual Studio 2010. I want to use a versatile method for handling triangles. I am using a preset array of VertexPositionColor items passed through the GraphicsDevice.DrawUserPrimitives() method, which only handles arrays. Because arrays are fixed, but I wanted to have a very large space to arbitrarily add new triangles to the array, my original idea was to make a large array, specifically

VertexPositionColor vertices = new VertexPositionColor[int.MaxValue];

but that ran my application out of memory. So what I'm wondering is how to approach this memory/performance issue best.

  • Is there an easy way to increase the amount of memory allocated to the stack whenever my program runs?
  • Would it be beneficial to store the array on the heap instead? And would I have to build my own allocator if I wanted to do that?
  • Or is my best approach simply to use a LinkedList and deal with the extra processing required to copy it to an array every frame?
Superdoggy
  • 218
  • 1
  • 11
  • Can you utilize the flyweight pattern(http://en.wikipedia.org/wiki/Flyweight_pattern) to reduce the amount of allocated objects? – Noel Feb 21 '15 at 15:42
  • Perhaps I could, although I'm already planning on removing redundant vertices. I'm not sure if it would be worth it to do anything further than that - I don't want my code to get too untidy. – Superdoggy Feb 21 '15 at 16:12

2 Answers2

2

I hit this building my voxel engine code. Consider the problem I had:

Given an unknown volume size that would clearly be bigger than the amount of memory the computer had how do I manage that volume of data?

My solution was to use sparse chunking. for example:

In my case instead of using an array I used a dictionary. This way I could lookup the values based on a key that was say the hashcode of a voxels position and the value was the voxel itself. This meant that the voxels were fast to pull out, and self organised by the language / compiler in to an indexed set.

It also means that when pulling data back out I could default to Voxel.Empty for voxels that hadn't yet been assigned.

In your case you might not need a default value but using a dictionary might prove more helpful than an array.

The up shot ... Arrays are a tad faster for some things but when you consider all of your usage scenarios for the data you may find that overall the gains of using a dictionary are worth a slight allocation cost.

In testing I found that if I was prepared to drop from something like 100ms per thousand to say 120ms per thousand on allocations I could then retrieve the data 100% faster for most of the queries I was performing on the set.

Reason for my suggestion here:

It looks like you don't know the size of your data set and using an array only makes sense if you do know the size otherwise you tie up needless "pre allocated chunks of ram" for no reason in order to make your code ready for any eventuality you want to throw at it.

Hope this helps.

War
  • 8,539
  • 4
  • 46
  • 98
  • Like I said to KcDoD below I think I might use a dictionary filled with lists, thanks for the tip. And yes, I don't know the size of my triangle data set - a program where nothing can be created or destroyed sounds very boring and limited to me so I did want to make my code ready for anything I wanted to throw at it. – Superdoggy Feb 21 '15 at 16:22
0

You may try List<T> and ToArray() method associate with List. And it's supported by XNA framework too (MSDN).

List is a successor to ArrayList and provide more features and strongly typed (A good comparison).

About performance, List<T>.ToArray is a O(n) operation. And I suggest you to break your lengthy array to sort of portions which you can name with a key [Some sort of unique identifier to a region or so on] . And store relevant information in a List and use Dictionary like Dictionary<Key, List<T>> which could reduce operations involved. Also you can process required models with priority based approach which would give a performance gain over processing complete array at once.

Community
  • 1
  • 1
Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46
  • Thanks, I think I might go with this. Does a dictionary of lists converted to arrays at draw time sound like a good model? I think with that approach I could also have a little bit more control over individual models, yet still have the opportunity to add more groups of triangles whenever I wanted. – Superdoggy Feb 21 '15 at 16:17
  • You could adopt the method and test for it's performance and yes as you mention you have more control since you try to handle individual models(let's say so) separately. – Kavindu Dodanduwa Feb 21 '15 at 16:31