Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use ILGenerator instead of Expression #18

Merged
merged 12 commits into from
Mar 18, 2022
Merged

Use ILGenerator instead of Expression #18

merged 12 commits into from
Mar 18, 2022

Conversation

Nyrest
Copy link
Owner

@Nyrest Nyrest commented Mar 15, 2022

Expected & Implemented

  • Faster constructor
  • Smaller assembly size
  • No runtime performance penalty

Issues

  • ILGenerator is slower than Expression on .NET Framework.
    • We should use Expression on .NET FX and use ILGenerator on .NET Core

Benchmark result of the static constructors:

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
AMD Ryzen 9 3900X, 1 CPU, 24 logical and 12 physical cores
.NET SDK=7.0.100-preview.1.22110.4
  [Host]     : .NET 6.0.3 (6.0.322.12309), X64 RyuJIT
  Job-RVXJKB : .NET 5.0.15 (5.0.1522.11506), X64 RyuJIT
  Job-CJDGIM : .NET 6.0.3 (6.0.322.12309), X64 RyuJIT
  Job-BTQWWL : .NET Framework 4.8 (4.8.4470.0), X64 RyuJIT

Method Job Runtime Mean Error StdDev Ratio RatioSD Gen 0 Gen 1 Allocated
CCtorIL Job-RVXJKB .NET 5.0 35.67 μs 0.266 μs 0.207 μs 0.92 0.01 0.1221 0.0610 1 KB
CCtorExpr Job-RVXJKB .NET 5.0 38.83 μs 0.364 μs 0.340 μs 1.00 0.00 0.4272 0.1831 4 KB
CCtorIL Job-CJDGIM .NET 6.0 31.79 μs 0.501 μs 0.469 μs 0.93 0.02 0.1221 0.0610 1 KB
CCtorExpr Job-CJDGIM .NET 6.0 34.03 μs 0.244 μs 0.216 μs 1.00 0.00 0.4272 0.1831 4 KB
CCtorIL Job-BTQWWL .NET Framework 4.8 46.18 μs 0.214 μs 0.201 μs 0.99 0.01 0.6104 0.3052 4 KB
CCtorExpr Job-BTQWWL .NET Framework 4.8 46.44 μs 0.212 μs 0.199 μs 1.00 0.00 0.6104 0.3052 4 KB

@Nyrest Nyrest added the enhancement New feature or request label Mar 15, 2022
@Nyrest Nyrest added this to the 3.1.0 milestone Mar 15, 2022
@Nyrest Nyrest self-assigned this Mar 15, 2022
@Nyrest Nyrest changed the title Use ILGenerator instead of Expression [WIP] Use ILGenerator instead of Expression Mar 15, 2022
@Nyrest
Copy link
Owner Author

Nyrest commented Mar 15, 2022

image
For some unknown reason, ILEmit is always slower than Expression.
This is obviously not worth switching to ILGenerator.

This will need more investigation.

Resolved.

@Nyrest
Copy link
Owner Author

Nyrest commented Mar 17, 2022

It seems the DynamicMethod needs to be bound to an object to get the best performance

@Nyrest Nyrest marked this pull request as ready for review March 18, 2022 05:27
@Nyrest Nyrest changed the title [WIP] Use ILGenerator instead of Expression Use ILGenerator instead of Expression Mar 18, 2022
@Nyrest Nyrest merged commit 9610d3c into main Mar 18, 2022
Nyrest added a commit that referenced this pull request Apr 15, 2022
Use ILGenerator instead of Expression
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant