2

I have a 3D game where objects are stored in a 3D array. Every object is a struct of a relatively large size (a few megabytes, 4,194,304 bytes to be precise).

If I store them as an array of raw values: gameObject objects[64][64][8], they are all stored in one place, but every object is allocated, regardless of whether or not it is used.
However, if I allocate the array as an array of pointers: gameObject *objects[64][64][8], unused objects (which may be over half of the entire array) will not be allocated until they are needed, reducing the memory impact, but might be slower because the objects are all over the memory

Considering the tradeoffs between performance and memory usage, which is the best approach? Are my concerns valid?

9
  • 1
    How much is "a few bytes"? A pointer is 8 bytes on x86_64, which I would consider to be a few too Commented Mar 2, 2012 at 11:53
  • Sorry, just realized I was looking at the wrong part of my code ;). It'll be more like a few million bytes. Commented Mar 2, 2012 at 11:54
  • 3
    "Considering the tradeoffs between performance and memory usage" anything you do is irrelevant until you measure. Commented Mar 2, 2012 at 11:56
  • 1
    You will not be able to allocate 2 million objects which each is 4 million bytes in storage, because it would require a 8 Terabyte RAM. So the answer is: You need to do it with dynamic allocation. Commented Mar 2, 2012 at 12:00
  • 1
    Sometimes the ability to estimated the product of four numbers is an advantage while developing software. For your modified question, you still need 128 GByte of RAM. In the iPhone 10 maybe. Commented Mar 2, 2012 at 12:09

3 Answers 3

5

[the second approach] might be slower because the objects are all over the memory

Since every object is 4MB in size, locality of reference across different objects is almost certainly not an issue. Go with the second approach.

If the numbers you're giving us are correct, a fully populated array of 128x128x128 4MB objects would require about 8TB of RAM, which makes the first approach somewhat infeasible on typical hardware. ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

It's not just locality of reference; dereferencing pointers has a latency cost as well.
1

Your more performant option would be to use a dynamically allocated array so you can resize it to the exact number of elements you have, and store them as an array of values. Unless it's sparse, in which case I would agree that you should use indirection.

Comments

0

After your comment saying you only need 500MB of RAM (your question now states 128GB of RAM...), it's possible to compare the two options, as both are viable.

Unless the only thing you do is access the first few bytes of each object, you really aren't going to lose anything by allocating them dynamically. You need to have quite unusual usage patterns for the 1st approach to be noticeably faster than the 2nd approach.

Since you're the only one who knows how you access those objects, you should probably measure each option and see if the first is indeed faster.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.