Skip to content

LottieGen: reduce the size of generated C# code, add -CSharpVersion - #587

Merged
aborziak-ms merged 12 commits into
mainfrom
user/sergiopedri/lottiegen-size-optimization
Nov 25, 2026
Merged

aborziak-ms merged 12 commits into
mainfrom
user/sergiopedri/lottiegen-size-optimization

Conversation

@Sergio0694

@Sergio0694 Sergio0694 commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

The C# code generated by LottieGen compiles to a lot of native code with Native AOT: one WinRT call per path command and keyframe, code duplicated across the UAP variant classes, and projection code inlined at every call site. This PR makes the generated code much smaller without changing the composition graph it builds, and adds a -CSharpVersion option so that the generated code can use collection expressions and spans to avoid allocating arrays.

Changes

  • Path data and keyframes are written as constant float tables and replayed by shared helpers, instead of one call per command or keyframe.

  • Methods that are identical in every UAP variant class move to a shared abstract base class. Callees that differ become abstract/override, so each variant runs the same code as before. This is decided from a model of each class that the generator builds before writing any code (fields and methods, with the fields and methods that each method uses recorded as their names are written), not by reading the generated text.

  • The helpers that the factories call are marked non-inlined. This matters for the ones that set properties declared by base classes (Visual, CompositionShape, KeyFrameAnimation, ...), such as the sprite shape and key frame animation creation helpers and the Shapes/Children/ColorStops additions: with Native AOT and CsWinRT 2.x each such call site otherwise inlines about 150 bytes of projection code.

  • Animated trims (a property set, 2 progress bound animations and 2 expression bindings) are set up by one BindTrim helper call. This applies only when the factories start the animations, so not with WinUI 2.8 and later.

  • New -CSharpVersion <version> option (C# only, default 7.3, which gives the same code style as before). With 12 or later, the helpers take ReadOnlySpan<T> and the data is passed as collection expressions:

    • constant float tables are read from the assembly through RuntimeHelpers.CreateSpan;
    • easing function lists are kept in stack buffers;
    • with CsWinRT 3.0, CanvasGeometry.CreateGroup gets a span too.

    This works with both CsWinRT 2.x and 3.0 and assumes .NET 8 or later.

  • Generated classes are now always partial. CsWinRT 2.x requires this for AOT (CsWinRT1028).

  • New unit test project tests/UIDataCodeGen.Tests for the detection of the shared members.

C++/CX and C++/WinRT output is unchanged.

Results

16 Microsoft Store animated icons, Native AOT, x64. Generated C# goes from 7,861,801 to 5,622,303 bytes (−28.5%) by default, and to 5,592,598 bytes (−28.9%) with -CSharpVersion 12.

Native code of the icons only, in a test app (from the compiler map file):

CsWinRT LottieGen options Before After
2.x default 2,762,210 B 913,872 B (−66.9%)
3.0 -CSharpVersion 12 1,460,042 B 785,067 B (−46.2%)

Real Store publish (Release x64, Public, Native AOT), icons from the final code of this branch, baseline being the same tree with the LottieGen 8.2.250604.1 icons:

TFM LottieGen option WinStore.App.dll MSIX
net10.0-windows10.0.28000.0 (CsWinRT 2.x) default 105,049,088 → 98,555,904 B (−6.18%) 43,931,238 → 42,502,391 B (−3.25%)
net10.0-windows10.0.28000.1 (CsWinRT 3.0) -CSharpVersion 12 87,898,112 → 87,198,208 B (−0.80%) 38,161,823 → 37,923,751 B (−0.62%)

Validation

  • Graph equivalence. The generated code was compiled against a recording mock of the Composition, Win2D and WinUI APIs. The full composition graph of every AnimatedVisual class was dumped and compared with the output of LottieGen 8.2 (same source as main). It is identical for the Store icons, and also for WinUI 2.8, WinUI 3, -GenerateDependencyObject, -GenerateColorBindings, -DisableCodeGenOptimizer and no minimum UAP version, in both C# modes.
  • Rendering. The Store icons were rendered with Windows.UI.Composition under Native AOT: 1,591 frames, covering every variant at 41 progress values and every marker. All frames are pixel-identical to the baseline for the default output on CsWinRT 2.x and the -CSharpVersion 12 output on CsWinRT 3.0.
  • Allocations. Compiled for CsWinRT 3.0, the -CSharpVersion 12 output has no newarr. Compiled for CsWinRT 2.x, the only arrays left are the ones passed to CreateGroup.
  • Tests. dotnet test tests\UIDataCodeGen.Tests passes (11 tests). The test project is not yet part of the solution or CI.
  • Not covered. Lottie files with images (LoadedImageSurface), and arm64/x86.

Sergio0694 and others added 8 commits November 23, 2026 16:05
Path geometry factories used to emit one CanvasPathBuilder call per path
command, each with up to 6 float literals. In Native AOT builds every such
call site becomes tens of bytes of machine code, and path factories were the
largest part of the generated code (626 KB of the 2.76 MB of native code of
the 16 Microsoft Store animated icons).

Each path is now a float array of command codes followed by their points,
passed to a static CreatePathFromData helper written once on the
IAnimatedVisualSource class. The helper makes the same CanvasPathBuilder
calls in the same order with the same values, so the resulting geometry is
identical. The generated code stays C# 7.3 compatible.

Measured on the 16 Store icons: generated C# -2.4%, native code of the icons
in a Native AOT build -23% (2,762,210 to 2,120,878 bytes). The composition
graphs built by every AnimatedVisual class (recorded through a mock of the
composition APIs) are identical to the baseline.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…class

When an IAnimatedVisualSource has several AnimatedVisual classes (one per
UAP version), each class is a full copy of the animation, although many of
their factory methods are identical. In the 16 Microsoft Store animated
icons, 8 have 2 or 3 classes and 20% to 49% of their code was duplicated.

The C# generator now moves the methods whose code is identical in every
AnimatedVisual class into a nested abstract base class that the classes
derive from. The fields those methods use move to the base class with the
same declarations, and the methods they call that differ between the classes
become abstract in the base class and are overridden by each class, so every
class still runs exactly the same code as before. This is a post-processing
of the generated text (CSharpSharedCodeExtractor) that relies on the regular
layout produced by the generator, and leaves the code unchanged if it finds
anything unexpected. It is skipped when optimizations are disabled. The
public surface of the generated IAnimatedVisualSource class is unchanged.

Measured on the 16 Store icons (together with the previous change): generated
C# -28.1% (7,861,801 to 5,652,235 bytes), native code of the icons in a
Native AOT build -43% (2,762,210 to 1,561,125 bytes), 6,798 to 4,930
methods. The composition graphs built by every AnimatedVisual class are
identical to the baseline, and 1,591 frames rendered by Windows.UI.Composition
(every class of every icon at 41 progress values and at every marker) are
pixel identical to the baseline.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Key frame animation factories used to emit one InsertKeyFrame call per key
frame. The key frame writing of the Boolean, Color, Scalar and Vector2/3/4
animation factories now goes through a common WriteInsertKeyFrames method,
with a hook that lets a language write the value key frames as a table. The
C# generator writes a float array of progress, value components and easing
function index, plus an array of the distinct easing functions in order of
first use, replayed by static InsertKeyFrames helpers written once on the
IAnimatedVisualSource class. Key frames are inserted in the same order with
the same values and the same easing function objects, and the easing
function factories are still called in the same order, so the animations
are identical. Animations with expression key frames, and animations with
too few key frames for a table to be smaller (measured on Native AOT x64:
fewer than 4 for Scalar and Vector2, 3 for Color, 2 for the others), keep
the existing code. Other languages are unchanged.

Measured on the 16 Microsoft Store animated icons, relative to the previous
change: native code of the icons in a Native AOT build -5% (1,561,125 to
1,482,380 bytes); generated C# +0.4%. The composition graphs built by every
AnimatedVisual class are identical to the baseline, the 1,591 rendered
frames are pixel identical, and the generated code compiles as C# 7.3.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
With Native AOT, the WinRT projection code of each property set and method
call is inlined into the factory that makes the call. Measured on x64 with
the CsWinRT 2.x projection, setting a property declared by the type of the
object costs about 17 bytes per call, but setting a property declared by a
base class (for example Visual.Size on a ShapeVisual, or CompositionShape.Offset
on a CompositionSpriteShape) costs about 150 bytes per call, because the code
that gets the interface of the base class is inlined every time. Adding to
the Shapes, Children and ColorStops collections costs 44 to 51 bytes per call.

The base generator now writes the properties declared by Visual,
CompositionShape, CompositionGradientBrush, CompositionClip,
CompositionGeometry and KeyFrameAnimation, and the additions to those
collections, through hooks that a language can implement. The C# generator
calls static Set<Property>, AddShape, AddColorStop and InsertChildAtTop
helpers written once on the IAnimatedVisualSource class and marked as not
inlinable, so each call site is about 17 bytes. The same properties are set
to the same values in the same order, so the composition graph is identical.
Other languages are unchanged.

Measured on the 16 Microsoft Store animated icons, relative to the previous
change: native code of the icons in a Native AOT build -24% (1,482,380 to
1,124,881 bytes); generated C# +0.5%. The composition graphs built by every
AnimatedVisual class are identical to the baseline and the 1,591 rendered
frames are pixel identical.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The extractor that moves the code shared by AnimatedVisual classes into a
base class is the most intricate part of the code size changes, so it gets
focused unit tests: shared methods and their fields move to the base class,
differing callees become abstract and overridden, methods using fields with
different declarations or calling overloads that are not available in every
class are not shared, and nothing is extracted when nothing can be shared.

The repository had no unit test project, so this adds a small standalone
MSTest project that compiles the tested source directly. Run it with
dotnet test tests\UIDataCodeGen.Tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Review feedback: the extractor recognizes fields and methods with regular
expressions, and code using a member it does not recognize (for example a
property) could have been moved to the base class without that member being
checked. A method is now not shared if it uses the name of any member of
the classes that is neither a recognized field nor a recognized method, and
nothing is extracted if a class declaration or a member has an unexpected
layout (attributes, multi-line expression bodied members). The code
generated for the 16 Microsoft Store icons is unchanged.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
LottieGen can now generate C# code for CsWinRT 3.0 with -CsWinRTVersion 3
(the default is 2). CsWinRT 3.0 requires .NET 10 and C# 14, and projects
input arrays of Windows Runtime APIs as ReadOnlySpan<T>, so the generated
code can use spans and collection expressions instead of allocating arrays:
- the path and key frame data helpers take ReadOnlySpan<float>, and the data
  is passed as collection expressions of constants, which the compiler
  stores in the data section of the assembly and reads with
  RuntimeHelpers.CreateSpan, with no allocation or copy;
- the easing functions of key frame tables and the geometries passed to
  CanvasGeometry.CreateGroup are collection expressions, kept in inline
  array buffers on the stack.
Code generated for CsWinRT 3.0 is not intended for CsWinRT 2.x: apps using
both should generate the code for each version.

The generated IAnimatedVisualSource and AnimatedVisual classes are now
always partial, as required for Native AOT with CsWinRT 2.x (CsWinRT1028),
so consumers no longer need to edit the generated code.

Measured on the 16 Microsoft Store animated icons with -CsWinRTVersion 3,
compiled for CsWinRT 3.0: no array allocations left in the generated IL (40
newarr in the baseline, plus an array per path and key frame table with the
previous commits), and icon native code 877,716 to 870,135 bytes. The key
frame table thresholds were re-measured for spans: lowering them grows the
code. The composition graphs are identical to the baseline and the 1,591
frames rendered on CsWinRT 3.0 are pixel identical. With the default
option, the code generated for the Store icons is unchanged apart from
the partial modifiers.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The code generated with -CsWinRTVersion 3 did not depend on CsWinRT 3.0: it
only used C# 12 features (collection expressions and ReadOnlySpan<T> helper
parameters), and CanvasGeometry.CreateGroup takes the collection expression
both as CanvasGeometry[] (CsWinRT 2.x) and as ReadOnlySpan<CanvasGeometry>
(CsWinRT 3.0). The option is replaced by -CSharpVersion, the version of the
C# language that the generated code may use (7.3 by default, as before).
With 12 or later the code uses collection expressions and spans, which
avoids allocating arrays on .NET 8 or later with either CsWinRT version.
The name avoids making -l and -lang ambiguous with -Language.

