Recent

Author Topic: Can compiler throw out unused code  (Read 377 times)

LemonParty

  • Sr. Member
  • ****
  • Posts: 360
Can compiler throw out unused code
« on: October 13, 2025, 11:30:26 am »
Hello.

Can compiler throw out from unit unused functions? Unused methods from class or record?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

Thaddy

  • Hero Member
  • *****
  • Posts: 18321
  • Here stood a man who saw the Elbe and jumped it.
Re: Can compiler throw out unused code
« Reply #1 on: October 13, 2025, 11:39:20 am »
Yes, the compiler can do that, but only for a whole program and with whole program optimization, a.k.a WPO and which is usually a three pass effort.
This is usually only worth it when the program is finished and debugged.
The above is the case for unused methods in class or record. It optimizes away the vmt's.
See https://wiki.freepascal.org/Whole_Program_Optimization
The compiler itself is compiled that way.

What the compiler always does at -O2 or higher is eliminate dead code, but the vmt's will then still be fully intact.
« Last Edit: October 13, 2025, 11:44:30 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12524
  • FPC developer.
Re: Can compiler throw out unused code
« Reply #2 on: October 13, 2025, 11:55:05 am »
But only virtual/overriden methods are in a VMT. The rest can be eliminated if unused.

LemonParty

  • Sr. Member
  • ****
  • Posts: 360
Re: Can compiler throw out unused code
« Reply #3 on: October 13, 2025, 03:44:12 pm »
Thank you, Thaddy. I will look the article.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

 

TinyPortal © 2005-2018