{"id":345617,"date":"2023-02-17T21:02:19","date_gmt":"2023-02-17T21:02:19","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=345617"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2023-02-17T21:02:19","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=345617","title":{"rendered":"<span>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c \u0441\u043b\u0430\u0439\u0441\u0430 \u0432 Go<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442 \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412 \u0441\u0432\u043e\u0435\u0439 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 <a href=\"https:\/\/habr.com\/ru\/post\/716292\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u043f\u0440\u043e \u0440\u0430\u0437\u0431\u043e\u0440 \u043a\u043e\u0434\u0430 \u043f\u043e\u0431\u0435\u0434\u0438\u0432\u0448\u0435\u0433\u043e \u0432 VK Cup&#8217;22\/23 \u044f \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b \u043a\u0430\u043a \u043c\u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0439 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 \u0432 \u0434\u0440\u0443\u0433\u0443\u044e \u0432 30 \u0440\u0430\u0437 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0447\u0451\u0440\u043d\u043e\u0439 \u043c\u0430\u0433\u0438\u0438 <code>unsafe<\/code>. \u041e\u0434\u043d\u0430\u043a\u043e \u044f \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0432\u0430\u043b \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0435\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0435. \u042f \u0434\u0430\u0436\u0435 \u043f\u0440\u0438\u0432\u043b\u0451\u043a OpenAI \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u043d\u043e \u043e\u043d \u043c\u043d\u0435 \u043f\u043e\u043c\u043e\u0433 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u043e\u0439 \u0434\u043b\u044f \u043e\u0431\u043b\u043e\u0436\u043a\u0438 \u0441\u0442\u0430\u0442\u044c\u0438. \u0412 \u0438\u0442\u043e\u0433\u0435 \u044f \u043d\u0430\u0448\u0435\u043b \u0441\u043f\u043e\u0441\u043e\u0431 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043a\u043e\u0434 \u0435\u0449\u0435 \u0432 2 \u0440\u0430\u0437\u0430. \u0427\u0435\u043c \u0438 \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f.<\/p>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430<\/h2>\n<p>\u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u044b\u043b\u0430 \u0432 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u043b\u0430\u0436\u0430 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c 512x512px, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u0432\u0435\u043b\u0430\u0441\u044c \u043a \u0437\u0430\u0434\u0430\u0447\u0435 \u043f\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u0441\u0438\u043d\u0435\u0439 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0439 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 <code>image.RGBA<\/code> \u0432 \u0438\u043d\u0434\u0435\u043a\u0441 \u0446\u0432\u0435\u0442\u0430 \u043f\u0430\u043b\u0438\u0442\u0440\u044b \u0432 <code>image.Paletted<\/code>. \u0414\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u044f \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u043b \u0437\u0430\u0434\u0430\u0447\u0443 \u0434\u043e: \u043d\u0430\u0434\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u043d\u044e\u044e \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0443\u044e \u0438\u0437 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 <code>image.RGBA<\/code> \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c 512x512px \u0432 <code>image.Paletted<\/code> \u0442\u0430\u043a\u043e\u0433\u043e \u0436\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430. <\/p>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043f\u0430\u043a\u0435\u0442\u0430 image \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">func BenchmarkCopySTD(b *testing.B) { for i := 0; i &lt; b.N; i++ { for y := 0; y &lt; imageHeight; y++ { dstPixOffset := dstPaletted.PixOffset(0, y) for x := 0; x &lt; imageWidth; x++ { _, _, b, _ := srcRGBA.At(x, y).RGBA() dstPaletted.Pix[dstPixOffset+x] = uint8(b) } } } }<\/code><\/pre>\n<p>\u041e\u043d\u043e \u0434\u043e\u043b\u0433\u043e\u0435, \u043c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0442\u0440\u0430\u0442\u0438\u0442\u0441\u044f \u043d\u0430 \u0432\u044b\u0437\u043e\u0432\u044b <code>At()<\/code>. \u0422\u0438\u043f\u044b \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a \u043d\u0430\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0441 \u043f\u0438\u043a\u0441\u0435\u043b\u0430\u043c\u0438:<\/p>\n<pre><code class=\"go\">func BenchmarkCopySliceElements(b *testing.B) { for i := 0; i &lt; b.N; i++ { for y := 0; y &lt; imageHeight; y++ { srcPixOffset := srcRGBA.PixOffset(0, y) dstPixOffset := dstPaletted.PixOffset(0, y) for x := 0; x &lt; imageWidth; x++ { dstPaletted.Pix[dstPixOffset+x] = srcRGBA.Pix[srcPixOffset+x*4+1] } } } }<\/code><\/pre>\n<p>\u0421\u0440\u0430\u0437\u0443 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0431\u0443\u0441\u0442:<\/p>\n<pre><code>BenchmarkCopySTD-16                    1590   3730930 ns\/op 1048600 B\/op  262144 allocs\/op BenchmarkCopySliceElements-16         22778    260128 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<p>\u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0447\u0451\u0440\u043d\u0430\u044f \u043c\u0430\u0433\u0438\u044f <code>unsafe<\/code>.<\/p>\n<h2>\u0418\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u0441\u044f \u043e\u0442 Bounds Check<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u0434\u0438\u0432\u0438\u0442\u044c\u0441\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u0434\u043b\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441\u043b\u0430\u0439\u0441\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ad0\/12a\/ca1\/ad012aca1989cb10ab4c8aafe1b798b0.png\" width=\"1012\" height=\"554\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ad0\/12a\/ca1\/ad012aca1989cb10ab4c8aafe1b798b0.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u043d\u0435\u0432\u044b\u0445\u043e\u0434 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0441\u043b\u0430\u0439\u0441\u0430. \u0415\u0451 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e \u043f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u043d\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0435\u0451 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0430\u0434\u0440\u0435\u0441 \u043d\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u041f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">func BenchmarkCopyUnsafe(b *testing.B) { for i := 0; i &lt; b.N; i++ { dstAddr := uintptr(unsafe.Pointer(&amp;dstPaletted.Pix[0])) srcAddr := uintptr(unsafe.Pointer(&amp;srcRGBA.Pix[0])) for y := 0; y &lt; imageHeight; y++ { srcPixOffset := srcRGBA.PixOffset(0, y) dstPixOffset := dstPaletted.PixOffset(0, y) for x := 0; x &lt; imageWidth; x++ { \/\/dst.Pix[dstPixOffset+x] = src.Pix[srcPixOffset+x*4+1] *(*uint8)((unsafe.Pointer)(dstAddr + uintptr(dstPixOffset+x))) = *(*uint8)((unsafe.Pointer)(srcAddr + uintptr(srcPixOffset+x*4+2))) } } } }<\/code><\/pre>\n<p>\u0412 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 (3) \u0438 (4) \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0430\u0434\u0440\u0435\u0441\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0443\u043b\u0435\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0430 \u0434\u0430\u043b\u044c\u0448\u0435 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u043a \u043d\u0438\u043c \u043f\u043e \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0430\u0434\u0440\u0435\u0441\u0430. \u0412\u0440\u0435\u043c\u044f \u043d\u0430 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u043b\u043e\u0441\u044c \u0432 2 \u0440\u0430\u0437\u0430:<\/p>\n<pre><code>BenchmarkCopySTD-16                    1590   3730930 ns\/op 1048600 B\/op  262144 allocs\/op BenchmarkCopySliceElements-16         22778    260128 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnsafe-16                44539    131881 ns\/op       0 B\/op       0 allocs\/op <\/code><\/pre>\n<p>\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 CPU \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u043b\u043e\u0441\u044c:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/fae\/e68\/0c6\/faee680c6acf503c66bfa4e1a2671dfa.png\" width=\"1012\" height=\"419\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fae\/e68\/0c6\/faee680c6acf503c66bfa4e1a2671dfa.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u044b\u043b\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043e \u043d\u0430 \u043a\u043e\u043d\u043a\u0443\u0440\u0441, \u043d\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 \u043c\u0435\u043d\u044f \u0432\u0441\u0451 \u0435\u0449\u0451 \u0441\u043c\u0443\u0449\u0430\u043b\u043e \u0438 \u0443\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u043d\u043a\u0443\u0440\u0441\u0430 \u044f \u043d\u0430\u0447\u0430\u043b \u0438\u0441\u043a\u0430\u0442\u044c \u043f\u0443\u0442\u0438 \u043a\u0430\u043a \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c.<\/p>\n<h2>\u0418\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u0441\u044f \u043e\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0438<\/h2>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435 \u0447\u0442\u043e \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430, \u044d\u0442\u043e \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435. \u041f\u043b\u044e\u0441 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u043d\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435, \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0436\u043e\u043d\u0433\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438:<\/p>\n<pre><code class=\"go\">func BenchmarkCopyUnsafeV2(b *testing.B) { for i := 0; i &lt; b.N; i++ { dstAddr := uintptr(unsafe.Pointer(&amp;dstPaletted.Pix[0])) srcAddr := uintptr(unsafe.Pointer(&amp;srcRGBA.Pix[0])) + 2 for y := 0; y &lt; imageHeight; y++ { for x := 0; x &lt; imageWidth; x++ { *(*uint8)((unsafe.Pointer)(dstAddr)) = *(*uint8)((unsafe.Pointer)(srcAddr)) dstAddr++ srcAddr += 4 } } } }<\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u044f \u0435\u0449\u0451 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u043b\u043e\u0441\u044c \u0432 1.5 \u0440\u0430\u0437\u0430:<\/p>\n<pre><code>BenchmarkCopySTD-16                    1590   3730930 ns\/op 1048600 B\/op  262144 allocs\/op BenchmarkCopySliceElements-16         22778    260128 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnsafe-16                44539    131881 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnsafeV2-16              65779     88408 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<p>\u0418\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 CPU \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0438\u043d\u0438\u043c\u0443\u043c:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c78\/c32\/1e2\/c78c321e2bf69b891b3fb297e9bd3023.png\" width=\"1012\" height=\"259\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c78\/c32\/1e2\/c78c321e2bf69b891b3fb297e9bd3023.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f \u0447\u0442\u043e \u0432\u0441\u0451, \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0431\u044b\u0442\u044c \u043d\u0435 \u043c\u043e\u0436\u0435\u0442, \u043d\u043e &#8230;<\/p>\n<h2>Loop unrolling<\/h2>\n<p>\u0412 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043c\u043e\u043c\u0435\u043d\u0442 \u044f \u043f\u043e\u0434\u0443\u043c\u0430\u043b, \u0430 \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u043b C++ \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441 <code>-O3<\/code> \u0438 \u044d\u0442\u043e \u043c\u0435\u043d\u044f \u043d\u0430\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u043e \u043d\u0430 \u043c\u044b\u0441\u043b\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 Go \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u043b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e &#171;<a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A0%D0%B0%D0%B7%D0%BC%D0%BE%D1%82%D0%BA%D0%B0_%D1%86%D0%B8%D0%BA%D0%BB%D0%B0\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u043c\u043e\u0442\u043a\u0430 \u0446\u0438\u043a\u043b\u0430<\/a>&#171;? \u041d\u0430\u0434\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0443\u043a\u0430\u043c\u0438:<\/p>\n<pre><code class=\"go\">func BenchmarkCopyUnrolledLoop4(b *testing.B) { for i := 0; i &lt; b.N; i++ { dstAddr := uintptr(unsafe.Pointer(&amp;dstPaletted.Pix[0])) srcAddr := uintptr(unsafe.Pointer(&amp;srcRGBA.Pix[0])) for y := 0; y &lt; imageHeight; y++ { for x := 0; x &lt; imageWidth; x += 4 { *(*uint8)((unsafe.Pointer)(dstAddr + 0)) = *(*uint8)((unsafe.Pointer)(srcAddr + 0)) *(*uint8)((unsafe.Pointer)(dstAddr + 1)) = *(*uint8)((unsafe.Pointer)(srcAddr + 4)) *(*uint8)((unsafe.Pointer)(dstAddr + 2)) = *(*uint8)((unsafe.Pointer)(srcAddr + 8)) *(*uint8)((unsafe.Pointer)(dstAddr + 3)) = *(*uint8)((unsafe.Pointer)(srcAddr + 12))  dstAddr += 4 srcAddr += 16 } } } }<\/code><\/pre>\n<p>\u0418 \u044d\u0442\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u043b\u043e \u0435\u0449\u0451 \u043d\u0430 \u0442\u0440\u0435\u0442\u044c. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044f \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0440\u0430\u0437\u043d\u044b\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0437\u0430 1 \u0446\u0438\u043a\u043b, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0432\u0441\u0435 512 px, \u043d\u043e \u0441\u0430\u043c\u044b\u0439 \u043b\u0443\u0447\u0448\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u043d\u0430 \u043c\u043e\u0451\u043c CPU, \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 4 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438:<\/p>\n<pre><code>BenchmarkCopySTD-16                    1590   3730930 ns\/op 1048600 B\/op  262144 allocs\/op BenchmarkCopySliceElements-16         22778    260128 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnsafe-16                44539    131881 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnsafeV2-16              65779     88408 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnrolledLoop2-16         88812     66479 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnrolledLoop4-16         95296     58995 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnrolledLoop8-16         95563     62937 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnrolledLoop512-16       95064     62808 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f \u0437\u0434\u0435\u0441\u044c \u043c\u043d\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f, \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u0435 \u0432 63 \u0440\u0430\u0437\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u0432 \u043f\u043e\u0447\u0442\u0438 4.5 \u0440\u0430\u0437\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435\u043f\u043b\u043e\u0445\u043e. \u0414\u0430\u043b\u044c\u0448\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u043c\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0430\u0437. \u041d\u043e \u0434\u043b\u044f \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 <a href=\"https:\/\/highload.fun\/tasks\/17\/\" rel=\"noopener noreferrer nofollow\">HighLoad.Fun<\/a>, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u0441\u0438\u043b\u044b \u0438 \u0435\u0449\u0451 \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u043a\u043e\u0434, \u043f\u0440\u0438\u0447\u0451\u043c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 Go, \u043d\u043e \u0438 \u043d\u0430 C++ \u0438\u043b\u0438 Rust. <\/p>\n<p>P.S. \u0420\u0430\u0434\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0430 \u044f \u0441\u043f\u0440\u043e\u0441\u0438\u043b \u0443 ChatGPT \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c BenchmarkCopySliceElements, \u043e\u043d \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b:<\/p>\n<ol>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u044b, \u0432 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u043e, \u0442.\u043a. \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u044f\u0434\u0440\u0430 CPU \u0443\u0436\u0435 \u0437\u0430\u043d\u044f\u0442\u044b \u0442\u043e\u0439 \u0436\u0435 \u0440\u0430\u0431\u043e\u0442\u043e\u0439 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c SIMD, \u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430\u043c\u0438, \u043d\u043e \u043a\u0430\u043a\u0443\u044e \u043d\u0435 \u0441\u043a\u0430\u0437\u0430\u043b.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0442\u043e\u0447\u043d\u044f\u044e\u0449\u0438\u0445 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432, \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0438\u043a\u043b, \u043d\u043e \u044f \u0438 \u0442\u0430\u043a \u044d\u0442\u043e \u0443\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043b.<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/717716\/\"> https:\/\/habr.com\/ru\/post\/717716\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442 \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412 \u0441\u0432\u043e\u0435\u0439 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 <a href=\"https:\/\/habr.com\/ru\/post\/716292\/\" rel=\"noopener noreferrer nofollow\">\u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u043f\u0440\u043e \u0440\u0430\u0437\u0431\u043e\u0440 \u043a\u043e\u0434\u0430 \u043f\u043e\u0431\u0435\u0434\u0438\u0432\u0448\u0435\u0433\u043e \u0432 VK Cup&#8217;22\/23 \u044f \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b \u043a\u0430\u043a \u043c\u043d\u0435 \u0443\u0434\u0430\u043b\u043e\u0441\u044c \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0439 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 \u0432 \u0434\u0440\u0443\u0433\u0443\u044e \u0432 30 \u0440\u0430\u0437 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0447\u0451\u0440\u043d\u043e\u0439 \u043c\u0430\u0433\u0438\u0438 <code>unsafe<\/code>. \u041e\u0434\u043d\u0430\u043a\u043e \u044f \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0432\u0430\u043b \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0435\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0435. \u042f \u0434\u0430\u0436\u0435 \u043f\u0440\u0438\u0432\u043b\u0451\u043a OpenAI \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u043d\u043e \u043e\u043d \u043c\u043d\u0435 \u043f\u043e\u043c\u043e\u0433 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u043e\u0439 \u0434\u043b\u044f \u043e\u0431\u043b\u043e\u0436\u043a\u0438 \u0441\u0442\u0430\u0442\u044c\u0438. \u0412 \u0438\u0442\u043e\u0433\u0435 \u044f \u043d\u0430\u0448\u0435\u043b \u0441\u043f\u043e\u0441\u043e\u0431 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043a\u043e\u0434 \u0435\u0449\u0435 \u0432 2 \u0440\u0430\u0437\u0430. \u0427\u0435\u043c \u0438 \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f.<\/p>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430<\/h2>\n<p>\u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u044b\u043b\u0430 \u0432 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u043b\u0430\u0436\u0430 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c 512x512px, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u0432\u0435\u043b\u0430\u0441\u044c \u043a \u0437\u0430\u0434\u0430\u0447\u0435 \u043f\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u0441\u0438\u043d\u0435\u0439 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0439 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 <code>image.RGBA<\/code> \u0432 \u0438\u043d\u0434\u0435\u043a\u0441 \u0446\u0432\u0435\u0442\u0430 \u043f\u0430\u043b\u0438\u0442\u0440\u044b \u0432 <code>image.Paletted<\/code>. \u0414\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u044f \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u043b \u0437\u0430\u0434\u0430\u0447\u0443 \u0434\u043e: \u043d\u0430\u0434\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u043d\u044e\u044e \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0443\u044e \u0438\u0437 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0438 <code>image.RGBA<\/code> \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c 512x512px \u0432 <code>image.Paletted<\/code> \u0442\u0430\u043a\u043e\u0433\u043e \u0436\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430. <\/p>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043f\u0430\u043a\u0435\u0442\u0430 image \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">func BenchmarkCopySTD(b *testing.B) { for i := 0; i &lt; b.N; i++ { for y := 0; y &lt; imageHeight; y++ { dstPixOffset := dstPaletted.PixOffset(0, y) for x := 0; x &lt; imageWidth; x++ { _, _, b, _ := srcRGBA.At(x, y).RGBA() dstPaletted.Pix[dstPixOffset+x] = uint8(b) } } } }<\/code><\/pre>\n<p>\u041e\u043d\u043e \u0434\u043e\u043b\u0433\u043e\u0435, \u043c\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0442\u0440\u0430\u0442\u0438\u0442\u0441\u044f \u043d\u0430 \u0432\u044b\u0437\u043e\u0432\u044b <code>At()<\/code>. \u0422\u0438\u043f\u044b \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a \u043d\u0430\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0441 \u043f\u0438\u043a\u0441\u0435\u043b\u0430\u043c\u0438:<\/p>\n<pre><code class=\"go\">func BenchmarkCopySliceElements(b *testing.B) { for i := 0; i &lt; b.N; i++ { for y := 0; y &lt; imageHeight; y++ { srcPixOffset := srcRGBA.PixOffset(0, y) dstPixOffset := dstPaletted.PixOffset(0, y) for x := 0; x &lt; imageWidth; x++ { dstPaletted.Pix[dstPixOffset+x] = srcRGBA.Pix[srcPixOffset+x*4+1] } } } }<\/code><\/pre>\n<p>\u0421\u0440\u0430\u0437\u0443 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0431\u0443\u0441\u0442:<\/p>\n<pre><code>BenchmarkCopySTD-16                    1590   3730930 ns\/op 1048600 B\/op  262144 allocs\/op BenchmarkCopySliceElements-16         22778    260128 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<p>\u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0447\u0451\u0440\u043d\u0430\u044f \u043c\u0430\u0433\u0438\u044f <code>unsafe<\/code>.<\/p>\n<h2>\u0418\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u0441\u044f \u043e\u0442 Bounds Check<\/h2>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u0434\u0438\u0432\u0438\u0442\u044c\u0441\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u0434\u043b\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441\u043b\u0430\u0439\u0441\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u043d\u0435\u0432\u044b\u0445\u043e\u0434 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0441\u043b\u0430\u0439\u0441\u0430. \u0415\u0451 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e \u043f\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u043d\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0435\u0451 \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0430\u0434\u0440\u0435\u0441 \u043d\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u041f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"go\">func BenchmarkCopyUnsafe(b *testing.B) { for i := 0; i &lt; b.N; i++ { dstAddr := uintptr(unsafe.Pointer(&amp;dstPaletted.Pix[0])) srcAddr := uintptr(unsafe.Pointer(&amp;srcRGBA.Pix[0])) for y := 0; y &lt; imageHeight; y++ { srcPixOffset := srcRGBA.PixOffset(0, y) dstPixOffset := dstPaletted.PixOffset(0, y) for x := 0; x &lt; imageWidth; x++ { \/\/dst.Pix[dstPixOffset+x] = src.Pix[srcPixOffset+x*4+1] *(*uint8)((unsafe.Pointer)(dstAddr + uintptr(dstPixOffset+x))) = *(*uint8)((unsafe.Pointer)(srcAddr + uintptr(srcPixOffset+x*4+2))) } } } }<\/code><\/pre>\n<p>\u0412 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 (3) \u0438 (4) \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0430\u0434\u0440\u0435\u0441\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0443\u043b\u0435\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0430 \u0434\u0430\u043b\u044c\u0448\u0435 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u043a \u043d\u0438\u043c \u043f\u043e \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0430\u0434\u0440\u0435\u0441\u0430. \u0412\u0440\u0435\u043c\u044f \u043d\u0430 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u043b\u043e\u0441\u044c \u0432 2 \u0440\u0430\u0437\u0430:<\/p>\n<pre><code>BenchmarkCopySTD-16                    1590   3730930 ns\/op 1048600 B\/op  262144 allocs\/op BenchmarkCopySliceElements-16         22778    260128 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnsafe-16                44539    131881 ns\/op       0 B\/op       0 allocs\/op <\/code><\/pre>\n<p>\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 CPU \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u043b\u043e\u0441\u044c:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0430\u043a\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u044b\u043b\u043e \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043e \u043d\u0430 \u043a\u043e\u043d\u043a\u0443\u0440\u0441, \u043d\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 \u043c\u0435\u043d\u044f \u0432\u0441\u0451 \u0435\u0449\u0451 \u0441\u043c\u0443\u0449\u0430\u043b\u043e \u0438 \u0443\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u043d\u043a\u0443\u0440\u0441\u0430 \u044f \u043d\u0430\u0447\u0430\u043b \u0438\u0441\u043a\u0430\u0442\u044c \u043f\u0443\u0442\u0438 \u043a\u0430\u043a \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c.<\/p>\n<h2>\u0418\u0437\u0431\u0430\u0432\u043b\u044f\u0435\u043c\u0441\u044f \u043e\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0438<\/h2>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435 \u0447\u0442\u043e \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430, \u044d\u0442\u043e \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435. \u041f\u043b\u044e\u0441 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u043d\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435, \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0436\u043e\u043d\u0433\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438:<\/p>\n<pre><code class=\"go\">func BenchmarkCopyUnsafeV2(b *testing.B) { for i := 0; i &lt; b.N; i++ { dstAddr := uintptr(unsafe.Pointer(&amp;dstPaletted.Pix[0])) srcAddr := uintptr(unsafe.Pointer(&amp;srcRGBA.Pix[0])) + 2 for y := 0; y &lt; imageHeight; y++ { for x := 0; x &lt; imageWidth; x++ { *(*uint8)((unsafe.Pointer)(dstAddr)) = *(*uint8)((unsafe.Pointer)(srcAddr)) dstAddr++ srcAddr += 4 } } } }<\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u044f \u0435\u0449\u0451 \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u043b\u043e\u0441\u044c \u0432 1.5 \u0440\u0430\u0437\u0430:<\/p>\n<pre><code>BenchmarkCopySTD-16                    1590   3730930 ns\/op 1048600 B\/op  262144 allocs\/op BenchmarkCopySliceElements-16         22778    260128 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnsafe-16                44539    131881 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnsafeV2-16              65779     88408 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<p>\u0418\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 CPU \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0438\u043d\u0438\u043c\u0443\u043c:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f \u0447\u0442\u043e \u0432\u0441\u0451, \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0431\u044b\u0442\u044c \u043d\u0435 \u043c\u043e\u0436\u0435\u0442, \u043d\u043e &#8230;<\/p>\n<h2>Loop unrolling<\/h2>\n<p>\u0412 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u043c\u043e\u043c\u0435\u043d\u0442 \u044f \u043f\u043e\u0434\u0443\u043c\u0430\u043b, \u0430 \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u043b C++ \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0441 <code>-O3<\/code> \u0438 \u044d\u0442\u043e \u043c\u0435\u043d\u044f \u043d\u0430\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u043e \u043d\u0430 \u043c\u044b\u0441\u043b\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 Go \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u043b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e &#171;<a href=\"https:\/\/ru.wikipedia.org\/wiki\/%D0%A0%D0%B0%D0%B7%D0%BC%D0%BE%D1%82%D0%BA%D0%B0_%D1%86%D0%B8%D0%BA%D0%BB%D0%B0\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0437\u043c\u043e\u0442\u043a\u0430 \u0446\u0438\u043a\u043b\u0430<\/a>&#171;? \u041d\u0430\u0434\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0443\u043a\u0430\u043c\u0438:<\/p>\n<pre><code class=\"go\">func BenchmarkCopyUnrolledLoop4(b *testing.B) { for i := 0; i &lt; b.N; i++ { dstAddr := uintptr(unsafe.Pointer(&amp;dstPaletted.Pix[0])) srcAddr := uintptr(unsafe.Pointer(&amp;srcRGBA.Pix[0])) for y := 0; y &lt; imageHeight; y++ { for x := 0; x &lt; imageWidth; x += 4 { *(*uint8)((unsafe.Pointer)(dstAddr + 0)) = *(*uint8)((unsafe.Pointer)(srcAddr + 0)) *(*uint8)((unsafe.Pointer)(dstAddr + 1)) = *(*uint8)((unsafe.Pointer)(srcAddr + 4)) *(*uint8)((unsafe.Pointer)(dstAddr + 2)) = *(*uint8)((unsafe.Pointer)(srcAddr + 8)) *(*uint8)((unsafe.Pointer)(dstAddr + 3)) = *(*uint8)((unsafe.Pointer)(srcAddr + 12))  dstAddr += 4 srcAddr += 16 } } } }<\/code><\/pre>\n<p>\u0418 \u044d\u0442\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u043b\u043e \u0435\u0449\u0451 \u043d\u0430 \u0442\u0440\u0435\u0442\u044c. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044f \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b \u0440\u0430\u0437\u043d\u044b\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0437\u0430 1 \u0446\u0438\u043a\u043b, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0432\u0441\u0435 512 px, \u043d\u043e \u0441\u0430\u043c\u044b\u0439 \u043b\u0443\u0447\u0448\u0438\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u043d\u0430 \u043c\u043e\u0451\u043c CPU, \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 4 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438:<\/p>\n<pre><code>BenchmarkCopySTD-16                    1590   3730930 ns\/op 1048600 B\/op  262144 allocs\/op BenchmarkCopySliceElements-16         22778    260128 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnsafe-16                44539    131881 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnsafeV2-16              65779     88408 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnrolledLoop2-16         88812     66479 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnrolledLoop4-16         95296     58995 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnrolledLoop8-16         95563     62937 ns\/op       0 B\/op       0 allocs\/op BenchmarkCopyUnrolledLoop512-16       95064     62808 ns\/op       0 B\/op       0 allocs\/op<\/code><\/pre>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f \u0437\u0434\u0435\u0441\u044c \u043c\u043d\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f, \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u0435 \u0432 63 \u0440\u0430\u0437\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u0432 \u043f\u043e\u0447\u0442\u0438 4.5 \u0440\u0430\u0437\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435\u043f\u043b\u043e\u0445\u043e. \u0414\u0430\u043b\u044c\u0448\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0438\u0433\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u0432\u0435\u043a\u0442\u043e\u0440\u043d\u044b\u043c\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0430\u0437. \u041d\u043e \u0434\u043b\u044f \u0436\u0435\u043b\u0430\u044e\u0449\u0438\u0445 \u044f \u0441\u0434\u0435\u043b\u0430\u043b \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 <a href=\"https:\/\/highload.fun\/tasks\/17\/\" rel=\"noopener noreferrer nofollow\">HighLoad.Fun<\/a>, \u0433\u0434\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u0441\u0438\u043b\u044b \u0438 \u0435\u0449\u0451 \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u043a\u043e\u0434, \u043f\u0440\u0438\u0447\u0451\u043c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 Go, \u043d\u043e \u0438 \u043d\u0430 C++ \u0438\u043b\u0438 Rust. <\/p>\n<p>P.S. \u0420\u0430\u0434\u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0430 \u044f \u0441\u043f\u0440\u043e\u0441\u0438\u043b \u0443 ChatGPT \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c BenchmarkCopySliceElements, \u043e\u043d \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b:<\/p>\n<ol>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u0440\u0443\u0442\u0438\u043d\u044b, \u0432 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u043e, \u0442.\u043a. \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u044f\u0434\u0440\u0430 CPU \u0443\u0436\u0435 \u0437\u0430\u043d\u044f\u0442\u044b \u0442\u043e\u0439 \u0436\u0435 \u0440\u0430\u0431\u043e\u0442\u043e\u0439 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a.<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c SIMD, \u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0435\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b.<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430\u043c\u0438, \u043d\u043e \u043a\u0430\u043a\u0443\u044e \u043d\u0435 \u0441\u043a\u0430\u0437\u0430\u043b.<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0442\u043e\u0447\u043d\u044f\u044e\u0449\u0438\u0445 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432, \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0438\u043a\u043b, \u043d\u043e \u044f \u0438 \u0442\u0430\u043a \u044d\u0442\u043e \u0443\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043b.<\/p>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/717716\/\"> https:\/\/habr.com\/ru\/post\/717716\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-345617","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=345617"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345617\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=345617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=345617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=345617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}