@@ -202,46 +202,25 @@ type block struct {
202
202
ref int32
203
203
}
204
204
205
- // incrRef increments the ref of a block and return a bool indicating if the
206
- // increment was successful. A true value indicates that the block can be used.
207
- func (b * block ) incrRef () bool {
208
- for {
209
- // We can't blindly add 1 to ref. We need to check whether it has
210
- // reached zero first, because if it did, then we should absolutely not
211
- // use this block.
212
- ref := atomic .LoadInt32 (& b .ref )
213
- // The ref would not be equal to 0 unless the existing
214
- // block get evicted before this line. If the ref is zero, it means that
215
- // the block is already added the the blockPool and cannot be used
216
- // anymore. The ref of a new block is 1 so the following condition will
217
- // be true only if the block got reused before we could increment its
218
- // ref.
219
- if ref == 0 {
220
- return false
221
- }
222
- // Increment the ref only if it is not zero and has not changed between
223
- // the time we read it and we're updating it.
224
- //
225
- if atomic .CompareAndSwapInt32 (& b .ref , ref , ref + 1 ) {
226
- return true
227
- }
228
- }
205
+ func (b * block ) incrRef () {
206
+ atomic .AddInt32 (& b .ref , 1 )
229
207
}
230
208
func (b * block ) decrRef () {
231
209
if b == nil {
232
210
return
233
211
}
234
212
213
+ p := atomic .AddInt32 (& b .ref , - 1 )
235
214
// Insert the []byte into pool only if the block is resuable. When a block
236
215
// is reusable a new []byte is used for decompression and this []byte can
237
216
// be reused.
238
217
// In case of an uncompressed block, the []byte is a reference to the
239
218
// table.mmap []byte slice. Any attempt to write data to the mmap []byte
240
219
// will lead to SEGFAULT.
241
- if atomic . AddInt32 ( & b . ref , - 1 ) == 0 && b .isReusable {
220
+ if p == 0 && b .isReusable {
242
221
blockPool .Put (& b .data )
243
222
}
244
- y .AssertTrue (atomic . LoadInt32 ( & b . ref ) >= 0 )
223
+ y .AssertTrue (p >= 0 )
245
224
}
246
225
func (b * block ) size () int64 {
247
226
return int64 (3 * intSize /* Size of the offset, entriesIndexStart and chkLen */ +
@@ -499,9 +478,6 @@ func calculateOffsetsSize(offsets []*pb.BlockOffset) int64 {
499
478
return totalSize + 3 * 8
500
479
}
501
480
502
- // block function return a new block. Each block holds a ref and the byte
503
- // slice stored in the block will be reused when the ref becomes zero. The
504
- // caller should release the block by calling block.decrRef() on it.
505
481
func (t * Table ) block (idx int ) (* block , error ) {
506
482
y .AssertTruef (idx >= 0 , "idx=%d" , idx )
507
483
if idx >= t .noOfBlocks {
@@ -511,20 +487,14 @@ func (t *Table) block(idx int) (*block, error) {
511
487
key := t .blockCacheKey (idx )
512
488
blk , ok := t .opt .Cache .Get (key )
513
489
if ok && blk != nil {
514
- // Use the block only if the increment was successful. The block
515
- // could get evicted from the cache between the Get() call and the
516
- // incrRef() call.
517
- if b := blk .(* block ); b .incrRef () {
518
- return b , nil
519
- }
490
+ return blk .(* block ), nil
520
491
}
521
492
}
522
493
523
494
// Read the block index if it's nil
524
495
ko := t .blockOffsets ()[idx ]
525
496
blk := & block {
526
497
offset : int (ko .Offset ),
527
- ref : 1 ,
528
498
}
529
499
var err error
530
500
if blk .data , err = t .read (blk .offset , int (ko .Len )); err != nil {
@@ -581,14 +551,8 @@ func (t *Table) block(idx int) (*block, error) {
581
551
}
582
552
if t .opt .Cache != nil && t .opt .KeepBlocksInCache {
583
553
key := t .blockCacheKey (idx )
584
- // incrRef should never return false here because we're calling it on a
585
- // new block with ref=1.
586
- y .AssertTrue (blk .incrRef ())
587
-
588
- // Decrement the block ref if we could not insert it in the cache.
589
- if ! t .opt .Cache .Set (key , blk , blk .size ()) {
590
- blk .decrRef ()
591
- }
554
+ blk .incrRef ()
555
+ t .opt .Cache .Set (key , blk , blk .size ())
592
556
}
593
557
return blk , nil
594
558
}
0 commit comments