DEV Community

dima853
dima853

Posted on

INLINE vs. NORMAL FUNCTIONS: What's Really Happening in Assembly?

πŸ”₯ 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

programming #C #assembler #optimization #inline #compiler

Top comments (0)