Home > Blockchain >  Computing data on the fly vs. pre-computed table
Computing data on the fly vs. pre-computed table

Time:10-30

A common technique to save some time is to use a table of pre-computed values, instead of computing the value every time on the fly. For example, for an integer approximation of the logarithm.

Could it happen that fetching a value for the first time will actually be slower than computing it because the table is far away in memory (the DATA segment) and the access causes a page load? Is there a way to force the compiler to put the data closer to the code?

I presume the table has to be static, does it then matter if it is declared locally inside the given function or globally?

CodePudding user response:

Could it happen that fetching a value for the first time will actually be slower than computing it because the table is far away in memory (the DATA segment) and the access causes a page load?

Yes, it can happen. Whether a page load occurs is not the only consideration. This question is hugely dependent on system architecture and the tools used. It can be affected by whether instruction cache and data cache are separate (and on which levels of cache), the cache geometry, other patterns of use in the program, processor speed, cache speed, memory speed, and more.

Sometimes high-performance software will “touch” data it does not need now but anticipates using in the future to cause it to be loaded into memory and/or cache so that it is available more quickly in the future when it is needed. (Depending on system architecture and circumstances, loading data from memory may not slow down any subsequent instructions if no such instruction uses the loaded data, or there may be instructions that explicitly request data be loaded into cache but not a CPU register.)

Is there a way to force the compiler to put the data closer to the code?

When it is necessary to closely control where code and data are located in memory, it is usually done with features of the linker and/or program loader, and possibly with assembly language, not with the compiler. The involvement of the compiler may be to compile small functions into separate object modules or with features allowing functions to be separated from other functions, so that the linker can rearrange the functions and data according to other instructions it is given.

I presume the table has to be static,…

No, “static” in the sense of object lifetime is largely irrelevant to system performance except in systems where some data may be put into ROM or different types of memory with different performance characteristics. As for how the keyword static affects identifier scope, that is irrelevant to memory performance.

… does it then matter if it is declared locally inside the given function or globally?

Not unless the compilers has features for controlling the location of one of these and not the other.

  • Related