Skip to content

Commit e34dce0

Browse files
authored
Fix alignment in chunkedContentCoder (#147)
An unaligned atomic bug was unfortunately introduced in #119 because the `bytesWritten` field was placed at the end of the `chunkedContentCoder` struct. This places this after the bytes.Buffers and the bool causing it to be misaligned. The ideal placement of this variable is not entirely clear but placing it before the progresiveWrite bool should help. An alternative would be to just place this atomic field at the top of the struct then there would be no risk of it becoming misaligned in future. I moved a few things around to reduce the size of the struct too but it could be possible to adjust things a little more to make the struct a little smaller. Signed-off-by: Andrew Thornton <[email protected]>
1 parent 96a016b commit e34dce0

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

contentcoder.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,26 @@ func init() {
3131
reflectStaticSizeMetaData = int(reflect.TypeOf(md).Size())
3232
}
3333

34-
var termSeparator byte = 0xff
35-
var termSeparatorSplitSlice = []byte{termSeparator}
34+
var (
35+
termSeparator byte = 0xff
36+
termSeparatorSplitSlice = []byte{termSeparator}
37+
)
3638

3739
type chunkedContentCoder struct {
3840
final []byte
3941
chunkSize uint64
4042
currChunk uint64
4143
chunkLens []uint64
4244

45+
compressed []byte // temp buf for snappy compression
46+
4347
w io.Writer
48+
bytesWritten uint64 // atomic access to this variable
4449
progressiveWrite bool
4550

51+
chunkMeta []MetaData
4652
chunkMetaBuf bytes.Buffer
4753
chunkBuf bytes.Buffer
48-
49-
chunkMeta []MetaData
50-
51-
compressed []byte // temp buf for snappy compression
52-
53-
// atomic access to this variable
54-
bytesWritten uint64
5554
}
5655

5756
// MetaData represents the data information inside a
@@ -64,7 +63,8 @@ type MetaData struct {
6463
// newChunkedContentCoder returns a new chunk content coder which
6564
// packs data into chunks based on the provided chunkSize
6665
func newChunkedContentCoder(chunkSize uint64, maxDocNum uint64,
67-
w io.Writer, progressiveWrite bool) *chunkedContentCoder {
66+
w io.Writer, progressiveWrite bool,
67+
) *chunkedContentCoder {
6868
total := maxDocNum/chunkSize + 1
6969
rv := &chunkedContentCoder{
7070
chunkSize: chunkSize,
@@ -191,7 +191,6 @@ func (c *chunkedContentCoder) Add(docNum uint64, vals []byte) error {
191191
//
192192
// | ..... data ..... | chunk offsets (varints)
193193
// | position of chunk offsets (uint64) | number of offsets (uint64) |
194-
//
195194
func (c *chunkedContentCoder) Write() (int, error) {
196195
var tw int
197196

0 commit comments

Comments
 (0)