This commit is contained in:
Teiva Harsanyi 2024-03-05 23:06:05 +01:00
parent 66b1d95047
commit 7274bf3b69
13 changed files with 27 additions and 2 deletions

View file

@ -7,6 +7,8 @@ hide:
# Interface pollution
![](img/interface-pollution.jpeg)
Interfaces are one of the cornerstones of the Go language when designing and structuring our code. However, like many tools or concepts, abusing them is generally not a good idea. Interface pollution is about overwhelming our code with unnecessary abstractions, making it harder to understand. Its a common mistake made by developers coming from another language with different habits. Before delving into the topic, lets refresh our minds about Gos interfaces. Then, we will see when its appropriate to use interfaces and when it may be considered pollution.
## Concepts

View file

@ -7,6 +7,8 @@ hide:
# Writing concurrent code that leads to false sharing
![](img/false-sharing.jpeg)
In previous sections, we have discussed the fundamental concepts of CPU caching. We have seen that some specific caches (typically, L1 and L2) arent shared among all the logical cores but are specific to a physical core. This specificity has some concrete impacts such as concurrency and the concept of false sharing, which can lead to a significant performance decrease. Lets look at what false sharing is via an example and then see how to prevent it.
In this example, we use two structs, `Input` and `Result`:

BIN
docs/img/false-sharing.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 KiB

View file

@ -1,6 +1,8 @@
---
title: Japanese Version
comments: true
hide:
- navigation
---
# Go言語でありがちな間違い

View file

@ -1,10 +1,14 @@
---
title: Chinese (Simplified) Version
comments: true
hide:
- navigation
---
# 100个Go常见错误及如何避免
![](img/inside-cover.png)
## 代码及工程组织
### 意外的变量隐藏 (#1)

View file

@ -1037,6 +1037,7 @@
<h1 id="interface-pollution">Interface pollution</h1>
<p><a class="glightbox" href="../img/interface-pollution.jpeg" data-type="image" data-width="auto" data-height="auto" data-desc-position="bottom"><img alt="" src="../img/interface-pollution.jpeg" /></a></p>
<p>Interfaces are one of the cornerstones of the Go language when designing and structuring our code. However, like many tools or concepts, abusing them is generally not a good idea. Interface pollution is about overwhelming our code with unnecessary abstractions, making it harder to understand. Its a common mistake made by developers coming from another language with different habits. Before delving into the topic, lets refresh our minds about Gos interfaces. Then, we will see when its appropriate to use interfaces and when it may be considered pollution.</p>
<h2 id="concepts">Concepts</h2>
<p>An interface provides a way to specify the behavior of an object. We use interfaces to create common abstractions that multiple objects can implement. What makes Go interfaces so different is that they are satisfied implicitly. There is no explicit keyword like <code>implements</code> to mark that an object X implements interface Y.</p>

View file

@ -879,6 +879,7 @@
<h1 id="writing-concurrent-code-that-leads-to-false-sharing">Writing concurrent code that leads to false sharing</h1>
<p><a class="glightbox" href="../img/false-sharing.jpeg" data-type="image" data-width="auto" data-height="auto" data-desc-position="bottom"><img alt="" src="../img/false-sharing.jpeg" /></a></p>
<p>In previous sections, we have discussed the fundamental concepts of CPU caching. We have seen that some specific caches (typically, L1 and L2) arent shared among all the logical cores but are specific to a physical core. This specificity has some concrete impacts such as concurrency and the concept of false sharing, which can lead to a significant performance decrease. Lets look at what false sharing is via an example and then see how to prevent it.</p>
<p>In this example, we use two structs, <code>Input</code> and <code>Result</code>:</p>
<div class="language-go highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">type</span><span class="w"> </span><span class="nx">Input</span><span class="w"> </span><span class="kd">struct</span><span class="w"> </span><span class="p">{</span>

BIN
site/img/false-sharing.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 KiB

View file

@ -363,7 +363,9 @@
<div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
<div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" hidden>
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
@ -824,6 +826,8 @@
<div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
@ -3564,6 +3568,8 @@
<h2 id="__comments">Comments</h2>

Binary file not shown.

View file

@ -363,7 +363,9 @@
<div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
<div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" hidden>
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
@ -824,6 +826,8 @@
<div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
@ -1935,6 +1939,7 @@
<h1 id="100go">100个Go常见错误及如何避免</h1>
<p><a class="glightbox" href="../img/inside-cover.png" data-type="image" data-width="auto" data-height="auto" data-desc-position="bottom"><img alt="" src="../img/inside-cover.png" /></a></p>
<h2 id="_1">代码及工程组织</h2>
<h3 id="1">意外的变量隐藏 (#1)</h3>
<p>避免变量隐藏(外部作用域变量被内部作用域同名变量隐藏),有助于避免变量引用错误,有助于他人阅读理解。</p>
@ -2284,6 +2289,8 @@
<h2 id="__comments">Comments</h2>