Lai*_*ith 14 c# struct compiler-optimization .net-native uwp
我在.Net Native和中发现了(可能是)过度优化的问题structs.我不确定编译器是否过于激进,或者我太盲目无法看到我做错了什么.
要重现此,请按照下列步骤操作:
步骤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>
Run Code Online (Sandbox Code Playgroud)
第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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
第4步:运行应用程序Debug x64.你应该看到这个标签:
42.5,42.5 => 107.5,107.5
第5步:运行应用程序Release x64.你应该看到这个标签:
-7.5,-7.5 => 7.5,7.5
第6步:取消line 45中MainPage.xaml.cs,然后重复步骤5.现在你看到原来的标签
42.5,42.5 => 107.5,107.5
通过注释line 45,代码将使用Rectangle2D.Inflate2(...)与完全相同的Rectangle2D.Inflate1(...)除外,它在将它们发送到构造函数之前创建计算的本地副本Rectangle2D.在调试模式下,这两个功能完全相同.然而,在发布中,某些东西正在逐步优化.
这是我们的应用程序中的一个讨厌的错误.你在这里看到的代码是从一个更大的图书馆中删除的,我担心可能会有更多.在我向微软报告这件事之前,如果您能看一眼并告诉我为什么Inflate1在发布模式下不起作用,我将不胜感激.为什么我们要创建本地副本?
我很不清楚为什么这个问题有悬赏。是的,正如 @Matt 告诉你的那样,这是一个错误。他知道,他致力于 .NET Native。他记录了临时解决方法,使用属性来防止优化器内联该方法。这是一个通常可以解决优化器错误的技巧。
using System.Runtime.CompilerServices;
....
[MethodImpl(MethodImplOptions.NoInlining)]
public Rectangle2D Inflate1(double value)
{
// etc...
}
Run Code Online (Sandbox Code Playgroud)
他们会修复它,下一个主要版本是通常的承诺。
| 归档时间: |
|
| 查看次数: |
756 次 |
| 最近记录: |