Skip to content

Commit

Permalink
MB-57871 - Avoid unnecessary atomic operation on bytesWritten (#278)
Browse files Browse the repository at this point in the history
- the construct of bytes written while constructing a segment is not a
shared resource i.e. a single thread operates on a segment creation.
Hence the atomic.Add() operation is not necessary here.
- Tested using bleve unit test with race flace 

```
go test -run=TestBytesWritten --race
PASS
ok  	github.com/blevesearch/bleve/v2	2.294s
```
  • Loading branch information
Thejas-bhat authored Oct 25, 2024
1 parent 5f8f0f0 commit ef2b41d
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 12 deletions.
7 changes: 3 additions & 4 deletions contentcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/binary"
"io"
"reflect"
"sync/atomic"

"github.com/golang/snappy"
)
Expand All @@ -37,7 +36,7 @@ var (
)

type chunkedContentCoder struct {
bytesWritten uint64 // atomic access to this variable, moved to top to correct alignment issues on ARM, 386 and 32-bit MIPS.
bytesWritten uint64 // moved to top to correct alignment issues on ARM, 386 and 32-bit MIPS.

final []byte
chunkSize uint64
Expand Down Expand Up @@ -112,11 +111,11 @@ func (c *chunkedContentCoder) Close() error {
}

func (c *chunkedContentCoder) incrementBytesWritten(val uint64) {
atomic.AddUint64(&c.bytesWritten, val)
c.bytesWritten += val
}

func (c *chunkedContentCoder) getBytesWritten() uint64 {
return atomic.LoadUint64(&c.bytesWritten)
return c.bytesWritten
}

func (c *chunkedContentCoder) flushContents() error {
Expand Down
1 change: 0 additions & 1 deletion docvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ type docValueReader struct {
curChunkData []byte // compressed data cache
uncompressed []byte // temp buf for snappy decompression

// atomic access to this variable
bytesRead uint64
}

Expand Down
1 change: 0 additions & 1 deletion intDecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ type chunkedIntDecoder struct {
data []byte
r *memUvarintReader

// atomic access to this variable
bytesRead uint64
}

Expand Down
6 changes: 2 additions & 4 deletions intcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"bytes"
"encoding/binary"
"io"
"sync/atomic"
)

// We can safely use 0 to represent termNotEncoded since 0
Expand All @@ -36,7 +35,6 @@ type chunkedIntCoder struct {

buf []byte

// atomic access to this variable
bytesWritten uint64
}

Expand Down Expand Up @@ -79,11 +77,11 @@ func (c *chunkedIntCoder) SetChunkSize(chunkSize uint64, maxDocNum uint64) {
}

func (c *chunkedIntCoder) incrementBytesWritten(val uint64) {
atomic.AddUint64(&c.bytesWritten, val)
c.bytesWritten += val
}

func (c *chunkedIntCoder) getBytesWritten() uint64 {
return atomic.LoadUint64(&c.bytesWritten)
return c.bytesWritten
}

// Add encodes the provided integers into the correct chunk for the provided
Expand Down
4 changes: 2 additions & 2 deletions section_inverted_text_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,11 @@ func (i *invertedIndexOpaque) grabBuf(size int) []byte {
}

func (i *invertedIndexOpaque) incrementBytesWritten(bytes uint64) {
atomic.AddUint64(&i.bytesWritten, bytes)
i.bytesWritten += bytes
}

func (i *invertedIndexOpaque) BytesWritten() uint64 {
return atomic.LoadUint64(&i.bytesWritten)
return i.bytesWritten
}

func (i *invertedIndexOpaque) BytesRead() uint64 {
Expand Down

0 comments on commit ef2b41d

Please sign in to comment.