Skip to content

Commit 7c72a84

Browse files
committed
go/analysis/passes/fieldalignment: clarify reported diagnostics in docs
It wasn't obvious that there are two different kind of diagnostics reported by fieldalignment, one for struct size and another for pointer bytes. The documentation now mentions both types, and shows an example that clarify what "pointer bytes" are. Fixes golang/go#45541 Change-Id: Ia62fb05980ddddf52e579ac51459aaaed168cfa7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/314469 Trust: Pontus Leitzler <leitzler@gmail.com> Run-TryBot: Pontus Leitzler <leitzler@gmail.com> gopls-CI: kokoro <noreply+kokoro@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
1 parent 9ff8648 commit 7c72a84

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

go/analysis/passes/fieldalignment/fieldalignment.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// Package fieldalignment defines an Analyzer that detects structs that would take less
5+
// Package fieldalignment defines an Analyzer that detects structs that would use less
66
// memory if their fields were sorted.
77
package fieldalignment
88

@@ -20,10 +20,27 @@ import (
2020
"golang.org/x/tools/go/ast/inspector"
2121
)
2222

23-
const Doc = `find structs that would take less memory if their fields were sorted
23+
const Doc = `find structs that would use less memory if their fields were sorted
2424
25-
This analyzer find structs that can be rearranged to take less memory, and provides
25+
This analyzer find structs that can be rearranged to use less memory, and provides
2626
a suggested edit with the optimal order.
27+
28+
Note that there are two different diagnostics reported. One checks struct size,
29+
and the other reports "pointer bytes" used. Pointer bytes is how many bytes of the
30+
object that the garbage collector has to potentially scan for pointers, for example:
31+
32+
struct { uint32; string }
33+
34+
have 16 pointer bytes because the garbage collector has to scan up through the string's
35+
inner pointer.
36+
37+
struct { string; *uint32 }
38+
39+
has 24 pointer bytes because it has to scan further through the *uint32.
40+
41+
struct { string; uint32 }
42+
43+
has 8 because it can stop immediately after the string pointer.
2744
`
2845

2946
var Analyzer = &analysis.Analyzer{

gopls/doc/analyzers.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,28 @@ of the second argument is not a pointer to a type implementing error.
119119

120120
## **fieldalignment**
121121

122-
find structs that would take less memory if their fields were sorted
122+
find structs that would use less memory if their fields were sorted
123123

124-
This analyzer find structs that can be rearranged to take less memory, and provides
124+
This analyzer find structs that can be rearranged to use less memory, and provides
125125
a suggested edit with the optimal order.
126126

127+
Note that there are two different diagnostics reported. One checks struct size,
128+
and the other reports "pointer bytes" used. Pointer bytes is how many bytes of the
129+
object that the garbage collector has to potentially scan for pointers, for example:
130+
131+
struct { uint32; string }
132+
133+
have 16 pointer bytes because the garbage collector has to scan up through the string's
134+
inner pointer.
135+
136+
struct { string; *uint32 }
137+
138+
has 24 pointer bytes because it has to scan further through the *uint32.
139+
140+
struct { string; uint32 }
141+
142+
has 8 because it can stop immediately after the string pointer.
143+
127144

128145
**Disabled by default. Enable it by setting `"analyses": {"fieldalignment": true}`.**
129146

internal/lsp/source/api_json.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)