dcrd/chaincfg/subsidy.go
Josh Rickmar f860883562 chaincfg: Avoid block 1 subsidy codegen explosion.
chaincfg/v2 modified the Params type to contain the required payouts
created by the miner of the block at height 1.  However, it was
discovered that, due to code generation issues of the Go compiler,
this caused the package's weight (measured by
github.com/jondot/goweight) to explode to 7.7 MB.  This appears to be
due to an enormous amount of function calls, all of which could panic,
when building the large slice literal.

This improves the compiled size of the package by using code
generation techniques to optimize the memory layout and code
transformation of the subsidy values each time a params function is
called.  A single string contains all scripts for subsidy payouts
concatenated together, in hex encoding, and a slice contains the index
in the script data and the required payout amount.  This is iterated
in a loop to create a []TokenPayout each time it is needed.

The new size of the compiled package using Go 1.12.7 on OpenBSD/amd64
is 379 kB, a reduction of 7.32 MB, and about 5% of the original size.
2019-07-28 14:19:13 -05:00

28 lines
694 B
Go

// Copyright (c) 2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package chaincfg
//go:generate go run -tags subsidydefs generatesubsidytables.go
type blockOnePayout struct {
offset int
amount int64
}
func tokenPayouts(scriptsHex string, payouts []blockOnePayout) []TokenPayout {
tokenPayouts := make([]TokenPayout, len(payouts))
var offset int
scripts := hexDecode(scriptsHex)
for i := range payouts {
tokenPayouts[i] = TokenPayout{
ScriptVersion: 0,
Script: scripts[offset:payouts[i].offset],
Amount: payouts[i].amount,
}
offset = payouts[i].offset
}
return tokenPayouts
}