From d931ac0d55347beea159673db5247c38f368d268 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 27 Jul 2019 02:25:26 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=93Add=20a=20section=20for=20new=20fea?= =?UTF-8?q?ture:=20pre-malloc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 19 +++++++++++++++---- README_ZH.md | 21 +++++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) 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