Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Go temporary object Pool pool

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use pool, the temporary object pool of Go. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

What is the temporary object pool pool?

Sync.Pool gave a long comment on what pool is. Let's take a look at what this paragraph says.

Temporary object pools are temporary objects that can be stored and retrieved separately.

Objects in the pool are removed (released or reused) without any notification. If a unique reference to an object is held in the pool, the object is likely to be recycled.

Pool is secure in a multi-goroutine usage environment.

Pool is used to relieve GC pressure by caching memory that has been applied for, which is currently unused, and which may be used next. Using it, you can easily and efficiently build thread-safe free list (a data structure for dynamic memory requests). However, it is not suitable for free list in all scenarios.

Silently sharing a set of temporary elements among multiple independent threads running independently in the same package is a reasonable use scenario for pool. Pool provides a mechanism for sharing temporary elements among multiple independent client.

There is an example of using Pool in the fmt package, which maintains a dynamic-sized output buffer.

In addition, some objects with short lifecycles are not suitable for maintenance using pool, so it is not cost-effective to use pool in this case. This should use their own free list (here may refer to the shared pool in the go memory model used to cache = 0 {x = l.shared [last] l.shared = l.shared [: last]} l.Unlock () / / find other P if x = = nil {x = p.getSlow ()}} if race.Enabled {race.Enable () if x! = nil {race.Acquire (poolRaceAddr (x) )}} / / No available elements were found Call New to generate if x = = nil & & p.New! = nil {x = p.New ()} return x}

GetSlow to get available elements from the shared pool in the other P:

Func (p * Pool) getSlow () (x interface {}) {/ / See the comment in pin regarding ordering of the loads. Size: = atomic.LoadUintptr (& p.localSize) / / load-acquire local: = p.local / / load-consume / / Try to steal one element from other procs. Pid: = runtime_procPin () runtime_procUnpin () for I: = 0; I

< int(size); i++ { l := indexLocal(local, (pid+i+1)%int(size)) // 对应 pool 需加锁 l.Lock() last := len(l.shared) - 1 if last >

= 0 {x = l.shared [last] l.shared = l.shared [: last] l.Unlock () break} l.Unlock ()} return x}

Put

Put prioritizes elements in the private pool, or if the private is not empty, in the shared pool. Interestingly, one of the elements may be discarded before entering the pool.

Func (p * Pool) Put (x interface {}) {if x = = nil {return} if race.Enabled {if fastrand ()% 4 = = 0 {/ / throw the element away at random. / / Randomly drop x on floor. Return} race.ReleaseMerge (poolRaceAddr (x)) race.Disable ()} l: = p.pin () if l.private = = nil {l.private = x = nil} runtime_procUnpin () if x! = nil {/ / shared pool access, need to lock l.Lock () l.shared = append (l.shared, x) l.Unlock ()} if race.Enabled {race.Enable ()}}

PoolCleanup

PoolCleanup is called when the world pauses and garbage collection is about to begin. Memory cannot be allocated within this function and no run-time functions can be called. Reason:

Prevent incorrect retention of the entire Pool

If a goroutine is accessing the l.shared when the GC occurs, the entire Pool will be retained and there will be double memory the next time it is executed

Func poolCleanup () {for I, p: = range allPools {allPools [I] = nil for i: = 0; I < int (p.localSize); iTunes + {l: = indexLocal (p.local, I) l.private = nil for j: = range l.shared {l.shared [j] = nil} l.shared = nil} p.local = nil p.localSize = 0} allPools = [] * Pool {}}

Context pool in case 1:gin

In web applications, the background creates a context Context for the current request when processing each request from the user, which is used to store request information and corresponding information. Context meets the characteristics of long life cycle, and user requests also belong to the concurrent environment, so thread-safe Pool is very suitable for maintaining the temporary object pool of Context.

Gin defines a pool in the structure Engine:

Type Engine struct {/ /... Other fields pool sync.Pool} are omitted.

The New function of pool is defined when initializing engine:

Engine.pool.New = func () interface {} {return engine.allocateContext ()} / / allocateContextfunc (engine * Engine) allocateContext () * Context {/ / construct a new context object return & Context {engine: engine}}

ServeHttp:

/ / get it from pool and convert it to * Contextc: = engine.pool.Get (). (* Context) c.writermem.reset (w) c.Request = reqc.reset () / / resetengine.handleHTTPRequest (c) / / and throw it back into pool engine.pool.Put (c)

Printer pool in case 2:fmt

Printer is also in line with the characteristics of long life cycle, and may also be used in multi-goroutine, so it is also suitable to use pool for maintenance.

Printer and its temporary object pool

/ / pp is used to maintain the state of printer / / it is reused through sync.Pool to avoid requesting memory type pp struct {/ /. Field has been omitted} var ppFree = sync.Pool {New: func () interface {} {return new (pp)},}

Get and release:

Func newPrinter () * pp {p: = ppFree.Get (). (* pp) p.panicking = false p.erroring = false p.fmt.init (& p.buf) return p} func (p * pp) free () {p.buf = p.buf [: 0] p.arg = nil p.value = reflect.Value {} ppFree.Put (p)} Thank you for reading! This is the end of this article on "how to use Go temporary object Pool pool". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report