π₯ INLINE vs. NORMAL FUNCTIONS: What's Really Happening in Assembly?
All sources (code, etc.) - https://github.com/dima853/self_university/tree/main/network/c/fucntions/inline
Everyone talks about 'inline', but few have seen the difference in real assembly. See:
π― With INLINE β code is INSERTED directly:
test_with_inline:
movl (%rdi), %r8d # Byte comparison
cmpl (%rsi), %r8d # β code INSERTED!
je .L18
movl (%rsi), %ecx
cmpl (%rdx), %ecx # β INSERTED again!
je .L19
The gist: The compiler COPYS the function code to each call site.
π WITHOUT INLINE β CALLS to the function:
test_without_inline:
call mac_equals_normal # β JUMP into the function!
movzbl %al, %ecx
call mac_equals_normal # β JUMP AGAIN!
movl %eax, %esi
The gist: Each call is a jump to a different memory location
π SIMPLE ANALOGY:
- Without inline = "Courier: you β courier β restaurant β courier β you"
- With inline = "Microwave: you β microwave β done!"
β‘ RESULT:
- With inline: ~4 instructions per comparison
- Without inline: ~20+ instructions (call + return)
π― CONCLUSION:
inline
is when the compiler stops "sending the courier" and starts "microwave" right there!
Command for testing:
gcc -S -O2 test.c -o test.s
Top comments (0)