0

I'm trying to port C++ code from a developer who uses global variables called p0, p1, ..., p30 of integer type. I wondered if I could not just use an array int p[31]; and access them as p[0], p[1],...

It seems plausible that there would be no performance hit if the indices were always passed as constants. Then I could just pass his data as extern int p[];.

Obviously I could use descriptive macros for the various indices to make the code clearer.

I know that this sounds like weird code, but the developer seems to have a "neurodiverse" personality, and we can't just tell him to mend his ways. Performance is very important in the module he is working on.

user1741137
  • 4,949
  • 2
  • 19
  • 28
  • 4
    Try doing it and measure the performance – HolyBlackCat Nov 09 '20 at 18:05
  • Now that's something revolutionary, using macros to make code clearer. ;) Anyhow, what have you tried and what have you found? BTW: Using global mutable state is a bad idea, not just in C++. – Ulrich Eckhardt Nov 09 '20 at 18:06
  • 4
    Quibble: `p0` through `p30` would be replaced by `int p[31];` – ShadowRanger Nov 09 '20 at 18:07
  • Agreed with @HolyBlackCat. That said, I don't see any reason it would be any slower or faster; I suppose in theory a compiler might feel obliged to store the array in order where individual variables might be reordered if the compiler can prove anything about access patterns, but in practice, if the array indices are compile time constants, it should be able to output the exact same memory load/store operations with no explicit load at offset from array base behaviors. – ShadowRanger Nov 09 '20 at 18:08
  • If the index are not constant, then you could not have used individually named variables anyway. So there is no point in comparing the speed of the two approaches because one of them doesn't work. – François Andrieux Nov 09 '20 at 18:09
  • 1
    maybe you should actually talk to the developer. Imagine he leaves the company and no one can read his code (which you already seem to have problems with) – Raildex Nov 09 '20 at 18:09
  • Yes, I was a little sloppy about the size of the array. – user1741137 Nov 09 '20 at 18:15
  • What benefit do you see from making this change? – Pete Becker Nov 09 '20 at 19:05
  • Yes, a modern compiler, when compiling with optimization enabled (not _Debug_ mode), should generate a code as fast with an array indexed by constants than a bunch of variables. – prapin Nov 09 '20 at 19:45

2 Answers2

1

I don't see any danger in the replacement of variables with an array.

Dominique
  • 16,450
  • 15
  • 56
  • 112
1

Modern compilers are very good at optimizing code.

You normally can asume that there will be no difference between using individual variables p0, … p30 and an std::array<int, 31> (or an int[31]), if they are used in the same way and if you use only constants for accessing the array.

A compiler is not required to keep an std::array or an int[] as such, but can completely or partially optimize it a way as long as it complains with the as-if rule.

Variables (also arrays) only need to exists in memory if the compiler can't determin their contents at runtime and/or if the registers are not sufficient to do all manipulations related to those variables using only these registers.

If they exits in memory they need to be referenced by their address in memory, for both a pN and a p[N] (if N is constant) the address where the value is in memory can be determined in the same way at compile time.

If you are unsure if the generated code is the same you can always compair the output generated by the compiler (this can e.g. be done on godbolt), or using the corresponding compiler flags if you don't want to submit code to a foreign service.

t.niese
  • 39,256
  • 9
  • 74
  • 101