{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/17065","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/17065/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/17065/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/17065/events","html_url":"https://github.com/dotnet/coreclr/issues/17065","id":306885470,"number":17065,"title":"Span-perf for copy via loop much slower than array-version","user":{"login":"gfoidl","id":5755834,"avatar_url":"https://avatars2.githubusercontent.com/u/5755834?v=4","gravatar_id":"","url":"https://api.github.com/users/gfoidl","html_url":"https://github.com/gfoidl","followers_url":"https://api.github.com/users/gfoidl/followers","following_url":"https://api.github.com/users/gfoidl/following{/other_user}","gists_url":"https://api.github.com/users/gfoidl/gists{/gist_id}","starred_url":"https://api.github.com/users/gfoidl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gfoidl/subscriptions","organizations_url":"https://api.github.com/users/gfoidl/orgs","repos_url":"https://api.github.com/users/gfoidl/repos","events_url":"https://api.github.com/users/gfoidl/events{/privacy}","received_events_url":"https://api.github.com/users/gfoidl/received_events","type":"User","site_admin":false},"labels":[{"id":196940911,"url":"https://api.github.com/repos/dotnet/coreclr/labels/area-CodeGen","name":"area-CodeGen","color":"d4c5f9","default":false},{"id":190283555,"url":"https://api.github.com/repos/dotnet/coreclr/labels/optimization","name":"optimization","color":"0052cc","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/dotnet/coreclr/milestones/5","html_url":"https://github.com/dotnet/coreclr/milestone/5","labels_url":"https://api.github.com/repos/dotnet/coreclr/milestones/5/labels","id":1328081,"number":5,"title":"Future","description":"Milestone tracking issues not triaged to a planned release. Work in this milestone may happen, or may not: it is not committed.","creator":{"login":"joshfree","id":1302850,"avatar_url":"https://avatars1.githubusercontent.com/u/1302850?v=4","gravatar_id":"","url":"https://api.github.com/users/joshfree","html_url":"https://github.com/joshfree","followers_url":"https://api.github.com/users/joshfree/followers","following_url":"https://api.github.com/users/joshfree/following{/other_user}","gists_url":"https://api.github.com/users/joshfree/gists{/gist_id}","starred_url":"https://api.github.com/users/joshfree/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joshfree/subscriptions","organizations_url":"https://api.github.com/users/joshfree/orgs","repos_url":"https://api.github.com/users/joshfree/repos","events_url":"https://api.github.com/users/joshfree/events{/privacy}","received_events_url":"https://api.github.com/users/joshfree/received_events","type":"User","site_admin":false},"open_issues":1322,"closed_issues":1027,"state":"open","created_at":"2015-09-29T00:26:07Z","updated_at":"2018-03-20T18:05:07Z","due_on":null,"closed_at":null},"comments":9,"created_at":"2018-03-20T14:18:35Z","updated_at":"2018-03-20T18:35:16Z","closed_at":null,"author_association":"NONE","body":"# Description\r\n\r\nIn a simple copy-loop Span is a lot slower than an array-version.\r\nI'd expect that Span and array have similar perf.\r\n\r\nNote: `Span_CopyTo` is just for reference included.\r\n\r\n# Benchmark\r\n\r\n## Results\r\n\r\n``` ini\r\n\r\nBenchmarkDotNet=v0.10.13, OS=Windows 10 Redstone 3 [1709, Fall Creators Update] (10.0.16299.309)\r\nIntel Core i7-7700HQ CPU 2.80GHz (Kaby Lake), 1 CPU, 8 logical cores and 4 physical cores\r\nFrequency=2742189 Hz, Resolution=364.6722 ns, Timer=TSC\r\n.NET Core SDK=2.1.300-preview3-008384\r\n  [Host]     : .NET Core 2.1.0-preview2-26313-01 (CoreCLR 4.6.26310.01, CoreFX 4.6.26313.01), 64bit RyuJIT\r\n  DefaultJob : .NET Core 2.1.0-preview2-26313-01 (CoreCLR 4.6.26310.01, CoreFX 4.6.26313.01), 64bit RyuJIT\r\n\r\n\r\n```\r\n|      Method |     Mean |     Error |    StdDev | Scaled |\r\n|------------ |---------:|----------:|----------:|-------:|\r\n|       Array | 4.435 us | 0.0400 us | 0.0374 us |   1.00 |\r\n|        Span | 6.885 us | 0.0307 us | 0.0256 us |   1.55 |\r\n| Span_CopyTo | 1.132 us | 0.0072 us | 0.0067 us |   0.26 |\r\n\r\n## Code\r\n\r\n```c#\r\npublic class Benchmarks\r\n{\r\n    private readonly int[] _source;\r\n    private readonly int[] _destination;\r\n\r\n    public Benchmarks()\r\n    {\r\n        _source = Enumerable.Range(0, 10_000).ToArray();\r\n        _destination = new int[_source.Length];\r\n    }\r\n\r\n    [Benchmark(Baseline = true)]\r\n    public void Array()\r\n    {\r\n        Copy(_source, _destination);\r\n    }\r\n\r\n    [Benchmark]\r\n    public void Span()\r\n    {\r\n        Copy(_source.AsSpan(), _destination.AsSpan());\r\n    }\r\n\r\n    [Benchmark]\r\n    public void Span_CopyTo()\r\n    {\r\n        _source.AsSpan().CopyTo(_destination.AsSpan());\r\n    }\r\n\r\n    [MethodImpl(MethodImplOptions.NoInlining)]\r\n    private static void Copy(int[] src, int[] dst)\r\n    {\r\n        for (int i = 0; i < src.Length; ++i)\r\n            dst[i] = src[i];\r\n    }\r\n\r\n    [MethodImpl(MethodImplOptions.NoInlining)]\r\n    private static void Copy(ReadOnlySpan<int> src, Span<int> dst)\r\n    {\r\n        for (int i = 0; i < src.Length; ++i)\r\n            dst[i] = src[i];\r\n    }\r\n}\r\n```\r\n\r\n# Disassembly\r\n\r\nNote: The JitDisasm is produced of fresh coreclr-build on ubuntu.\r\n<details>\r\n  <summary>dotnet --info</summary>\r\n    \r\n\t.NET Core SDK (reflecting any global.json):\r\n     Version:   2.1.300-preview3-008384\r\n     Commit:    4343118151\r\n\r\n    Runtime Environment:\r\n     OS Name:     ubuntu\r\n     OS Version:  16.04\r\n     OS Platform: Linux\r\n     RID:         ubuntu.16.04-x64\r\n     Base Path:   /usr/share/dotnet/sdk/2.1.300-preview3-008384/\r\n\r\n    Host (useful for support):\r\n      Version: 2.1.0-preview3-26319-04\r\n      Commit:  939333dbc8\r\n\r\n    .NET Core SDKs installed:\r\n      2.1.4 [/usr/share/dotnet/sdk]\r\n      2.1.300-preview3-008384 [/usr/share/dotnet/sdk]\r\n\r\n    .NET Core runtimes installed:\r\n      Microsoft.AspNetCore.All 2.1.0-preview2-30338 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]\r\n      Microsoft.AspNetCore.App 2.1.0-preview2-30338 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]\r\n      Microsoft.NETCore.App 2.0.5 [/usr/share/dotnet/shared/Microsoft.NETCore.App]\r\n      Microsoft.NETCore.App 2.1.0-preview2-26313-01 [/usr/share/dotnet/shared/Microsoft.NETCore.App]\r\n      Microsoft.NETCore.App 2.1.0-preview3-26319-04 [/usr/share/dotnet/shared/Microsoft.NETCore.App]\r\n</details>\r\n\r\nAlso only the loops are shown.\r\n\r\n## Array-variant\r\n\r\n```asm\r\nG_M16768_IG04:\r\n       4863C8               movsxd   rcx, eax\r\n       448B448F10           mov      r8d, dword ptr [rdi+4*rcx+16]\r\n       4489448E10           mov      dword ptr [rsi+4*rcx+16], r8d\r\n       FFC0                 inc      eax\r\n       3BD0                 cmp      edx, eax\r\n       7FED                 jg       SHORT G_M16768_IG04\r\n```\r\n\r\n## Span-variant\r\n\r\n```asm\r\nG_M16768_IG03:\r\n       413BDE               cmp      ebx, r14d\r\n       732D                 jae      SHORT G_M16768_IG05\r\n       4863FB               movsxd   rdi, ebx\r\n       3B5DE0               cmp      ebx, dword ptr [rbp-20H]\r\n       7325                 jae      SHORT G_M16768_IG05\r\n       488B45D8             mov      rax, bword ptr [rbp-28H]\r\n       8B04B8               mov      eax, dword ptr [rax+4*rdi]\r\n       418904BF             mov      dword ptr [r15+4*rdi], eax\r\n       FFC3                 inc      ebx\r\n       488D7DD8             lea      rdi, bword ptr [rbp-28H]\r\n       E8278F5CFF           call     System.ReadOnlySpan`1[Int32][System.Int32]:get_Length():int:this\r\n       3BC3                 cmp      eax, ebx\r\n       7FD9                 jg       SHORT G_M16768_IG03\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/374710008","html_url":"https://github.com/dotnet/coreclr/issues/17065#issuecomment-374710008","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/17065","id":374710008,"user":{"login":"gfoidl","id":5755834,"avatar_url":"https://avatars2.githubusercontent.com/u/5755834?v=4","gravatar_id":"","url":"https://api.github.com/users/gfoidl","html_url":"https://github.com/gfoidl","followers_url":"https://api.github.com/users/gfoidl/followers","following_url":"https://api.github.com/users/gfoidl/following{/other_user}","gists_url":"https://api.github.com/users/gfoidl/gists{/gist_id}","starred_url":"https://api.github.com/users/gfoidl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gfoidl/subscriptions","organizations_url":"https://api.github.com/users/gfoidl/orgs","repos_url":"https://api.github.com/users/gfoidl/repos","events_url":"https://api.github.com/users/gfoidl/events{/privacy}","received_events_url":"https://api.github.com/users/gfoidl/received_events","type":"User","site_admin":false},"created_at":"2018-03-20T18:35:16Z","updated_at":"2018-03-20T18:35:16Z","author_association":"NONE","body":"@mikedn thx for your https://github.com/dotnet/coreclr/issues/17065#issuecomment-374705550\r\nWhen I view the disassembly in VS while debugging (Release + suppress JIT optimizations unchecked, ...) then I'll see equal code (Windows)\r\n\r\n@AndyAyersMS I followed the steps described in [Viewing JIT Dumps](https://github.com/dotnet/coreclr/blob/master/Documentation/building/viewing-jit-dumps.md). Built from https://github.com/dotnet/coreclr/commit/ff2abe597b602c5228bae66c9f3f581a632ff4fb\r\nIf this is the wrong way, please point me to the adequate way."}}
{"actor":{"display_login":"gfoidl"}}