这是.Net Native编译和优化中可能存在的错误吗?

时间:2016-06-19 14:15:57

标签: c# struct uwp compiler-optimization .net-native

我在.Net Nativestructs中发现了(可能是)过度优化的问题。我不确定编译器是否过于激进,或者我太盲目无法看到我做错了什么。

要重现此操作,请按以下步骤操作:

第1步:在 Visual Studio 2015 Update 2 中创建一个新的Blank Universal(win10)应用,定位版本10586,最小版本为10240.调用项目 NativeBug 所以我们有相同的命名空间。

第2步:打开MainPage.xaml并插入此标签

<Page x:Class="NativeBug.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <!-- INSERT THIS LABEL -->
        <TextBlock x:Name="_Label" HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</Page>

第3步:将以下内容复制/粘贴到MainPage.xaml.cs

using System;
using System.Collections.Generic;

namespace NativeBug
{
    public sealed partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();

            var startPoint = new Point2D(50, 50);
            var points = new[]
            {
                new Point2D(100, 100), 
                new Point2D(100, 50), 
                new Point2D(50, 100), 
            };

            var bounds = ComputeBounds(startPoint, points, 15);

            _Label.Text = $"{bounds.MinX} , {bounds.MinY}   =>   {bounds.MaxX} , {bounds.MaxY}";
        }

        private static Rectangle2D ComputeBounds(Point2D startPoint, IEnumerable<Point2D> points, double strokeThickness = 0)
        {
            var lastPoint = startPoint;
            var cumulativeBounds = new Rectangle2D();

            foreach (var point in points)
            {
                var bounds = ComputeBounds(lastPoint, point, strokeThickness);
                cumulativeBounds = cumulativeBounds.Union(bounds);
                lastPoint = point;
            }

            return cumulativeBounds;
        }

        private static Rectangle2D ComputeBounds(Point2D fromPoint, Point2D toPoint, double strokeThickness)
        {
            var bounds = new Rectangle2D(fromPoint.X, fromPoint.Y, toPoint.X, toPoint.Y);

            // ** Uncomment the line below to see the difference **
            //return strokeThickness <= 0 ? bounds : bounds.Inflate2(strokeThickness);

            return strokeThickness <= 0 ? bounds : bounds.Inflate1(strokeThickness);
        }
    }

    public struct Point2D
    {
        public readonly double X;
        public readonly double Y;

        public Point2D(double x, double y)
        {
            X = x;
            Y = y;
        }
    }

    public struct Rectangle2D
    {
        public readonly double MinX;
        public readonly double MinY;
        public readonly double MaxX;
        public readonly double MaxY;

        private bool IsEmpty => MinX == 0 && MinY == 0 && MaxX == 0 && MaxY == 0;

        public Rectangle2D(double x1, double y1, double x2, double y2)
        {
            MinX = Math.Min(x1, x2);
            MinY = Math.Min(y1, y2);
            MaxX = Math.Max(x1, x2);
            MaxY = Math.Max(y1, y2);
        }

        public Rectangle2D Union(Rectangle2D rectangle)
        {
            if (IsEmpty)
            {
                return rectangle;
            }

            var newMinX = Math.Min(MinX, rectangle.MinX);
            var newMinY = Math.Min(MinY, rectangle.MinY);
            var newMaxX = Math.Max(MaxX, rectangle.MaxX);
            var newMaxY = Math.Max(MaxY, rectangle.MaxY);

            return new Rectangle2D(newMinX, newMinY, newMaxX, newMaxY);
        }

        public Rectangle2D Inflate1(double value)
        {
            var halfValue = value * .5;

            return new Rectangle2D(MinX - halfValue, MinY - halfValue, MaxX + halfValue, MaxY + halfValue);
        }

        public Rectangle2D Inflate2(double value)
        {
            var halfValue = value * .5;
            var x1 = MinX - halfValue;
            var y1 = MinY - halfValue;
            var x2 = MaxX + halfValue;
            var y2 = MaxY + halfValue;

            return new Rectangle2D(x1, y1, x2, y2);
        }
    }
}

第4步:在Debug x64中运行该应用程序。你应该看到这个标签:

  

42.5,42.5 =&gt; 107.5,107.5

第5步:在Release x64中运行该应用程序。你应该看到这个标签:

  

-7.5,-7.5 =&gt; 7.5,7.5

第6步:在line 45中取消注释MainPage.xaml.cs并重复步骤5.现在您看到了原始标签

  

42.5,42.5 =&gt; 107.5,107.5

通过注释line 45,代码将使用与Rectangle2D.Inflate2(...)完全相同的Rectangle2D.Inflate1(...),除非它在将计算发送到{{{}的构造函数之前创建计算的本地副本。 1}}。在调试模式下,这两个功能完全相同。然而,在发布中,有些东西正在逐步优化。

这是我们的应用程序中的一个讨厌的错误。你在这里看到的代码是从一个更大的图书馆中删除的,我担心可能会有更多。在我向微软报告此消息之前,如果你能看一下并告诉我为什么Rectangle2D在发布模式下不起作用,我将不胜感激。为什么我们要创建本地副本?

1 个答案:

答案 0 :(得分:4)

我不清楚为什么这个问题有赏金。是的,这是@Matt告诉你的一个错误。他知道,他在.NET Native上工作。他记录了临时解决方法,使用一个属性来防止该方法被优化器内联。一个经常用于绕过优化器错误的技巧。

using System.Runtime.CompilerServices;
....
    [MethodImpl(MethodImplOptions.NoInlining)]
    public Rectangle2D Inflate1(double value)
    {
        // etc...
    }

他们会得到修复,下一个主要版本是通常的承诺。