Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

why mono runtime's performance is lowwer than java(or dotnetcore) runtime?can you(or we) optimize mono runtime #6719

Closed
introspection3 opened this issue Jan 30, 2018 · 9 comments

Comments

@introspection3
Copy link

introspection3 commented Jan 30, 2018

why mono runtime's performance is slower than java(or dotnetcore) runtime?can you(or we) optimize mono runtime

@lewurm
Copy link
Contributor

lewurm commented Jan 30, 2018

It depends on the workload. Please post a specific (minimal) program where you experience performance issues, so we can look into it.

@lewurm lewurm closed this as completed Jan 30, 2018
@introspection3
Copy link
Author

please see this
https://github.com/kostya/benchmarks

@introspection3
Copy link
Author

please reopen this issue

@introspection3
Copy link
Author

@lewurm please reopen this issue
please see this
https://github.com/kostya/benchmarks

@Therzok
Copy link
Contributor

Therzok commented Jan 30, 2018

I did a quick look at the bf2 benchmark, and I saw the following:

Kotlin does a dry warming run, so it doesn't include JIT time: https://github.com/kostya/benchmarks/blob/master/brainfuck2/bf2.kt#L89

The C# version does not:
https://github.com/kostya/benchmarks/blob/master/brainfuck2/bf.cs#L85

And the build and run file:
https://github.com/kostya/benchmarks/blob/master/brainfuck2/build.sh#L5-L6

On Mono, it uses mcs -debug -optimize+ (thus debug code), instead of csc /optimize+, while on .NET Core it uses a release build.

Edit: mcs -optimize+ is mostly a NOP.

@Therzok
Copy link
Contributor

Therzok commented Jan 30, 2018

I'd say the benchmarks are rather incorrectly setup and don't reflect real stats between different runtimes.

It also uses mono -O=all, instead of the default set of optimizations, which can incur JIT overhead.

@Therzok
Copy link
Contributor

Therzok commented Jan 30, 2018

On my machine using mcs:

➜  brainfuck2 git:(master) time mono ./bf.exe bench.b
ZYXWVUTSRQPONMLKJIHGFEDCBA
mono ./bf.exe bench.b  4.36s user 0.01s system 99% cpu 4.384 total

With csc /optimize+:

➜  brainfuck2 git:(master) time mono ./bf.exe bench.b
ZYXWVUTSRQPONMLKJIHGFEDCBA
mono ./bf.exe bench.b  4.34s user 0.02s system 99% cpu 4.371 total

Then I looked at the IL generated by that code, and noticed that the hot functions in the code are not inlined at all:

diff --git a/brainfuck2/bf.cs b/brainfuck2/bf.cs
index a93e6a3..a411c46 100644
--- a/brainfuck2/bf.cs
+++ b/brainfuck2/bf.cs
@@ -2,6 +2,7 @@ using System;
 using System.IO;
 using System.Linq;
 using System.Collections.Generic;
+using System.Runtime.CompilerServices;
 using System.Text;
 
 namespace Test
@@ -29,7 +30,9 @@ namespace Test
         }
 
         public int Get() { return tape[pos]; }
+		[MethodImpl(MethodImplOptions.AggressiveInlining)]
         public void Inc(int x) { tape[pos] += x; }
+		[MethodImpl(MethodImplOptions.AggressiveInlining)]
         public void Move(int x) { pos += x; while (pos >= tape.Length) Array.Resize(ref tape, tape.Length*2); }
     }

So I applied this patch and the runtime was cut down to:

➜  brainfuck2 git:(master) ✗ time mono ./bf.exe bench.b
ZYXWVUTSRQPONMLKJIHGFEDCBA
mono ./bf.exe bench.b  2.93s user 0.02s system 99% cpu 2.958 total

Even without a warm run.

@Therzok
Copy link
Contributor

Therzok commented Jan 30, 2018

Using the same code:

➜  brainfuck2 git:(master) ✗ time dotnet bin/Release/netcoreapp2.0/brainfuck2.dll bench.b
ZYXWVUTSRQPONMLKJIHGFEDCBA
dotnet bin/Release/netcoreapp2.0/brainfuck2.dll bench.b  2.54s user 0.02s system 99% cpu 2.567 total

I get this with .netcore on the same machine for comparison.

@Therzok
Copy link
Contributor

Therzok commented Jan 30, 2018

And my assumption was right, doing -O=all does increase runtime of the app:

➜  brainfuck2 git:(master) ✗ time mono -O=all ./bf.exe bench.b
ZYXWVUTSRQPONMLKJIHGFEDCBA
mono -O=all ./bf.exe bench.b  3.01s user 0.02s system 99% cpu 3.042 total

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants