From ef2b41dc3ae17c3e3c44ba4d2e003e7e0abee786 Mon Sep 17 00:00:00 2001 From: Thejas-bhat <35959007+Thejas-bhat@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:24:58 +0530 Subject: [PATCH] MB-57871 - Avoid unnecessary atomic operation on bytesWritten (#278) - 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 ``` --- contentcoder.go | 7 +++---- docvalues.go | 1 - intDecoder.go | 1 - intcoder.go | 6 ++---- section_inverted_text_index.go | 4 ++-- 5 files changed, 7 insertions(+), 12 deletions(-) diff --git a/contentcoder.go b/contentcoder.go index cd8b3fc8..3343d317 100644 --- a/contentcoder.go +++ b/contentcoder.go @@ -19,7 +19,6 @@ import ( "encoding/binary" "io" "reflect" - "sync/atomic" "github.com/golang/snappy" ) @@ -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 @@ -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 { diff --git a/docvalues.go b/docvalues.go index 6fb7a9a2..3d0d269f 100644 --- a/docvalues.go +++ b/docvalues.go @@ -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 } diff --git a/intDecoder.go b/intDecoder.go index e50c4717..1a69e614 100644 --- a/intDecoder.go +++ b/intDecoder.go @@ -27,7 +27,6 @@ type chunkedIntDecoder struct { data []byte r *memUvarintReader - // atomic access to this variable bytesRead uint64 } diff --git a/intcoder.go b/intcoder.go index 2957fbd0..e1586edc 100644 --- a/intcoder.go +++ b/intcoder.go @@ -18,7 +18,6 @@ import ( "bytes" "encoding/binary" "io" - "sync/atomic" ) // We can safely use 0 to represent termNotEncoded since 0 @@ -36,7 +35,6 @@ type chunkedIntCoder struct { buf []byte - // atomic access to this variable bytesWritten uint64 } @@ -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 diff --git a/section_inverted_text_index.go b/section_inverted_text_index.go index 91a126e5..ea8722e4 100644 --- a/section_inverted_text_index.go +++ b/section_inverted_text_index.go @@ -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 {