If a function is frequently called many times, we should start with the details. Sometimes we have no idea about the logic of the Code itself. These details can often bring us unexpected results. A type modifier is one of them.
const
Keywords do not need to be said, but we can often ignore them, so try to develop a habit: when designing a function parameter or declaring a variable (either local or global, subconsciously consider whether we can design this variable as unchangeable.
NOTE: If it is a pointer, we should also consider that the content pointed to by the pointer cannot be changed (it should be declaredconst int *p
), Or the pointer itself cannot be modified (should be declaredint * const p
), Or neither of them can be modified (should be declaredconst int * const p
).
restrict
Keywords: I have never touched on DSP Platform optimization before, but it has brought me unexpected results several times in recent learning and optimization processes.
Restrict, a type qualifiers in C language, used to tell the compiler that the object has been referenced by the pointer, you cannot modify the content of this object in any other way, either directly or indirectly, except for the pointer. -- Baidu encyclopedia
The Type modifier is used to tell the compiler more information about variables, so that the compiler can be more relaxed and boldly optimized during optimization, because the compiler always chooses the safest optimization solution. However, sometimes we can see that even if these type modifiers are added, the effect is not obvious. In fact, the compiler has done this for us.
DSP Platform optimization example: If you can add const, restrict and other types of modifiers as much as possible