This modifies the block node structure to include only the specifically used fields, some of which in a more compact format, as opposed to copying the entire header and updates all code and tests accordingly. Not only is this a more efficient approach that helps pave the way for future optimizations, it is also consistent with the upstream code which helps minimize the differences to facilitate easier syncs due to less merge conflicts. In particular, since the merkle and stake roots, number of revocations, size, nonce, and extradata fields aren't used currently, they are no longer copied into the block node. Also, the block node already had a height field, which is also in the header, so this change also removes that duplication. Another change is that the block node now stores the timestamp as an int64 unix-style timestamp which is only 8 bytes versus the old timestamp that was in the header which is a time.Time and thus 24 bytes. It should be noted that future optimizations will very likely end up adding most of the omitted header fields back to the block node as individual fields so the headers can be efficiently reconstructed from memory, however, these changes are still beneficial due to the ability to decouple the block node storage format from the header struct which allows more compact representations and reording of the fields for optimal struct packing. Ultimately, the need for the parent hash can also be removed, which will save an additional 32 bytes which would not be possible without this decoupling.
29 lines
874 B
Go
29 lines
874 B
Go
// Copyright (c) 2013-2014 The btcsuite developers
|
|
// Copyright (c) 2015-2018 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package blockchain
|
|
|
|
// timeSorter implements sort.Interface to allow a slice of timestamps to
|
|
// be sorted.
|
|
type timeSorter []int64
|
|
|
|
// Len returns the number of timestamps in the slice. It is part of the
|
|
// sort.Interface implementation.
|
|
func (s timeSorter) Len() int {
|
|
return len(s)
|
|
}
|
|
|
|
// Swap swaps the timestamps at the passed indices. It is part of the
|
|
// sort.Interface implementation.
|
|
func (s timeSorter) Swap(i, j int) {
|
|
s[i], s[j] = s[j], s[i]
|
|
}
|
|
|
|
// Less returns whether the timstamp with index i should sort before the
|
|
// timestamp with index j. It is part of the sort.Interface implementation.
|
|
func (s timeSorter) Less(i, j int) bool {
|
|
return s[i] < s[j]
|
|
}
|