Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

.NET Fest 2018. Николай Балакин. .NET Internals: управление памятью в стеке

16 views

Published on

Как CLR гарантирует, что память в стеке будет очищена?
В этом докладе мы обсудим процесс преобразования C# кода в машинный код и то, как это связано с управлением памятью в стеке. Увидим разные трюки, которые JIT-компилятор делает, чтобы код работал быстрее. Доклад будет интересен тем, кто хочет знать, что скрывается под капотом привычных вещей и предпочитает понимать, как в деталях работает его код.

Published in: Education
  • Be the first to comment

  • Be the first to like this

.NET Fest 2018. Николай Балакин. .NET Internals: управление памятью в стеке

  1. 1. .NET Internals stack memory management
  2. 2. Compilation flow var hello = "Hello"; var world = "World"; Console.WriteLine(hello + world); ldstr "Hello" ldstr "World" call System.String::Concat call System.Console::WriteLine movabs rdi, 0x196af5078 mov rdi, qword ptr [rdi] movabs rsi, 0x196af5080 mov rsi, qword ptr [rsi] call 0x10d5ad380 mov rdi, rax call 0x10dbb1d40
  3. 3. Notation types • Infix • Suffix • Postfix 2 + 3 + 2 3 2 3 +
  4. 4. Notation types • Infix • Suffix • Postfix builder.Append("Hello!") Append builder "Hello!" builder "Hello!" Append
  5. 5. Stack machine
  6. 6. 2 + 3 * 4 Stack machine
  7. 7. 2 2 3 4 3 2 2 12 14 2 + 3 * 4 Stack machine Stack head
  8. 8. Stack machine C# var i = 2 + 2; 2 2 + i; Forth
  9. 9. Developer Forth was Yoda just
  10. 10. Stack machine var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone); C# call Contact GetPhoneNumber() dup call void SendMessage(Contact) call void MakeCall(Contact) CIL
  11. 11. Stack machine var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone); call Contact GetPhoneNumber() dup call void SendMessage(Contact) call void MakeCall(Contact) C# CIL Stack phone top
  12. 12. Stack machine var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone); call Contact GetPhoneNumber() dup call void SendMessage(Contact) call void MakeCall(Contact) C# CIL Stack phone phone top
  13. 13. Stack machine var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone); call Contact GetPhoneNumber() dup call void SendMessage(Contact) call void MakeCall(Contact) C# CIL Stack phone phone top
  14. 14. Stack machine var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone); call Contact GetPhoneNumber() dup call void SendMessage(Contact) call void MakeCall(Contact) C# CIL Stack phone top
  15. 15. Stack machine var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone); call Contact GetPhoneNumber() dup call void SendMessage(Contact) call void MakeCall(Contact) C# CIL Stack top
  16. 16. Stack machine var mode = Mode.Default var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone, mode); C# ldc.i4.1 stloc.0 call Contact GetPhoneNumber() stloc.1 ldloc.1 call void SendMessage(Contact) ldloc.1 ldloc.0 call void MakeCall(Contact, valuetype Mode) CIL
  17. 17. Stack machine var mode = Mode.Default var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone, mode); ldc.i4.1 stloc.0 call Contact GetPhoneNumber() stloc.1 ldloc.1 call void SendMessage(Contact) ldloc.1 ldloc.0 call void MakeCall(Contact, valuetype Mode) C# CIL Stack 1 top
  18. 18. Stack machine var mode = Mode.Default var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone, mode); ldc.i4.1 stloc.0 call Contact GetPhoneNumber() stloc.1 ldloc.1 call void SendMessage(Contact) ldloc.1 ldloc.0 call void MakeCall(Contact, valuetype Mode) C# CIL Stack 1 Locals mode top
  19. 19. Stack machine var mode = Mode.Default var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone, mode); ldc.i4.1 stloc.0 call Contact GetPhoneNumber() stloc.1 ldloc.1 call void SendMessage(Contact) ldloc.1 ldloc.0 call void MakeCall(Contact, valuetype Mode) C# CIL Stack phone Locals mode top
  20. 20. Stack machine var mode = Mode.Default var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone, mode); ldc.i4.1 stloc.0 call Contact GetPhoneNumber() stloc.1 ldloc.1 call void SendMessage(Contact) ldloc.1 ldloc.0 call void MakeCall(Contact, valuetype Mode) C# CIL Stack phone Locals mode phone top
  21. 21. Stack machine var mode = Mode.Default var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone, mode); ldc.i4.1 stloc.0 call Contact GetPhoneNumber() stloc.1 ldloc.1 call void SendMessage(Contact) ldloc.1 ldloc.0 call void MakeCall(Contact, valuetype Mode) C# CIL Stack phone Locals mode phone top
  22. 22. Stack machine var mode = Mode.Default var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone, mode); ldc.i4.1 stloc.0 call Contact GetPhoneNumber() stloc.1 ldloc.1 call void SendMessage(Contact) ldloc.1 ldloc.0 call void MakeCall(Contact, valuetype Mode) C# CIL Stack phone Locals mode phone top
  23. 23. Stack machine var mode = Mode.Default var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone, mode); ldc.i4.1 stloc.0 call Contact GetPhoneNumber() stloc.1 ldloc.1 call void SendMessage(Contact) ldloc.1 ldloc.0 call void MakeCall(Contact, valuetype Mode) C# CIL Stack phone Locals mode phone top
  24. 24. Stack machine var mode = Mode.Default var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone, mode); ldc.i4.1 stloc.0 call Contact GetPhoneNumber() stloc.1 ldloc.1 call void SendMessage(Contact) ldloc.1 ldloc.0 call void MakeCall(Contact, valuetype Mode) C# CIL Stack phone mode Locals mode phone top
  25. 25. Stack machine var mode = Mode.Default var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone, mode); ldc.i4.1 stloc.0 call Contact GetPhoneNumber() stloc.1 ldloc.1 call void SendMessage(Contact) ldloc.1 ldloc.0 call void MakeCall(Contact, valuetype Mode) C# CIL Stack phone mode Locals mode phone top
  26. 26. Stack machine var mode = Mode.Default var phone = GetPhoneNumber(); SendMessage(phone); MakeCall(phone, mode); ldc.i4.1 stloc.0 call Contact GetPhoneNumber() stloc.1 ldloc.1 call void SendMessage(Contact) ldloc.1 ldloc.0 call void MakeCall(Contact, valuetype Mode) C# CIL Stack Locals mode phone top
  27. 27. JIT compiler
  28. 28. Demo
  29. 29. JIT compiler LdVar Sorting & Tree Ordering Loop Optimizations Range Check Elimination Rationalization Importer Inliner Morph Flowgraph Analysis EmitterLowering LSRA CodeGen Rationalized LIR Native Code GenTrees IR with reference counts & evaluation order Instr Descs Real registers Reg Reqts GenTrees (IR) IL (bytecode) Copy Propagation CSE Assertion Propagation Loop Optimizations Range Check Elimination Copy Propagation CSE Assertion Propagation
  30. 30. JIT optimizations • Not saving variables • Method call inlining • Range check elimination • Jump instead of call • …
  31. 31. Demo
  32. 32. .NET Internals stack memory management Nikolay Balakin nikolay@balakin.me

×