10

Github GitHub - rocketlaunchr/go-pool

 3 years ago
source link: https://github.com/rocketlaunchr/go-pool
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

star   the project to show your appreciation. arrow_upper_right

A Better sync.Pool

This package is a thin wrapper over the Pool provided by the sync package. The Pool is an essential package to obtain maximum performance by reducing the number of memory allocations.

Extra Features

  • Invalidate an item from the Pool (so it never gets used again)
  • Set a maximum number of items for the Pool
  • Returns the number of items in the pool (idle and in-use)

When should I use a pool?

If you frequently allocate many objects of the same type and you want to save some memory allocation and garbage allocation overhead — @jrv

How did I improve latency by 700% using sync.Pool

Example

import "github.com/rocketlaunchr/go-pool"

pool := pool.New(5) // maximum of 5 items in pool
pool.SetFactory(func() interface{} {
	return &X{}
})

item := pool.Borrow()
defer item.Return()

// Use item here or mark as invalid
x := item.Item.(*X) // Use item here
item.MarkAsInvalid()

Other useful packages

  • awesome-svelte - Resources for killing react
  • dataframe-go - Statistics and data manipulation
  • dbq - Zero boilerplate database operations for Go
  • electron-alert - SweetAlert2 for Electron Applications
  • google-search - Scrape google search results
  • igo - A Go transpiler with cool new syntax such as fordefer (defer for for-loops)
  • mysql-go - Properly cancel slow MySQL queries
  • react - Build front end applications using Go
  • remember-go - Cache slow database queries
  • testing-go - Testing framework for unit testing

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK