Bug 7688 - Unwanted boxing by the runtime for generics
Summary: Unwanted boxing by the runtime for generics
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: unspecified
Hardware: PC Mac OS
: --- major
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-10-05 13:05 UTC by Marek Safar
Modified: 2012-10-14 07:54 UTC (History)
3 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-10-05 13:05:27 UTC
using System;
using System.Diagnostics;

struct Record : Foo<Record>.IRecord {
    int idx;
    int Foo<Record>.IRecord.Key { get { return idx; } }

class Foo<T> where T : Foo<T>.IRecord {
    public interface IRecord {
        int Key { get; }

    public static int Extract (T[] t) {
        int a = t[0].Key;
        if (a > 10)
            return 100;
            return 200;

class Driver {
    static void Main () {
    	int v = 1;
    	var sw = new Stopwatch ();
    	sw.Start ();
    	for (int i = 0; i < 10000000; ++i)
        	v = Foo<Record>.Extract (new Record [10]);

        sw.Stop ();
        Console.WriteLine (sw.ElapsedMilliseconds);

        Console.WriteLine (v);

This is extract from mcs source code where I tried to track down why we are slower on mono. The code is used in quite few mcs hot paths (binary search) for loading assemblies.

.NET: 109
Mono: 2640  ~25x slower
Comment 1 Zoltan Varga 2012-10-11 14:17:18 UTC
The testcase allocates an object inside the loop, changing it to:
		var o = new Record [10];
        for (int i = 0; i < 10000000; ++i)
            v = Foo<Record>.Extract (o);
makes it run in 30ms for me.
Comment 2 Marek Safar 2012-10-14 07:54:19 UTC
Yeah, the test in invalid I screwed up when trying to reproduce it from scratch, will try it again