Documentation
¶
Overview ¶
Package goreset generates Reset methods for structs that are reused through a pool.
A generated Reset puts the value back to its zero state without giving up the memory the value owns: slices keep their backing arrays (cleared, length zero), nested values with their own Reset are reset through it, and struct tags choose a different treatment per field:
Buf []byte `reset:"-"` // left as it is Timer *time.Timer `reset:"call=Stop"` // kept, method called Flags uint32 `reset:"value=^uint32(0)"` // set to EXPR, not zero Conn *Conn `reset:"release=PutConn"` // PutConn(r.Conn); r.Conn = nil
Fields of sync types are never touched, and a struct holding one is reset field by field. Any other struct is reset either in one assignment (`*r = T{}`) with the kept fields restored after it, or field by field, whichever a cost model built on the field sizes rates cheaper; the generated comment says which and why.
The usual way to run it is a go:generate directive next to the type:
//go:generate go tool goreset -type=Request
which writes request_reset.gen.go in the same package. Load and Package.Generate expose the generator to other tools.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoPackage = errors.New("goreset: no package found")
ErrNoPackage is returned by Load when dir holds no Go package.
Functions ¶
Types ¶
type Package ¶
type Package struct {
// contains filtered or unexported fields
}
Package is a loaded Go package whose struct types Generate can describe.
func Load ¶
Load type-checks the package in dir (the current directory when empty).
The dependencies come as export data from `go list -export`; the package itself is checked from source and its own type errors are tolerated, so a stale generated file (one that names a field that no longer exists) does not stop the type from being generated again. A dependency that does not compile is an error.
func (*Package) Analyze ¶ added in v1.2.0
Analyze is Generate with the findings behind the output: the mode, the costs the model gave the two modes, and for field-wise mode, why.
type Result ¶ added in v1.2.0
type Result struct {
// Source is the generated file, as Generate returns it.
Source []byte
// Hints say why Reset is field by field and how it could be one
// assignment instead. Empty when Bulk.
Hints []string
// BulkCost and FieldwiseCost are the model's estimates for the two modes,
// in units of one field store; the cheaper one is generated.
BulkCost int
FieldwiseCost int
// Bulk reports whether Reset is one assignment with the kept fields
// restored after it (true), or one statement per field (false).
Bulk bool
}
Result is what Analyze found out about a type.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
goreset
command
Command goreset writes a Reset method for the struct types named by -type into <type>_reset.gen.go in the current package.
|
Command goreset writes a Reset method for the struct types named by -type into <type>_reset.gen.go in the current package. |
|
internal
|
|
|
cases/bulk
Package bulk is a goreset test case: a struct without locks and with enough plain fields for the cost model to pick one assignment with the kept fields restored after it; every tag is exercised in that mode.
|
Package bulk is a goreset test case: a struct without locks and with enough plain fields for the cost model to pick one assignment with the kept fields restored after it; every tag is exercised in that mode. |
|
cases/consts
Package consts holds a value a reset tag refers to through import=.
|
Package consts holds a value a reset tag refers to through import=. |
|
cases/fieldwise
Package fieldwise is a goreset test case: a struct that cannot be reset in one assignment (it holds a lock and a big value with its own Reset), so every field is reset on its own.
|
Package fieldwise is a goreset test case: a struct that cannot be reset in one assignment (it holds a lock and a big value with its own Reset), so every field is reset on its own. |
|
cases/wide
Package wide is a goreset test case: no locks, but the cost model picks field-wise mode — slices cost a keep and a restore each around one assignment, and there is little else for the assignment to zero.
|
Package wide is a goreset test case: no locks, but the cost model picks field-wise mode — slices cost a keep and a restore each around one assignment, and there is little else for the assignment to zero. |