I have a memory block that is divided into a series of location that can be retrieved and returned by client code.
The method that returns locations back looks like this:
void ReturnLocation(void *address) {
int location = AddressToLocation(address); // I need the location here
// some code
DoSmthA(location);
}
void DoSmthA(int location) {
// I need the address, but also the location
void *address = LocationToAddress(location);
// do something with the address
DoSmthB(location);
}
void DoSmthB(int location) {
// Again, I need the address, but also the location
void *address = LocationToAddress(location);
// do something with the address
DoSmthC(location); // It may go on this way...
}
// ------------------------------------------------------
void* LocationToAddress(int location)
{
return (void *)((char *)this + HEADER_SIZE + (location * LocationSize));
}
int AddressToLocation(void *address)
{
return (int)(((__int64)address - HEADER_SIZE - (__int64)this) / LocationSize);
}
My question is: should I pass only the location to the helper methods, or it is faster to pass each time the address too (and not compute it again and again):
void DoSmthA(int location, void *address) { }
or even better to use a struct like this:
struct LocationInfo { int Location; void *Address; };
void DoSmthA(LocationInfo locInfo) { }
This methods may be called million of times and I'm not sure if the operations to compute the address from the location (two additions and one multiplication) are faster or slower than passing a second parameter containing the address.
Thanks in advance!