Bug 58911 - performance degradation with linq expressions
Summary: performance degradation with linq expressions
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
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)

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

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.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

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.