2

If I had a C or C++ program where I was using say 20 integers throughout the program, would it improve performance to create an array of size 20 to store the integers and then create aliases for each number?

Would this improve the cache locality (rather than just creating 20 normal ints) because the ints would be loaded into the cache together as part of the int array(or at least , improve the chances of this)?

9
  • 2
    How do you know that your compiler isn't doing this already?
    – Bo Persson
    Feb 20, 2013 at 10:45
  • 2
    Are they constant? Chances are this isn't something you should be worrying about.
    – Aesthete
    Feb 20, 2013 at 10:46
  • @BoPersson because it is CPU who caches stuff during the execution.
    – Andrey
    Feb 20, 2013 at 10:46
  • Do you use them together? Otherwise improving their locality won't help a bit. Also note that caches have more then 20 cachelines, so it depends on whether or not you application uses much more data. Without knowing anything about the program there is no way to generally answer this
    – Grizzly
    Feb 20, 2013 at 10:46
  • 1
    @Andrey: but it is the compiler (and the language specs and the source code) which determines the layout of data.
    – jalf
    Feb 20, 2013 at 10:51

6 Answers 6

2

The question is how do you allocate space for them? I doubt that you just randomly do new int 20 times here and there in the code. If they are local variables then they will get on stack and get cached.

The main question is is that worth bothering? Try to write your program in readable and elegant way first, and then try to remove major bottlenecks and only after start messing with microoptimizations. If you are processing 20 ints, should not they be array essentially?

Also is it theoretical question? If it is, then yes, array will likely be cached better then 20 random areas in memory. If it is practical question, then I doubt that this is really important unless you are writing supercritical performance code, and even then microoptimizations are last thing to deal with.

1
  • I meant new int[]- i.e. an array of ints Feb 20, 2013 at 11:16
2

It might improve performance a bit, yes. It might also completely ruin your performance. Or it might have no impact whatsoever because the compiler already did something similar for you. Or it might have no impact because you're just not using those integers often enough for this to make a difference.

It also depends on whether one or multiple threads access these integers, and whether they just read, or also modify the numbers. (if you have multiple threads and you write to those integers, then putting them in an array will cause false sharing which will hurt your performance far more than anything you'd hoped to gain)

So why don't you just try it?

There is no simple, single answer. The only serious answer you're going to get is "it depends". If you want to know how it would behave in your case, then you have two options:

  1. try it and see what happens, or
  2. gain a thorough understanding of how your CPU works, gather data on exactly how often these values are accessed and in which patterns, so you can make an educated guess at how the change would affect your performance.

If you choose #2, you'll likely need to follow it up with #1 anyway, to verify that your guess was correct.

Performance isn't simple. There are few universal rules, and everything depends on context. A change which is an optimization in one case might slow everything down in another.

If you're serious about optimizing your code, then there's no substitute for the two steps above. And if you're not serious about it, don't do it. :)

1

Yes, the theoretical chance of the 20 integers ending up on the same cache line would be higher, although I think a good compiler would almost always be able to replicate the same performance for you even when not using an array.

1

So, you currently have int positionX, positionY, positionZ;then somewhere else int fuzzy; and int foo;, etc to make about 20 integers?

And you want to do something like this:

int arr[20];
#define positionX arr[0]
#define positionY arr[1]
#define positionZ arr[2]
#define fuzzy     arr[3]
#define foo       arr[4]

I would expect that if there is ANY performance difference, it may make it slower, because the compiler will notice that you are using arr in some other place, and thus can't use registers to store the value of foo, since it sees that you call update_position which touches arr[0]..arr[2]. It depends on how finegrained the compilers detection of "we're touching the same data" is. And I suspect it may quite often be based on "object" rather than individual fields of an object - particularly for arrays.

However, if you do have data that is used close together, e.g. position variables, it would probably help to have them next to each other.

But I seriously think that you are wasting your time trying to put variables next to each other, and using an array is almost certainly a BAD idea.

1
  • I think problem with this question that it is asked in super abstract form and it is hard to say something specific, only making assumptions.
    – Andrey
    Feb 20, 2013 at 10:56
1

It would likely decrease performance. Modern compilers will move variables around in memory when you're not looking, and may store two variables at the same address when they're not used concurrently. With your array idea, those variables cannot overlap, and must use distinct cache lines.

0

Yes this may improve your performance, however it may not, as its really Variables that get used together that should be stored together.

So if they are used together then yes. Variables and objects should really be declared in the function in which they are used as they will be stored on the stack (level-1 cache in most cases).

So yes if you are going to use them together i.e they are relevant to each other, then this would probably be a little more efficient, provding you also take into consideration how you allocate them memory.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.