Skip to content

Commit

Permalink
ssz: remove a bool alloc, update benchmarkers
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Jul 9, 2024
1 parent 81e2953 commit 6887dc3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 63 deletions.
15 changes: 11 additions & 4 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
"github.com/holiman/uint256"
)

// Some helpers to avoid occasional allocations
var (
boolFalse = []byte{0x00}
boolTrue = []byte{0x01}
uint256Zero = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
)

// Encoder is a wrapper around an io.Writer or a []byte buffer to implement SSZ
// encoding in a streaming or buffered way. It has the following behaviors:
//
Expand Down Expand Up @@ -74,9 +81,9 @@ func EncodeBool[T ~bool](enc *Encoder, v T) {
return
}
if !v {
_, enc.err = enc.outWriter.Write([]byte{0x00})
_, enc.err = enc.outWriter.Write(boolFalse)
} else {
_, enc.err = enc.outWriter.Write([]byte{0x01})
_, enc.err = enc.outWriter.Write(boolTrue)
}
} else {
if !v {
Expand Down Expand Up @@ -114,13 +121,13 @@ func EncodeUint256(enc *Encoder, n *uint256.Int) {
n.MarshalSSZInto(enc.buf[:32])
_, enc.err = enc.outWriter.Write(enc.buf[:32])
} else {
_, enc.err = enc.outWriter.Write([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
_, enc.err = enc.outWriter.Write(uint256Zero)
}
} else {
if n != nil {
n.MarshalSSZInto(enc.outBuffer)
} else {
copy(enc.outBuffer, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
copy(enc.outBuffer, uint256Zero)
}
enc.outBuffer = enc.outBuffer[32:]
}
Expand Down
18 changes: 13 additions & 5 deletions tests/consensus_specs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ func TestConsensusSpecs(t *testing.T) {
testConsensusSpecType[*types.SignedBeaconBlockHeader](t, "SignedBeaconBlockHeader")
testConsensusSpecType[*types.SignedBLSToExecutionChange](t, "SignedBLSToExecutionChange")
testConsensusSpecType[*types.SignedVoluntaryExit](t, "SignedVoluntaryExit")
testConsensusSpecType[*types.SigningRoot](t, "SigningRoot")
testConsensusSpecType[*types.SyncAggregate](t, "SyncAggregate")
testConsensusSpecType[*types.SyncCommittee](t, "SyncCommittee")
testConsensusSpecType[*types.Transfer](t, "Transfer")
testConsensusSpecType[*types.Validator](t, "Validator")
testConsensusSpecType[*types.VoluntaryExit](t, "VoluntaryExit")
testConsensusSpecType[*types.Withdrawal](t, "Withdrawal")
Expand Down Expand Up @@ -221,19 +219,29 @@ func BenchmarkConsensusSpecs(b *testing.B) {
benchmarkConsensusSpecType[*types.AttestationData](b, "deneb", "AttestationData")
benchmarkConsensusSpecType[*types.AttesterSlashing](b, "deneb", "AttesterSlashing")
benchmarkConsensusSpecType[*types.BeaconBlock](b, "phase0", "BeaconBlock")
benchmarkConsensusSpecType[*types.BeaconBlockBody](b, "phase0", "BeaconBlockBody")
benchmarkConsensusSpecType[*types.BeaconBlockBodyDeneb](b, "deneb", "BeaconBlockBody")
benchmarkConsensusSpecType[*types.BeaconBlockHeader](b, "deneb", "BeaconBlockHeader")
benchmarkConsensusSpecType[*types.BeaconState](b, "phase0", "BeaconState")
benchmarkConsensusSpecType[*types.BLSToExecutionChange](b, "deneb", "BLSToExecutionChange")
benchmarkConsensusSpecType[*types.Checkpoint](b, "deneb", "Checkpoint")
benchmarkConsensusSpecType[*types.Deposit](b, "deneb", "Deposit")
benchmarkConsensusSpecType[*types.DepositData](b, "deneb", "DepositData")
benchmarkConsensusSpecType[*types.DepositMessage](b, "deneb", "DepositMessage")
benchmarkConsensusSpecType[*types.Eth1Block](b, "deneb", "Eth1Block")
benchmarkConsensusSpecType[*types.Eth1Data](b, "deneb", "Eth1Data")
benchmarkConsensusSpecType[*types.ExecutionPayloadCapella](b, "capella", "ExecutionPayload")
benchmarkConsensusSpecType[*types.ExecutionPayloadDeneb](b, "deneb", "ExecutionPayload")
benchmarkConsensusSpecType[*types.ExecutionPayloadHeaderDeneb](b, "deneb", "ExecutionPayloadHeader")
benchmarkConsensusSpecType[*types.Fork](b, "deneb", "Fork")
benchmarkConsensusSpecType[*types.HistoricalBatch](b, "deneb", "HistoricalBatch")
benchmarkConsensusSpecType[*types.HistoricalBatchVariation](b, "deneb", "HistoricalBatch")
benchmarkConsensusSpecType[*types.HistoricalSummary](b, "deneb", "HistoricalSummary")
benchmarkConsensusSpecType[*types.IndexedAttestation](b, "deneb", "IndexedAttestation")
benchmarkConsensusSpecType[*types.PendingAttestation](b, "deneb", "PendingAttestation")
benchmarkConsensusSpecType[*types.ProposerSlashing](b, "deneb", "ProposerSlashing")
benchmarkConsensusSpecType[*types.SignedBeaconBlockHeader](b, "deneb", "SignedBeaconBlockHeader")
benchmarkConsensusSpecType[*types.SignedBLSToExecutionChange](b, "deneb", "SignedBLSToExecutionChange")
benchmarkConsensusSpecType[*types.SignedVoluntaryExit](b, "deneb", "SignedVoluntaryExit")
benchmarkConsensusSpecType[*types.SyncAggregate](b, "deneb", "SyncAggregate")
benchmarkConsensusSpecType[*types.SyncCommittee](b, "deneb", "SyncCommittee")
benchmarkConsensusSpecType[*types.Validator](b, "deneb", "Validator")
benchmarkConsensusSpecType[*types.VoluntaryExit](b, "deneb", "VoluntaryExit")
benchmarkConsensusSpecType[*types.Withdrawal](b, "deneb", "Withdrawal")
Expand Down
16 changes: 0 additions & 16 deletions tests/testtypes/consensus-spec-tests/gen_signing_root_ssz.go

This file was deleted.

21 changes: 0 additions & 21 deletions tests/testtypes/consensus-spec-tests/gen_transfer_ssz.go

This file was deleted.

17 changes: 0 additions & 17 deletions tests/testtypes/consensus-spec-tests/types_consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ import "github.com/holiman/uint256"
//go:generate go run ../../../cmd/sszgen -type SignedBeaconBlockHeader -out gen_signed_beacon_block_header_ssz.go
//go:generate go run ../../../cmd/sszgen -type SignedBLSToExecutionChange -out gen_signed_bls_to_execution_change_ssz.go
//go:generate go run ../../../cmd/sszgen -type SignedVoluntaryExit -out gen_signed_voluntary_exit_ssz.go
//go:generate go run ../../../cmd/sszgen -type SigningRoot -out gen_signing_root_ssz.go
//go:generate go run ../../../cmd/sszgen -type SyncAggregate -out gen_sync_aggregate_ssz.go
//go:generate go run ../../../cmd/sszgen -type SyncCommittee -out gen_sync_committee_ssz.go
//go:generate go run ../../../cmd/sszgen -type Transfer -out gen_transfer_ssz.go
//go:generate go run ../../../cmd/sszgen -type VoluntaryExit -out gen_voluntary_exit_ssz.go
//go:generate go run ../../../cmd/sszgen -type Validator -out gen_validator_ssz.go
//go:generate go run ../../../cmd/sszgen -type Withdrawal -out gen_withdrawal_ssz.go
Expand Down Expand Up @@ -389,11 +387,6 @@ type SignedVoluntaryExit struct {
Signature [96]byte
}

type SigningRoot struct {
ObjectRoot [32]byte
Domain [8]byte
}

type SyncAggregate struct {
SyncCommiteeBits [64]byte
SyncCommiteeSignature [96]byte
Expand All @@ -404,16 +397,6 @@ type SyncCommittee struct {
AggregatePubKey [48]byte
}

type Transfer struct {
Sender uint64
Recipient uint64
Amount uint64
Fee uint64
Slot uint64
Pubkey [48]byte
Signature [96]byte
}

type VoluntaryExit struct {
Epoch uint64
ValidatorIndex uint64
Expand Down

0 comments on commit 6887dc3

Please sign in to comment.