Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6c3f73a
LINQ: Fixes memory leak from Expression.Compile() in SubtreeEvaluator
kirankumarkolli Feb 2, 2026
b6f882f
Move benchmark test to Performance.Tests project
kirankumarkolli Feb 2, 2026
8ec3864
Making lambda runtime selection instead of comile time
kirankumarkolli Mar 18, 2026
f4055b6
Merge branch 'master' into users/kirankk/copilot-5487-fix-linq-memory…
kirankumarkolli Mar 18, 2026
a866fdf
Benchmark: use SubtreeEvaluator directly instead of duplicating fix
kirankumarkolli Mar 18, 2026
6110c1a
Merge branch 'master' into users/kirankk/copilot-5487-fix-linq-memory…
kirankumarkolli Mar 18, 2026
d1445f4
LINQ: Apply Compile(preferInterpretation) fix to all Expression.Compi…
kirankumarkolli Mar 18, 2026
6d34f4a
Merge PR #5703 into #5588: consolidate all Expression.Compile() fixes
kirankumarkolli Mar 19, 2026
c063cf1
Removing not needed files
kirankumarkolli Mar 19, 2026
aa12c80
Add ENV variable feature flag for LINQ expression interpretation mode
kirankumarkolli Mar 19, 2026
698cce0
Move feature flag check to CompileLambda for runtime toggling
kirankumarkolli Mar 19, 2026
e5390e6
Cache ENV variable read at static init for zero per-call overhead
kirankumarkolli Mar 19, 2026
2e0c232
Address review: add generic CompileLambda<T> and memory benchmark
kirankumarkolli Mar 26, 2026
72b8a02
Address review: add generic CompileLambda<T>, E2E query generation be…
kirankumarkolli Mar 26, 2026
67aeab1
Simplify: remove ExpressionCompileHelper, inline Compile(preferInterp…
kirankumarkolli Mar 26, 2026
90d39db
Merge branch 'master' into users/kirankk/copilot-5487-fix-linq-memory…
kirankumarkolli Mar 26, 2026
469230c
Add guard test to prevent bare .Compile() in LINQ source files
kirankumarkolli Mar 27, 2026
e4de241
Removing dead code
kirankumarkolli Mar 28, 2026
bd23026
Adding NativeMemoryProfiler
kirankumarkolli Mar 28, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Microsoft.Azure.Cosmos/src/Linq/DocumentQueryEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private static LinqQueryOperation HandleAsSqlTransformExpression(MethodCallExpre
{
LambdaExpression lambdaExpression = (LambdaExpression)paramExpression;
// Send the lambda expression through the partial evaluator.
return GetSqlQuerySpec(lambdaExpression.Compile().DynamicInvoke(null));
return GetSqlQuerySpec(lambdaExpression.Compile(preferInterpretation: true).DynamicInvoke(null));
}
else if (paramExpression.NodeType == ExpressionType.Constant)
{
Expand All @@ -119,7 +119,7 @@ private static LinqQueryOperation HandleAsSqlTransformExpression(MethodCallExpre
else
{
LambdaExpression lamdaExpression = Expression.Lambda(paramExpression);
return GetSqlQuerySpec(lamdaExpression.Compile().DynamicInvoke(null));
return GetSqlQuerySpec(lamdaExpression.Compile(preferInterpretation: true).DynamicInvoke(null));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static SqlScalarExpression Construct(Expression geometryExpression)
try
{
Expression<Func<Geometry>> le = Expression.Lambda<Func<Geometry>>(geometryExpression);
Func<Geometry> compiledExpression = le.Compile();
Func<Geometry> compiledExpression = le.Compile(preferInterpretation: true);
geometry = compiledExpression();
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Linq/SubtreeEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private Expression EvaluateConstant(Expression expression)
}

LambdaExpression lambda = Expression.Lambda(expression);
Delegate function = lambda.Compile();
Delegate function = lambda.Compile(preferInterpretation: true);

return Expression.Constant(function.DynamicInvoke(null), expression.Type);
}
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Linq/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public override object EvalBoxed(Expression expr)
public T Eval(Expression expr)
{
Expression<Func<T>> lambda = Expression.Lambda<Func<T>>(expr);
Func<T> func = lambda.Compile();
Func<T> func = lambda.Compile(preferInterpretation: true);
return func();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// ----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ----------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Linq
{
using System;
using System.Linq.Expressions;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnostics.Windows.Configs;

/// <summary>
/// Benchmark comparing Expression.Compile() vs Compile(preferInterpretation: true)
/// in the context of CosmosDB LINQ-to-SQL query generation.
/// Validates fix for GitHub Issue #5487: Unbounded JIT/IL growth from Expression.Compile().
///
/// [MemoryDiagnoser] reports managed GC allocations (Gen0/Gen1/Allocated columns).
/// [NativeMemoryProfiler] uses ETW to track native memory allocations and leaks per method,
/// adding "Allocated native memory" and "Native memory leak" columns to the results table.
/// Note: NativeMemoryProfiler requires Windows and elevated (admin) privileges.
/// </summary>
[ShortRunJob]
[MemoryDiagnoser]
// [NativeMemoryProfiler] // Enable this line to include native memory profiling, requires Windows and admin privileges.
public class SubtreeEvaluatorBenchmark
Comment thread
xinlian12 marked this conversation as resolved.
Comment thread
kirankumarkolli marked this conversation as resolved.
{
private LambdaExpression lambda;

[GlobalSetup]
public void Setup()
{
string status = "active";
Expression<Func<bool>> expr = () => status == "active";
this.lambda = Expression.Lambda(expr.Body);
}

[Benchmark(Baseline = true)]
public object Compile()
{
Delegate fn = this.lambda.Compile();
return fn.DynamicInvoke(null);
}

[Benchmark]
public object CompileWithInterpretation()
{
Delegate fn = this.lambda.Compile(preferInterpretation: true);
return fn.DynamicInvoke(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Linq
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// Guards against introducing bare Expression.Compile() calls in the LINQ provider.
/// All Compile() calls must use Compile(preferInterpretation: true) to avoid native
/// memory leaks from DynamicMethod IL emission.
/// See: https://github.com/Azure/azure-cosmos-dotnet-v3/issues/5487
/// </summary>
[TestClass]
public class LinqCompileGuardTests
{
// Matches .Compile() with no arguments — the problematic pattern
private static readonly Regex BareCompilePattern = new Regex(
@"\.Compile\(\s*\)",
RegexOptions.Compiled);

// Matches .Compile(preferInterpretation: true) — the correct pattern
private static readonly Regex InterpretedCompilePattern = new Regex(
@"\.Compile\(\s*preferInterpretation\s*:\s*true\s*\)",
RegexOptions.Compiled);

[TestMethod]
public void LinqSourceFiles_ShouldNotUseBareCompile()
{
string linqDirectory = Path.GetFullPath(
Path.Combine(
Directory.GetCurrentDirectory(),
"..", "..", "..", "..", "..",
"src", "Linq"));

Assert.IsTrue(
Directory.Exists(linqDirectory),
$"LINQ source directory not found at: {linqDirectory}");

string[] sourceFiles = Directory.GetFiles(linqDirectory, "*.cs", SearchOption.AllDirectories);
Assert.IsTrue(sourceFiles.Length > 0, "No source files found in LINQ directory.");

List<string> violations = new List<string>();

foreach (string file in sourceFiles)
{
string[] lines = File.ReadAllLines(file);
string fileName = Path.GetFileName(file);

for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];

if (BareCompilePattern.IsMatch(line) && !InterpretedCompilePattern.IsMatch(line))
{
violations.Add($" {fileName}:{i + 1} => {line.Trim()}");
}
}
}

Assert.AreEqual(
0,
violations.Count,
$"Found bare .Compile() calls without preferInterpretation: true. " +
$"Use .Compile(preferInterpretation: true) to avoid native memory leaks " +
$"(see issue #5487):\n{string.Join("\n", violations)}");
}
}
}
Loading