forked from roadrunner-server/goridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Valery Piashchynski <[email protected]>
- Loading branch information
Showing
6 changed files
with
84 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package internal | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
var frameChunkedPool = &sync.Map{} | ||
var preallocate = &sync.Once{} | ||
|
||
const ( | ||
OneMB uint32 = 1024 * 1024 * 1 | ||
FiveMB uint32 = 1024 * 1024 * 5 | ||
TenMB uint32 = 1024 * 1024 * 10 | ||
) | ||
|
||
func Preallocate() { | ||
preallocate.Do(internalAllocate) | ||
} | ||
|
||
func internalAllocate() { | ||
pool1 := &sync.Pool{ | ||
New: func() interface{} { | ||
data := make([]byte, OneMB) | ||
return &data | ||
}, | ||
} | ||
pool5 := &sync.Pool{ | ||
New: func() interface{} { | ||
data := make([]byte, FiveMB) | ||
return &data | ||
}, | ||
} | ||
pool10 := &sync.Pool{ | ||
New: func() interface{} { | ||
data := make([]byte, TenMB) | ||
return &data | ||
}, | ||
} | ||
|
||
frameChunkedPool.Store(OneMB, pool1) | ||
frameChunkedPool.Store(FiveMB, pool5) | ||
frameChunkedPool.Store(TenMB, pool10) | ||
} | ||
|
||
func get(size uint32) *[]byte { | ||
switch { | ||
case size <= OneMB: | ||
val, _ := frameChunkedPool.Load(OneMB) | ||
return val.(*sync.Pool).Get().(*[]byte) | ||
case size <= FiveMB: | ||
val, _ := frameChunkedPool.Load(FiveMB) | ||
return val.(*sync.Pool).Get().(*[]byte) | ||
case size <= TenMB: | ||
val, _ := frameChunkedPool.Load(TenMB) | ||
return val.(*sync.Pool).Get().(*[]byte) | ||
default: | ||
data := make([]byte, size) | ||
return &data | ||
} | ||
} | ||
|
||
func put(size uint32, data *[]byte) { | ||
switch { | ||
case size <= OneMB: | ||
pool, _ := frameChunkedPool.Load(OneMB) | ||
pool.(*sync.Pool).Put(data) | ||
return | ||
case size <= FiveMB: | ||
pool, _ := frameChunkedPool.Load(FiveMB) | ||
pool.(*sync.Pool).Put(data) | ||
return | ||
default: | ||
pool, _ := frameChunkedPool.Load(TenMB) | ||
pool.(*sync.Pool).Put(data) | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters