C
C
coder2021-08-30 13:01:52
Programming
coder, 2021-08-30 13:01:52

How to help the processor make the right decision related to preloading?

I'm very interested in the mechanism of processors - preloading, but I can't figure out how I can optimize this in the code.
As I understand this mechanism is related to caching, maybe you need to structure the code correctly (the fattest code (by data type) of the data is at the top, and the small one is at the bottom?).
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wataru, 2021-08-30
@RomanSamets

The only thing you need to remember is the locality of data access. Next, you need to understand how you are accessing data. From this, you need to dance further. It is better to walk along the array from left to right than to jump back and forth on it. Ideally, you have all the data tightly packed in memory and you clean them in a row.
If you have a struct, such as coordinates, where multiple fields are always used together, then it's worth stuffing them into an array of structs. If you have a large structure from which only a few fields are needed in different parts of the program, then it is better to use an array structure - because the data used will be more densely located in memory.
Arrays are better than lists if you don't need to frequently add to/remove from the middle.
Use smaller data types if you can. Avoid allocations if you can.

M
Myclass, 2021-12-30
@Myclass

I'm not sure I understood you. But I'll try to answer. . No wayBecause it is not your code that is preloaded, but what is compiled. That is, it depends on the compiler, how and what the processor will execute. This time. The second is that the processor loads not only those commands that are being executed, but also simply sections of code that may or may not be executed. For example, when a branch occurs - if this condition = true, then the executor is this piece of Claude, earlier - another. And these two pieces will be loaded even before the condition is fulfilled. This reduces the time delay if the condition is met and only then the section of code to which the jump occurs is reloaded. Therefore, optimization on your part when writing programs in C, C++, C# or Java for this particular function of the processor does not see much sense or need. He even makes parallelism for you, if it has several cores and you didn’t even provide for this parallelism in your code. The name of this function is register renaming.
Although the tips above for example will make it easier to write and the assembly code will also be better, but these are more general optimization suggestions than for this processor function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question