diff --git a/README.md b/README.md index 826ba46..7037201 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # ants - -
-

A goroutine pool for Go

- +

+ +

+A goroutine pool for Go +

@@ -10,6 +11,7 @@
+

[中文](README_ZH.md) | [Project Blog](https://taohuawu.club/high-performance-implementation-of-goroutine-pool) @@ -190,6 +192,15 @@ pool.Tune(100000) // Tune its capacity to 100000 Don't worry about the synchronous problems in this case, the function here is thread-safe (or should be called goroutine-safe). +## Pre-malloc goroutine queue in pool + +`ants` allows you to pre-allocate memory of goroutine queue in pool, which may get a performance enhancement under some special certain circumstances such as the scenario that requires an pool with ultra-large capacity, meanwhile each task in goroutine lasts for a long time, in this case, pre-mallocing will reduce a lot of costs when re-slicing goroutine queue. + +```go +// ants will pre-malloc the whole capacity of pool when you invoke this function +p, _ := ants.NewPoolPreMalloc(AntsSize) +``` + ## Release Pool ```go diff --git a/README_ZH.md b/README_ZH.md index 5b5578b..4e437f4 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -1,8 +1,9 @@ # ants - -
-

A goroutine pool for Go

- +

+ +

+A goroutine pool for Go +

@@ -10,6 +11,7 @@
] +

[英文](README.md) | [项目博客](https://taohuawu.club/high-performance-implementation-of-goroutine-pool) @@ -190,6 +192,17 @@ pool.Tune(100000) // Tune its capacity to 100000 该方法是线程安全的。 +## 预先分配goroutine队列内存 + +`ants`允许你预先把整个池的容量分配内存, 这个功能可以在某些特定的场景下提高协程池的性能。比如, 有一个场景需要一个超大容量的池,而且每个goroutine里面的任务都是耗时任务,这种情况下,预先分配goroutine队列内存将会减少re-slice时的复制内存损耗。 + +```go +// ants will pre-malloc the whole capacity of pool when you invoke this function +p, _ := ants.NewPoolPreMalloc(AntsSize) +``` + + + ## 销毁协程池 ```go