Bug 58911 - performance degradation with linq expressions
Summary: performance degradation with linq expressions
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: 5.2 (2017-04)
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Alexander Kyte
Depends on:
Reported: 2017-08-20 13:43 UTC by Jan Tolenaar
Modified: 2017-10-19 19:36 UTC (History)
5 users (show)

See Also:
Tags: bugpool-archive
Is this bug a regression?: ---
Last known good build:


Description Jan Tolenaar 2017-08-20 13:43:01 UTC
The issue is related to #56240. The fix on android was to comment out the SufficientStackCode code in icall.c. To my surprise the #56240 issue was not fixed on linux. I have tried the android fix on linux and it works. But that code was introduced in 2016 for a reason (which I do not know of course), so I prefer another solution. I found a workaround, which is to run the code not on the primary thread but on a secondary thread.

        static void Main(string[] args)
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            var options = RuntimeConsole.ParseArgs(args);

            // Mono5 option is a workaround for a performance problem in the compilation of
            // linq expressions (https://bugzilla.xamarin.com/show_bug.cgi?id=56240).
            // The problem disappears when not running on the primary thread. Therefore this
            // workaround is not a solution for gui programs.

            if (options.Mono5)
                // no stack size change required! 
                var t = new Thread(() => RuntimeConsole.RunConsoleMode(options));

The program fragment shown is from my DLR-based kiezellisp interpreter/compiler (www.kiezellisp.nl) It uses a lot of linq expressions. Startup time went from 0.2 in mono 4.8.6 to 3 seconds in mono 5.2.

I also found that the values returned by Process.UserProcessorTime and Process.PrivilegedProcessorTime have changed considerably between mono 4.8.6 and mono 5.2. In the latter case 99% of the time goes to privileged time. There is clearly something strange going on and it may very well be the cause of #56240.
Comment 1 Zoltan Varga 2017-08-21 03:12:36 UTC
I can repro this with:
using System;
using System.Threading;
using System.Linq.Expressions;
using System.Diagnostics;

class Driver {
	static void Main () {
		Stopwatch timer;
                int counter = 0;
                timer = new Stopwatch();

                while (counter < 10000)
                    BinaryExpression be = Expression.Power(Expression.Constant(2D), Expression.Constant(3D));

                    Expression<Func<double>> le = Expression.Lambda<Func<double>>(be);

                    Func<double> compiledExpression = le.Compile();

                    double result = compiledExpression();


This takes 8s to run on a linux vm, commenting out the SufficientExecutionStack icall cuts the runtime to 0.7s.
Comment 2 Alexander Kyte 2017-09-11 20:40:10 UTC
After some investigation, the best solution looks like just disabling this on linux as well as android. 

Comment 3 Jan Tolenaar 2017-09-12 20:04:26 UTC
That fix solves the performance problem, but it does not address the two threading anomalies that I mentioned: (1) Difference between primary thread and secondary thread. (2) User processor time changing into privileged processor time.

Notice (2018-05-21): bugzilla.xamarin.com will be switching to read-only mode on Thursday, 2018-05-25 22:00 UTC.

Please join us on Visual Studio Developer Community and GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs and copy them to the new locations as needed for follow-up. The See Also field on each Bugzilla bug will be updated with a link to its new location when applicable.

After Bugzilla is read-only, if you have new information to add for a bug that does not yet have a matching issue on Developer Community or GitHub, you can create a follow-up issue in the new location. Copy and paste the title and description from this bug, and then add your new details. You can get a pre-formatted version of the title and description here:

In special cases you might also want the comments:

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Note You need to log in before you can comment on or make changes to this bug.