netpoll icon indicating copy to clipboard operation
netpoll copied to clipboard

feat: Added Reuse and Initialize methods to LinkBuffer

Open JHue58 opened this issue 1 year ago • 3 comments

When used with sharequeue, the nodes of LinkBuffer are set to nil after Append, which prevents reuse and causes GC pressure.

JHue58 avatar Feb 19 '24 13:02 JHue58

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Feb 19 '24 13:02 CLAassistant

the nodes of LinkBuffer are set to nil after Append, which prevents reuse and causes GC pressure.

@Nana-Miko Hi, can you show me your code and how it cause GC pressure?

joway avatar Feb 20 '24 07:02 joway

the nodes of LinkBuffer are set to nil after Append, which prevents reuse and causes GC pressure.

@Nana-Miko Hi, can you show me your code and how it cause GC pressure?

sure!

queue := mux.NewShardQueue(mux.ShardSize, conn)

	// Simulating pressure environment using for loops
	for {
		var getter mux.WriterGetter = func() (buf Writer, isNil bool) {
			// I can't use sync.Pool to get buf
			// In this func,'var buf = &LinkBuffer{}' will continuously allocate memory
			buf = NewLinkBuffer(128)

			buf.Malloc(128)
			buf.Flush()
			return buf, false
		}
		queue.Add(getter)
	}

In this example, is there any way to reuse LinkBuffer using sync.Pool?

JHue58 avatar Feb 20 '24 08:02 JHue58