The generated code is unchanged apart from the header: the default output is
identical to before, and -CSharpVersion 12 gives the same code as
-CsWinRTVersion 3 did. Compiled for CsWinRT 2.x with Native AOT, the
-CSharpVersion 12 output of the 16 Microsoft Store icons builds the same
composition graphs, renders the same 1,591 frames pixel for pixel, reads its
577 float tables from the data section, and only allocates the 34 arrays
passed to CreateGroup. Its icon native code is 1,119,838 bytes, against
1,124,881 for the default output.

Also removes a double blank line reported by StyleCop (SA1507).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@Sergio0694 Sergio0694 changed the title LottieGen: reduce the binary size of generated C# code, add -CsWinRTVersion 3 LottieGen: reduce the binary size of generated C# code, add -CSharpVersion Nov 24, 2026
Sergio0694 and others added 3 commits November 24, 2026 13:00
The CreateSpriteShape and Create<Type>KeyFrameAnimation helpers set properties
declared by base classes (TransformMatrix, FillBrush and Duration). With
Native AOT such small helpers are inlined into each of their callers, and so
is the projection code for those property sets, which is large. Marking the
helpers [MethodImpl(MethodImplOptions.NoInlining)] keeps that code in one
place. Only the C# generator writes the attribute.

The shared code extractor now keeps attributes with their member, so that
these helpers can still move to the shared base class. Unexpected attributes,
such as attributes split over several lines, still disable the extraction.

