ConstExpr.SourceGenerator 0.1.0-preview

This is a prerelease version of ConstExpr.SourceGenerator.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package ConstExpr.SourceGenerator --version 0.1.0-preview
                    
NuGet\Install-Package ConstExpr.SourceGenerator -Version 0.1.0-preview
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ConstExpr.SourceGenerator" Version="0.1.0-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ConstExpr.SourceGenerator" Version="0.1.0-preview" />
                    
Directory.Packages.props
<PackageReference Include="ConstExpr.SourceGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ConstExpr.SourceGenerator --version 0.1.0-preview
                    
#r "nuget: ConstExpr.SourceGenerator, 0.1.0-preview"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ConstExpr.SourceGenerator@0.1.0-preview
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ConstExpr.SourceGenerator&version=0.1.0-preview&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=ConstExpr.SourceGenerator&version=0.1.0-preview&prerelease
                    
Install as a Cake Tool

ConstExpr - Compile-Time Expression Optimizer

ConstExpr is a powerful C# source generator that optimizes method calls at compile time, transforming runtime computations into compile-time constants for improved performance.

🚀 Features

  • Compile-Time Optimization: Converts method calls into optimized, compile-time evaluated expressions
  • LINQ Support: Optimizes LINQ operations like Where, Average, Select, and more
  • Mathematical Operations: Compile-time evaluation of arithmetic, statistics, and mathematical functions
  • String Operations: Optimized string manipulation, encoding, and processing
  • Collection Operations: Enhanced performance for array and collection operations
  • Vector Operations: Hardware-accelerated operations using System.Numerics.Vector
  • Configurable Generation Levels: Choose between minimal, balanced, and performance optimization levels

🔧 Usage

Basic Example

Mark your methods with the [ConstExpr] attribute to enable compile-time optimization:

using ConstantExpression;
using System.Linq;

[ConstExpr]
public static class MathOperations
{
    public static double Average(params double[] values)
    {
        return values.Average();
    }
    
    public static IEnumerable<int> GetEvens(params int[] numbers)
    {
        return numbers.Where(x => x % 2 == 0);
    }
}

// Usage - these calls will be optimized at compile time
var avg = MathOperations.Average(1.0, 2.0, 3.0, 4.0, 5.0);
var evens = MathOperations.GetEvens(1, 2, 3, 4, 5, 6);

Generation Levels

Control the optimization level using the Level property:

[ConstExpr(Level = GenerationLevel.Performance)]
public static class HighPerformanceOps
{
    public static double StandardDeviation(params double[] data)
    {
        var sum = 0d;
        var sumOfSquares = 0d;

        foreach (var item in data)
        {
            sum += item;
            sumOfSquares += item * item;
        }

        var mean = sum / data.Length;
        var variance = sumOfSquares / data.Length - mean * mean;
        return Math.Sqrt(variance);
    }
}

Generation Levels:

  • Minimal: Basic optimizations with minimal code generation
  • Balanced: Default level with good balance of performance and code size
  • Performance: Maximum optimization with potential for larger generated code

Advanced Examples

String Operations
[ConstExpr]
public static class StringOps
{
    public static int GetByteCount(string text, Encoding encoding)
    {
        return encoding.GetByteCount(text);
    }
    
    public static string EncodeBase64(string input)
    {
        return Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
    }
}
Mathematical Computations
[ConstExpr]
public static class Math
{
    public static IEnumerable<int> Fibonacci(int count)
    {
        if (count <= 0) yield break;
        
        int a = 0, b = 1;
        yield return a;
        
        for (int i = 1; i < count; i++)
        {
            yield return b;
            (a, b) = (b, a + b);
        }
    }
    
    public static (double H, double S, double L) RgbToHsl(int r, int g, int b)
    {
        double rd = r / 255.0;
        double gd = g / 255.0;
        double bd = b / 255.0;
        
        // HSL conversion logic...
        // This will be computed at compile time!
    }
}
Async Operations
[ConstExpr]
public static class AsyncOps
{
    public static async Task<string> ProcessDataAsync()
    {
        await Task.Delay(100);
        return "Processed";
    }
}

🛠️ Requirements

  • .NET 9.0 or later
  • C# 13 or later (for latest language features)
  • Visual Studio 2022 or JetBrains Rider (recommended)

🎯 Performance Benefits

ConstExpr can provide significant performance improvements:

  • Compile-time evaluation: Eliminates runtime computation overhead
  • Reduced allocations: Optimized memory usage patterns
  • Vector operations: Hardware-accelerated SIMD instructions
  • Loop unrolling: Optimized iteration patterns

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


Note: This library leverages advanced C# compiler features and may require the latest .NET SDK for optimal performance and compatibility.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.19-preview 113 9/2/2025
0.1.18-preview 120 9/1/2025
0.1.17-preview 162 8/27/2025
0.1.16-preview 42 8/23/2025
0.1.15-preview 62 8/15/2025
0.1.14-preview 64 8/15/2025
0.1.13-preview 66 8/15/2025
0.1.12-preview 85 8/15/2025
0.1.11-preview 90 8/15/2025
0.1.10-preview 92 8/15/2025
0.1.9-preview 95 8/15/2025
0.1.8-preview 122 8/14/2025
0.1.7-preview 120 8/14/2025
0.1.6-preview 95 7/31/2025
0.1.5-preview 93 7/31/2025
0.1.4-preview 92 7/30/2025
0.1.3-preview 93 7/30/2025
0.1.2-preview 92 7/30/2025
0.1.1-preview 94 7/30/2025
0.1.0-preview 93 7/30/2025