Skip to content

Commit

Permalink
Add byte slice pool
Browse files Browse the repository at this point in the history
  • Loading branch information
n10v committed May 24, 2017
1 parent 1d4e19d commit 060e463
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
13 changes: 13 additions & 0 deletions bspool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "sync"

var bsPool = sync.Pool{New: func() interface{} { return make([]byte, 0, 128*1024) }}

func getByteSlice() []byte {
return bsPool.Get().([]byte)
}

func putByteSlice(bs []byte) {
bsPool.Put(bs)
}
3 changes: 2 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func analyzeFile(filename string, language *Language) *ClocFile {
isFirstLine := true
isInComments := false
isInCommentsSame := false
buf := make([]byte, 0, 128*1024)
buf := getByteSlice()
defer putByteSlice(buf)
scanner := bufio.NewScanner(fp)
scanner.Buffer(buf, 1024*1024)
for scanner.Scan() {
Expand Down

0 comments on commit 060e463

Please sign in to comment.