浏览代码

Merge pull request #82 from greatroar/hashtable-alloc

lz4block: Use pointer-to-array in pool for up to 30% throughput improvement
Pierre Curto 5 年之前
父节点
当前提交
8d633507ac
共有 1 个文件被更改,包括 14 次插入13 次删除
  1. 14 13
      internal/lz4block/block.go

+ 14 - 13
internal/lz4block/block.go

@@ -26,21 +26,19 @@ const (
 )
 
 // Pool of hash tables for CompressBlock.
-var HashTablePool = hashTablePool{sync.Pool{New: func() interface{} { return make([]int, htSize) }}}
+var HashTablePool = hashTablePool{sync.Pool{New: func() interface{} { return new([htSize]int) }}}
 
 type hashTablePool struct {
 	sync.Pool
 }
 
-func (p *hashTablePool) Get() []int {
-	return p.Pool.Get().([]int)
+func (p *hashTablePool) Get() *[htSize]int {
+	return p.Pool.Get().(*[htSize]int)
 }
 
 // Zero out the table to avoid non-deterministic outputs (see issue#65).
-func (p *hashTablePool) Put(t []int) {
-	for i := range t {
-		t[i] = 0
-	}
+func (p *hashTablePool) Put(t *[htSize]int) {
+	*t = [htSize]int{}
 	p.Pool.Put(t)
 }
 
@@ -90,8 +88,9 @@ func CompressBlock(src, dst []byte, hashTable []int) (_ int, err error) {
 	}
 
 	if cap(hashTable) < htSize {
-		hashTable = HashTablePool.Get()
-		defer HashTablePool.Put(hashTable)
+		poolTable := HashTablePool.Get()
+		defer HashTablePool.Put(poolTable)
+		hashTable = poolTable[:]
 	} else {
 		hashTable = hashTable[:htSize]
 	}
@@ -275,15 +274,17 @@ func CompressBlockHC(src, dst []byte, depth CompressionLevel, hashTable, chainTa
 	// hashTable: stores the last position found for a given hash
 	// chainTable: stores previous positions for a given hash
 	if cap(hashTable) < htSize {
-		hashTable = HashTablePool.Get()
-		defer HashTablePool.Put(hashTable)
+		poolTable := HashTablePool.Get()
+		defer HashTablePool.Put(poolTable)
+		hashTable = poolTable[:]
 	} else {
 		hashTable = hashTable[:htSize]
 	}
 	_ = hashTable[htSize-1]
 	if cap(chainTable) < htSize {
-		chainTable = HashTablePool.Get()
-		defer HashTablePool.Put(chainTable)
+		poolTable := HashTablePool.Get()
+		defer HashTablePool.Put(poolTable)
+		chainTable = poolTable[:]
 	} else {
 		chainTable = chainTable[:htSize]
 	}