Bug 4189 - Delegate invocation over value type is slow
Summary: Delegate invocation over value type is slow
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-04-02 13:26 UTC by Marek Safar
Modified: 2013-08-23 12:59 UTC (History)
4 users (show)

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 Marek Safar 2012-04-02 13:26:45 UTC
using System;
using System.Diagnostics;

namespace BoxingTest
	public interface ICriticalNotifyCompletion
		void UnsafeOnCompleted(Action a);

	public interface IAsyncStateMachine
		void MoveNext();

	struct Awaiter : ICriticalNotifyCompletion
		public void UnsafeOnCompleted(Action a)

	public struct AsyncVoidMethodBuilder
		public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine)
			where TAwaiter : ICriticalNotifyCompletion
			where TStateMachine : IAsyncStateMachine
			var action = new Action(stateMachine.MoveNext);

	struct Test : IAsyncStateMachine
		public static int counter;

		public void MoveNext()

		public void Entry ()
			var s = new AsyncVoidMethodBuilder();
			var awaiter = new Awaiter();
			s.AwaitUnsafeOnCompleted(ref awaiter, ref this);

	class Program
		static void Main(string[] args)
			var sw = new Stopwatch();
			for (int i = 0; i < 1000000; ++i)
				new Test().Entry();


On amd64

.NET: ~150 (but also 40 in one of 3 runs)
Mono: ~1500 (~650 with sgen)
Comment 1 Zoltan Varga 2012-04-03 02:12:10 UTC
The generics stuff in AwaitUnsafeOnCompleted forces us to skip the optimized code path for delegate creation, so we go through the slower path in mono_delegate_trampoline (), which accounts for a large part of the slowdown in this testcase.
Comment 2 Rodrigo Kumpera 2013-08-23 12:59:08 UTC
We now use delegate fast path in generic code too.