Skip to content

Commit be42814

Browse files
committed
change Block to a value struct
The goal is to stop doing two allocations for each block (now it will only allocate the []byte buffer, and pass the cid.Cid, []byte pair by decomposed registers or stack). This way of changing does not change the syntax for trivial uses of block.Block, so in theory we will have to update only producers of block.Block, not consumers. Fixes #45 Replaces ipfs/boxo#192 Note: This change will cause a failure here: https://github.com/ipfs/go-ipld-format/blob/0f7aff00f72e9dea0d9718bc0972e309ba7c3e8d/format.go#L27
1 parent b7a5c11 commit be42814

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

blocks.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,66 +16,57 @@ import (
1616
// according to the contents. It is currently used only when debugging.
1717
var ErrWrongHash = errors.New("data did not match given hash")
1818

19-
// Block provides abstraction for blocks implementations.
20-
type Block interface {
21-
RawData() []byte
22-
Cid() cid.Cid
23-
String() string
24-
Loggable() map[string]interface{}
25-
}
26-
27-
// A BasicBlock is a singular block of data in ipfs. It implements the Block
28-
// interface.
29-
type BasicBlock struct {
19+
// A Block is a singular block of data in ipfs. This is some bytes addressed by a hash.
20+
type Block struct {
3021
cid cid.Cid
3122
data []byte
3223
}
3324

3425
// NewBlock creates a Block object from opaque data. It will hash the data.
35-
func NewBlock(data []byte) *BasicBlock {
26+
func NewBlock(data []byte) Block {
3627
// TODO: fix assumptions
37-
return &BasicBlock{data: data, cid: cid.NewCidV0(u.Hash(data))}
28+
return Block{data: data, cid: cid.NewCidV0(u.Hash(data))}
3829
}
3930

4031
// NewBlockWithCid creates a new block when the hash of the data
4132
// is already known, this is used to save time in situations where
4233
// we are able to be confident that the data is correct.
43-
func NewBlockWithCid(data []byte, c cid.Cid) (*BasicBlock, error) {
34+
func NewBlockWithCid(data []byte, c cid.Cid) (Block, error) {
4435
if u.Debug {
4536
chkc, err := c.Prefix().Sum(data)
4637
if err != nil {
47-
return nil, err
38+
return Block{}, err
4839
}
4940

5041
if !chkc.Equals(c) {
51-
return nil, ErrWrongHash
42+
return Block{}, ErrWrongHash
5243
}
5344
}
54-
return &BasicBlock{data: data, cid: c}, nil
45+
return Block{data: data, cid: c}, nil
5546
}
5647

5748
// Multihash returns the hash contained in the block CID.
58-
func (b *BasicBlock) Multihash() mh.Multihash {
49+
func (b Block) Multihash() mh.Multihash {
5950
return b.cid.Hash()
6051
}
6152

6253
// RawData returns the block raw contents as a byte slice.
63-
func (b *BasicBlock) RawData() []byte {
54+
func (b Block) RawData() []byte {
6455
return b.data
6556
}
6657

6758
// Cid returns the content identifier of the block.
68-
func (b *BasicBlock) Cid() cid.Cid {
59+
func (b Block) Cid() cid.Cid {
6960
return b.cid
7061
}
7162

7263
// String provides a human-readable representation of the block CID.
73-
func (b *BasicBlock) String() string {
64+
func (b Block) String() string {
7465
return fmt.Sprintf("[Block %s]", b.Cid())
7566
}
7667

7768
// Loggable returns a go-log loggable item.
78-
func (b *BasicBlock) Loggable() map[string]interface{} {
69+
func (b Block) Loggable() map[string]interface{} {
7970
return map[string]interface{}{
8071
"block": b.Cid().String(),
8172
}

0 commit comments

Comments
 (0)