Measured on the 16 Microsoft Store icons with -CSharpVersion 12, compiled for
CsWinRT 3.0 with Native AOT: icon native code 870,135 to 820,843 bytes. The
composition graphs are identical to the baseline, and the 1,591 rendered
frames are pixel identical.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The translator animates the trim of a geometry with 4 animators: progress
bound animations of TStart and TEnd values in the property set of the
geometry, and expressions binding TrimStart and TrimEnd to their minimum and
maximum. Each such geometry factory inserted the 2 values and started the 4
animations with 7 lines of code. These now are one call to a BindTrim helper
that does the same, in the same order.

The helper is written only by the C# generator, and only when the animations
are started by the factories, so not when IAnimatedVisual2 is implemented
(WinUI 2.8 and later), whose CreateAnimations method starts the progress bound
animations. The geometry property set must have only the TStart and TEnd
values, and no other animation may reference it. The factories are evaluated
in the same order as before.

Measured on the 16 Microsoft Store icons (218 trims), on Native AOT:
- with -CSharpVersion 12 on CsWinRT 3.0, icon native code goes from 820,843
  to 785,067 bytes (870,135 before the previous commit);
- with the default options on CsWinRT 2.x, icon native code goes from
  1,124,881 to 913,872 bytes, together with the previous commit.
