B
bill
I recently worked with a piece of code where dereferencing the pointer
was too slow, and I was able to achieve a nearly 2x speed-up by
replacing a local array of size 8 with 8 local variables. (*x requires
2 fetches, x requires 1, so it's easy to explain it, I was just
surprised that I actually encountered a situation where it makes sense
to do this optimization.) Now, I want to test a similar situation, but
the array that will be replaced is substantially larger, and I'd like
to make the code "cleaner" by doing something like the following:
u_int32_t x0, x1, x2, ...., xN;
u_int32_t *p;
int i;
p = &xN;
for (i=0; i<N+1; i++) {
p = initialize(i);
}
Really, the only advantage I get is that I don't have N+1 lines of
initialization in the source. This works in the one case I've
tested...is there any chance that I can rely on this? I'm not worried
about portability, but I am wondering if there's a chance that, for
instance, increasing N might cause the implementation to not put the
local variables in contiguous memory on the stack. Would it be safer
to declare a struct, or should I just avoid this 'trick' altogether?
Also, on a slightly related note, what's the deal with linux defining
"u_int32_t" instead of "uint32_t"? There's a comment before the
declarations of u_int{8,16,32}_t in /usr/include/sys/type.h (on Fedora
Core 3) that reads "But these were defined by ISO C without the first
`_'." Does this mean that ISO C wants the "_" to not be there, but the
linux implementation decided to add it? Which is the proper
nomenclature? My syntax highlighting (vim) recognizes uint32_t and not
u_int32_t, but gcc doesn't like uint32_t. Does it matter? And if so,
which is correct? I stated above that I don't care about portability,
but I want to do things properly at least!
was too slow, and I was able to achieve a nearly 2x speed-up by
replacing a local array of size 8 with 8 local variables. (*x requires
2 fetches, x requires 1, so it's easy to explain it, I was just
surprised that I actually encountered a situation where it makes sense
to do this optimization.) Now, I want to test a similar situation, but
the array that will be replaced is substantially larger, and I'd like
to make the code "cleaner" by doing something like the following:
u_int32_t x0, x1, x2, ...., xN;
u_int32_t *p;
int i;
p = &xN;
for (i=0; i<N+1; i++) {
p = initialize(i);
}
Really, the only advantage I get is that I don't have N+1 lines of
initialization in the source. This works in the one case I've
tested...is there any chance that I can rely on this? I'm not worried
about portability, but I am wondering if there's a chance that, for
instance, increasing N might cause the implementation to not put the
local variables in contiguous memory on the stack. Would it be safer
to declare a struct, or should I just avoid this 'trick' altogether?
Also, on a slightly related note, what's the deal with linux defining
"u_int32_t" instead of "uint32_t"? There's a comment before the
declarations of u_int{8,16,32}_t in /usr/include/sys/type.h (on Fedora
Core 3) that reads "But these were defined by ISO C without the first
`_'." Does this mean that ISO C wants the "_" to not be there, but the
linux implementation decided to add it? Which is the proper
nomenclature? My syntax highlighting (vim) recognizes uint32_t and not
u_int32_t, but gcc doesn't like uint32_t. Does it matter? And if so,
which is correct? I stated above that I don't care about portability,
but I want to do things properly at least!