Fluid Functions, Lombiq Training Demo updates - This week in Orchard (04/02/2022)

Gábor Domonkos's avatar
Performance, This week in Orchard, Liquid

Fluid provides optional support for functions, optimizing SlugServie and MediaTokenService, and hiding a shape sample in the Lombiq Training Demo for Orchard Core are the topics of this week. Check out this post for the details!

Orchard Core updates

Optimize SlugService

The goal here was to increase the performance of the SlugService as much as we can. Marko Lahma went through the thing, and the problem was that the ZString haven't been used, it was leaking all the time thus bad perf.

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
.NET SDK=6.0.101
[Host] : .NET 6.0.1 (6.0.121.56705), X64 RyuJIT
DefaultJob : .NET 6.0.1 (6.0.121.56705), X64 RyuJIT

General easy rule (when implicit usings aren't being used): If there's using System.Linq; at the top of the performance-critical code, try to find a way to get rid of it.

Optimize the SlugService

Reduce allocations in MediaTokenService and improve performance

Since Orchard Core can use all .NET 6 goodness, Marko Lahma improved the performance of the MediaTokenService too. And what did he change?

  • Don't ToString StringValues, keep them as-is as in dictionary once built.
  • Parse commands and other commands with one pass using constructs that QueryHelpers uses.
  • Allocate static command arrays to return from processors, some of them were always returning new ones.
  • Change GetHash not to allocation anonymous closure each time by using Try/Set construct, using the same logic that extension method GetOrCreate uses.
  • Change hash calculation to use stackalloc and spans for workloads < 1024 bytes (common case).
  • Use the custom AddQueryString method that understands spans and concrete a dictionary type.

Optimize the MediaTokenService

Here you can see the numbers:

Before:

The performance of the MediaTokenService before the optimization

After:

The performance of the MediaTokenService after the optimization

Fluid functions

Now there is a new optional feature in Fluid called Functions. It's optional because the goal of Fluid is to be able to as close as possible to Liquid, but Liquid doesn't have Functions. If you open up the readme file of Fluid, you can see a new section there called Functions. There is a new option in the FluidParser that you can say AllowFunctions. By setting the AllowFunctions to true, you can call functions like this: {{ field('user') }}. The format of this one is {{ identifier('arguments')}} just like in JavaScript or in C#.

Or you can say {{ field('pass', type='password') }} which is like {{ identifier('values', type='named value') }}.

Functions in Fluid

How it works is a field is an object, a FluidValue of type FunctionValue. In Fluid, anytime we access something like {% assign name = "Bill" %}, the name is a FluidValue, and in this case, the type is StringValue. When we find a field, the field is a value of type FunctionValue, and the FunctionValue implements the method name InvokeAsync. There is InvokeAsync, there is GetPropertyAsync, etc., everything that an object can do, and when this FunctionValue's Invoke is invoked it will execute something. And to invoke a FluidValue you use parenthesis. If we do parenthesis on something like name, which is a string, we can do name(), it will invoke the string, but it just returns NIL value. So you can invoke name if you want, it will not do anything. If it's a function, it will do something.

To learn more about Functions, check out the documentation of Fluid!

News from the community

Hiding a shape sample in the Lombiq Training Demo for Orchard Core

The Lombiq Training Demo for Orchard Core is a demo Orchard Core CMS module for training purposes guiding you to become an Orchard developer.

And the module has a new sample, which is a shape table provider that hides shapes. Check out the code of the new ShapeHidigingShapeTableProvider here!

Sample for hiding shapes in the Lombiq Training Demo Module

Orchard Dojo Newsletter

Lombiq's Orchard Dojo Newsletter has 240 subscribers! We have started this newsletter to inform the community around Orchard with the latest news about the platform. By subscribing to this newsletter, you will get an e-mail whenever a new post is published to Orchard Dojo, including This week in Orchard of course.

Do you know of other Orchard enthusiasts who you think would like to read our weekly articles? Tell them to subscribe here!

If you are interested in more news around Orchard and the details of the topics above, don't forget to check out the recording of this week's Orchard meeting!

No Comments

Add a Comment