mirror of
https://github.com/pound-emu/ballistic.git
synced 2026-01-31 01:15:21 +01:00
docs: add handling arrays in programming rules
Signed-off-by: Ronald Caesar <github43132@proton.me>
This commit is contained in:
@@ -5,11 +5,11 @@ Pointer aliasing forces compilers to avoid optimizing code as best as they shoul
|
||||
For all examples shown below, assume `context` is a pointer to a heap allocated
|
||||
struct.
|
||||
|
||||
## Rule 1: Scalars and Arrays inside a loop
|
||||
## Rule 1: Scalars inside a loop
|
||||
|
||||
If booleans, integers, or arrays gets modified inside a loop, always assign them to
|
||||
If booleans or integers gets modified inside a loop, always assign them to
|
||||
local variables before doing so. Yes there are other ways to make the compiler
|
||||
optimize this specific scenario, but we do this to guarantee speed.
|
||||
optimize this specific scenario, but we do this to easily **guarantee** speed.
|
||||
|
||||
```c
|
||||
// SLOW
|
||||
@@ -82,3 +82,24 @@ for (int i = 0; i < 100; i++) {
|
||||
|
||||
// No final store needed, we kept it in sync the whole time.
|
||||
```
|
||||
|
||||
## Rule 2: Arrays inside a loop
|
||||
|
||||
`struct->array[i]` forces the CPU to calculate `Base + Offset + (i * 4)` every
|
||||
time. `*cursor++` is just `Pointer + 4`.
|
||||
|
||||
|
||||
```c
|
||||
// SLOW
|
||||
//
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
context->instructions[i] = ...; // Math overhead + Aliasing fear.
|
||||
}
|
||||
|
||||
// FAST
|
||||
//
|
||||
bal_instruction_t *cursor = context->instructions;
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
*cursor++ = ...; // Simple increment.
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user