Want to make your Dart or Flutter code run a little faster? There's a useful tip involving how your code gets turned into machine code.
Use @pragma('vm:prefer-inline')
Put @pragma('vm:prefer-inline')
just before a function definition.
This is a hint to the Dart Virtual Machine (VM) compiler.
It suggests that you'd prefer this function to be 'inlined'.
What Does Inlining Mean?
Inlining is like copying the code inside a function and pasting it directly where that function is called. Instead of the program jumping to a separate spot for the function and then returning, it just runs the code right there.
Benefits of Inlining
Avoiding the function call jump can sometimes make the code run faster. It can be especially helpful for small functions that are called over and over again.
How the Compiler Uses It
The compiler looks at your code and uses this pragma as a suggestion. It decides if inlining is actually a good idea based on different factors. If it decides to inline the function, the final machine code will have the function's logic placed directly in the calling spot, as shown in the image example. This can lead to more efficient execution.