Nudged by the report of high memory usage I want to seriously optimize memory usage of CGE. Right now X3D nodes cost too much in memory β which limits some use-cases (when spawning a lot of TCastleScenes would be the most natural solution), and it affects both loading and runtime performance (as loading is slower when you need to clone/initialize more memory, and performance is slow when the cache is underutilized).
A first wave of improvements has already landed in the engine. A 2nd wave is in progress β it depends on remaking how all X3D nodes are set up, so itβs a big rework (but it was a long time coming).
Things done now:
-
We now have an efficient step interpolation mode (see TInterpolation), used automatically by sprite sheets and glTF models with step animation.
While βstepβ interpolation was possible previously, but it was (ab)using the linear interpolation calculation, by duplicating keys. This was wasting both memory (2x data needed) and time (doing useless lerp operations).
-
We have simplified our X3D nodes inheritance. We use now TNodeFunctionality (a simple component system inside X3D nodes) with some descendants like TTimeDependentFunctionality and TTransformFunctionality. We no longer use Pascal interfaces for them. There are actually 2 reasons for it:
-
In some cases, we really wanted to share implementation, not just API. So interfaces were never a proper tool.
In effect our new TTimeDependentFunctionality is a much simplified and more natural old
TInternalTimeDependentHandler
, the new concept just fits our needs better. -
I never really grew to like COM interfaces. In FPC/Lazarus we have CORBA interfaces, and I made no secret that I like CORBA interfaces much more (basically, they work more like Java and C# interfaces, and are not entangled with unrelated things like memory management or COM technology). But Delphi only has COM interfaces, so CORBA interfaces are not available to us.
This introduces quirky code when using interfaces, as we do not want the automatic garbage collection that the COM interfaces bring (and one has to watch out for it, even with hacked
_AddRef
,_Release
likeTComponent
does). So one has to watch for lifetime of temporary values.
-
-
CoordinateInterpolator
in sprite sheets can be often much simpler or even removed completely. This saves memory and execution time.