The composition graphs are identical to the baseline and the 1,591 rendered
frames are pixel identical, on both CsWinRT versions. The graphs are also
identical to LottieGen 8.2 for WinUI 2.8, WinUI 3, -DisableCodeGenOptimizer,
-GenerateColorBindings, -GenerateDependencyObject and no minimum UAP version,
and the C++/CX and C++/WinRT output is unchanged.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Write the NoInlining attribute on StartProgressBoundAnimation, BindProperty and
BindProperty2 too, for consistency with the other helpers that the factories
call. They are already not inlined by Native AOT, so this does not change the
code size in practice: icon native code on CsWinRT 3.0 was measured at 785,067
bytes without the attribute and 785,192 with it. The composition graphs are
identical to the baseline, and the C++/CX and C++/WinRT output is unchanged.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@Sergio0694 Sergio0694 changed the title LottieGen: reduce the binary size of generated C# code, add -CSharpVersion LottieGen: reduce the size of generated C# code, add -CSharpVersion Nov 24, 2026
@Sergio0694
Sergio0694 marked this pull request as ready for review November 24, 2026 22:35
The code that is identical in the AnimatedVisual classes of the versions of an
IAnimatedVisualSource was moved to a shared base class by a post processor that
parsed the text of the generated classes with regular expressions. The
generator now builds a model of each class before it writes any code, and the
sharing works on the model:
- fields are AnimatedVisualField and methods are AnimatedVisualMethod, with
  their comment, attribute, header and body kept apart. Comment lines are
  flagged by CodeBuilder when they are written, so that methods are equal if
  their attribute, signature and code are the same, whatever their comments;
- each method records the fields and methods of its class that it uses, at
  the points where the generator writes their names: the factory call
  helpers (the cache of factory calls stores the uses with the code, and
  records them again on a hit), the field helpers, the helper methods and the
  local variable of the animations, which is a LocalReference so that the field
  of an object is recorded exactly when it is written into a method, and not
  when it is written into the CreateAnimations method;
- SharedAnimatedVisualMembers works out which methods, fields and abstract
  declarations the base class has from those uses, as the post processor did;
- the C# generator writes the shared base class and the classes from the model.
  The default writer in the base generator writes the classes as before.

CSharpSharedCodeExtractor, which read the generated text, is removed, and its
unit tests are replaced by tests on the model.

The generated code is unchanged: the 272 files generated for the 16 Microsoft
Store icons with the option sets for WinUI 2.6, 2.8 and 3.0, no minimum UAP
version, -DisableCodeGenOptimizer, -GenerateColorBindings,
-GenerateDependencyObject, C# 7.3 and C# 12, C++/CX and C++/WinRT are
identical to the output of the previous commit.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

@aborziak-ms aborziak-ms left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@aborziak-ms
aborziak-ms merged commit 20c434c into main Nov 25, 2026
4 checks passed
@Sergio0694
Sergio0694 deleted the user/sergiopedri/lottiegen-size-optimization branch November 25, 2026 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants