Comments (7)
linkedbuffer是不是最优我建议还是要自己实际测试、实践来看看,不能别人说啥就信啥的。。。
from gnet.
这里的内存管理是指什么?能具体说一下吗?
from gnet.
这里的内存管理是指什么?能具体说一下吗?
基于gnet去实现框架的批量发送功能时,如果考虑内存复用,等特性,就需要自己去管理pool. 类似于netpoll的mux的writequeue,相当于gnet也有自己的writequeue了.
from gnet.
用 sync.Pool
就能实现一个简单的内存池了,结合 asyncwritev 的回调函数可以方便地回收内存到 pool,自己管理这部分在我看来也不麻烦啊,还是说你还想要其他的东西?能不能用(伪)代码具体表示一下你的确切需求?
from gnet.
用
sync.Pool
就能实现一个简单的内存池了,结合 asyncwritev 的回调函数可以方便地回收内存到 pool,自己管理这部分在我看来也不麻烦啊,还是说你还想要其他的东西?能不能用(伪)代码具体表示一下你的确切需求?
起因是希望能实现oneway形式的通讯模式的sub-pub system,需要提升单个tcp连接上的传输性能。 使用writev系统调用来传输数据是个很好的选择.但是内存管理比较麻烦。类似于并发安全的writequeue buffer, 可以对接writev系统调用。
这种内存管理有两个用途,一个是单个tcp连接上的多路复用,另外一个是应用级别的内存管理
from gnet.
异步库有太多事先不确定长度的buffer及append需求,append后size就不对齐了并且大于get时候的那个level的pool,所以常见的分级size的池很容易出现 small size pool 取出、放回 big size pool,这样就效果更差。
如果不分级、只用一个pool,同样存在append的问题,下次取出来没法保证size够用,可能会导致pool内的buffer都向大size看齐。
基于标准库同步接口的Conn、同步编解码逻辑的框架,不涉及这个问题,pool的用法也相对容易的多,异步库非常麻烦。
我的方案是默认使用单个pool在通常场景下够用,比较特殊的时候不使用pool。
高版本go支持debug.SetMemoryLimit后,相对好用的多,不太需要自己去判断并手动GC了,这两种都可以配合debug.SetMemoryLimit来尽量及时回收内存来保障程序的稳定。
在做ws百万连接测试中有尝试用debug.SetMemoryLimit,效果确实还不错,不一定能提升响应性能,但能让程序尽量稳定,推荐使用。
另外一种方案是用cgo去搞,一些团队有这样做、用cgo去调用jemalloc那些,效果挺不错的。但cgo又需要精确控制malloc/free一对一的调用,跟arena草案用法类似,都引入了更多的复杂度和心智负担。因为我自己的一些项目实现里没有保障一对一的malloc/free,所以用这种有风险。为了避免泄漏,又弄了个利用SetFinalizer在[]byte变量GC时自动cgo free的玩具,但GC及时性仍然是不够好的,如果配合手动GC能确保及时性其实也还好、但非必要不想增加手动的额外开销,所以暂时没有在生产中使用过这个
from gnet.
异步库有太多事先不确定长度的buffer及append需求,append后size就不对齐了并且大于get时候的那个level的pool,所以常见的分级size的池很容易出现 small size pool 取出、放回 big size pool,这样就效果更差。
如果不分级、只用一个pool,同样存在append的问题,下次取出来没法保证size够用,可能会导致pool内的buffer都向大size看齐。
基于标准库同步接口的Conn、同步编解码逻辑的框架,不涉及这个问题,pool的用法也相对容易的多,异步库非常麻烦。
我的方案是默认使用单个pool在通常场景下够用,比较特殊的时候不使用pool。 高版本go支持debug.SetMemoryLimit后,相对好用的多,不太需要自己去判断并手动GC了,这两种都可以配合debug.SetMemoryLimit来尽量及时回收内存来保障程序的稳定。 在做ws百万连接测试中有尝试用debug.SetMemoryLimit,效果确实还不错,不一定能提升响应性能,但能让程序尽量稳定,推荐使用。
另外一种方案是用cgo去搞,一些团队有这样做、用cgo去调用jemalloc那些,效果挺不错的。但cgo又需要精确控制malloc/free一对一的调用,跟arena草案用法类似,都引入了更多的复杂度和心智负担。因为我自己的一些项目实现里没有保障一对一的malloc/free,所以用这种有风险。为了避免泄漏,又弄了个利用SetFinalizer在[]byte变量GC时自动cgo free的玩具,但GC及时性仍然是不够好的,如果配合手动GC能确保及时性其实也还好、但非必要不想增加手动的额外开销,所以暂时没有在生产中使用过这个
类似于netpoll的linkedbuffer确实是实践出来的参数,什么情况下的buffer node需要重新分配,什么样的情况下node可以直接使用,确实很麻烦。
from gnet.
Related Issues (20)
- [Question]: Is the netpoll v1 version defaulting to horizontal triggering, and the V2 version defaulting to edge triggering? HOT 15
- [Bug]: fatal error: concurrent map read and map write HOT 1
- Inconsistent behaviors with SO_REUSEPORT on Linux and *BSD HOT 7
- Don't use SO_REUSEPORT on Unix domain sockets HOT 1
- Don't disable SO_REUSEPORT on DragonFlyBSD HOT 1
- [Question]: gnet client works not as expected even tho i did it according to the api ref HOT 1
- [Bug]: GNET client not working as expected HOT 4
- [Bug]: example gnet for echo tcp uses too much memory HOT 7
- [Bug]: Read returns io.ErrShortBuffer on closed connection HOT 7
- Special treatment for the first package HOT 8
- [Question]: How to shut down the started tcp server and turn off port listening? HOT 2
- [Question]: When using io.CopyBuffer(), do you need to call Flush to send the data? HOT 2
- [Bug]: Discard() panic HOT 17
- [Feature]: `io_uring` support HOT 4
- [Question]: there's gnet https server now, how to use gnet https client? HOT 1
- UDP FD handle is not released HOT 1
- [Question]: The redis server service implemented using gnet has too many read tcp i/o timeouts in the production environment. HOT 6
- [Question]: redhub upgraded to use gnet/v2 issues, seemed "stalled" HOT 1
- [Feature]: tls support HOT 3
- [Bug]: gnet.Conn.Peek crashes slice bounds out of range HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gnet.