pool.go 992 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package pool
  2. import (
  3. "bytes"
  4. "math/big"
  5. "sync"
  6. )
  7. var bytesBufferPool = sync.Pool{
  8. New: allocBytesBuffer,
  9. }
  10. func allocBytesBuffer() interface{} {
  11. return &bytes.Buffer{}
  12. }
  13. func GetBytesBuffer() *bytes.Buffer {
  14. //nolint:forcetypeassert
  15. return bytesBufferPool.Get().(*bytes.Buffer)
  16. }
  17. func ReleaseBytesBuffer(b *bytes.Buffer) {
  18. b.Reset()
  19. bytesBufferPool.Put(b)
  20. }
  21. var bigIntPool = sync.Pool{
  22. New: allocBigInt,
  23. }
  24. func allocBigInt() interface{} {
  25. return &big.Int{}
  26. }
  27. func GetBigInt() *big.Int {
  28. //nolint:forcetypeassert
  29. return bigIntPool.Get().(*big.Int)
  30. }
  31. func ReleaseBigInt(i *big.Int) {
  32. bigIntPool.Put(i.SetInt64(0))
  33. }
  34. var keyToErrorMapPool = sync.Pool{
  35. New: allocKeyToErrorMap,
  36. }
  37. func allocKeyToErrorMap() interface{} {
  38. return make(map[string]error)
  39. }
  40. func GetKeyToErrorMap() map[string]error {
  41. //nolint:forcetypeassert
  42. return keyToErrorMapPool.Get().(map[string]error)
  43. }
  44. func ReleaseKeyToErrorMap(m map[string]error) {
  45. for key := range m {
  46. delete(m, key)
  47. }
  48. }