Optimizing by compiler

Discussions about Coding and Scripting
User avatar
Barbie
Godlike
Posts: 2956
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Optimizing by compiler

Post by Barbie »

I noticed that if a class is defined in a project in an UC file but never used as a class within the source code in another project file, the compiler left it out. I stumbled over that by assigning that class via SetPropertyText():
MapPatcher code for map "MH-Incursion+":

Code: Select all

if (SpecialControlCannon.GetPropertyText("Fire_ProjectileClass") == "Class'UnrealI.RazorBlade'") // #=5
	if (class'SharedCode'.static.SetPropertyTextEx(SpecialControlCannon, "Fire_ProjectileClass", "Class'MapPatcherSvr.RazorBladeSB'", CRespectCase))
		PatchCount++;
It does NOT work in runtime when 'RazorBladeSB' is not used as a class in the source code. If I just add local RazorBladeSB WhatEver;, the replacement in runtime works.
(SetPropertyTextEx() is the same as SetPropertyText() but returns FALSE if assigning didn't work.)
"If Origin not in center it be not in center." --Buggie
Buggie
Godlike
Posts: 3229
Joined: Sat Mar 21, 2020 5:32 am

Re: Optimizing by compiler

Post by Buggie »

Compiler not optimize anything. In fact compiler pretty simple and not do any optimizations at all.
Even do all casts even which not need. Only in 469e start some changes about that - compiler remove unnecessary casts for constants and put final constant into code.

What described is linker optimization.
Linker in game load only things, which linked in some way. So before assign something via text, which not mention before, you need make dynamic load for this class.

You can do own wrapper which try set text value with check after, if it turn into none, then dynamic load object and try again.
User avatar
Barbie
Godlike
Posts: 2956
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Optimizing by compiler

Post by Barbie »

Buggie wrote: Fri Nov 22, 2024 9:19 am So before assign something via text, which not mention before, you need make dynamic load for this class.
Thanks for answer. It also works - as described above - when I add an unused local variable of that type.
"If Origin not in center it be not in center." --Buggie
Buggie
Godlike
Posts: 3229
Joined: Sat Mar 21, 2020 5:32 am

Re: Optimizing by compiler

Post by Buggie »

Compiler not optimize anything. So unused local variable still create dependency. And force linker to load specified class.
That how "preload" in UT2004 work - they simple list used classes in function, which never called.

Unused function with "preload" in name is better approach, since not trigger compiler warning about unused local variable.