Sunday, April 6, 2025

Optimizing Expression Tree Variable Extraction in .NET

In one of my recent experiments, I explored the performance of extracting captured variable values from LINQ expression trees. This is a common need when you're working with LINQ providers or building your own ORM.

For example, consider the following expression:

int minAge = 30;
Expression<Func<User, bool>> expr = x => x.Age > minAge;
  

While it's easy to cache the translated SQL to avoid repeated query generation, we still need to extract the current value of minAge every time the expression is executed. This extraction requires walking through the expression tree to locate and resolve the captured variables.

I compared two approaches for extracting these variable values:

  • Visitor-Based Traversal: Traditional approach using ExpressionVisitor to walk the tree and manually check for relevant node types.
  • Cached Traversal Path: My approach, where the tree is analyzed once to build a path map to variable nodes, and that path is used for fast extraction.

The project is available as a study on GitHub:

https://github.com/sallushan/linq-variable-finder

Here's the benchmark comparison I got using BenchmarkDotNet:

| Method                    | Mean     | StdDev   |
|---------------------------|---------:|---------:|
| VisitorBasedExtraction    | 703.2 ns | 15.81 ns |
| CachedTraversalExtraction | 374.1 ns |  5.60 ns |
  

As seen above, the cached traversal approach provides a significant performance gain and keeps your tree navigation logic clean and separated.

This is not a reusable library (yet), but rather a focused study that I plan to integrate into my ORM project in the future.

Hope it helps someone looking into similar expression tree scenarios.

Monday, March 17, 2025

Atis ORM is now public

Atis ORM is a lightweight LINQ to SQL conversion system. I've been working on this ORM from past several years and an older version of this ORM is currently being used in a complex ERP system.

It's still under development but I have decided to make it public https://github.com/atis-orm/atis-orm.

Wednesday, May 17, 2023

Crystal Reports font problem in Exported PDF

So, we had this issue, that one of our Web App running on Server was generating a PDF file using Crystal Reports Export API. The Crystal Reports file had barcode fonts to generate barcodes. But the exported PDF was showing the normal Arial font instead of barcodes.

First I investigated that PDF file itself, whether barcode fonts are being embedded in the PDF file or not. So, I realized that instead of barcode fonts, it has Arial font only. So Crystal Reports export system was not embedding the Barcode Fonts during PDF file creation process.

Though we had already installed the fonts on the server, but the font was installed for the user which was logged in at the time of installation. So, I right clicked on the font file and clicked on Install for all Users. This resolved the issue and barcode fonts started to appear in PDF.

Note that this problem can occur for other reasons as well, for example, the fonts are secured and they are not allowed to be embedded. But in my case installing the fonts for all users resolved the problem.