From 020959a1d58ce7467e45091a5dbd5d1975741018 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 4 Aug 2018 09:17:21 +0800 Subject: [PATCH 1/6] rename some symbol --- ants.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ants.go b/ants.go index f22782e..3191b1f 100644 --- a/ants.go +++ b/ants.go @@ -28,39 +28,40 @@ import ( ) const ( - // DefaultPoolSize is the default capacity for a default goroutine pool - DefaultPoolSize = math.MaxInt32 + // DefaultAntsPoolSize is the default capacity for a default goroutine pool + DefaultAntsPoolSize = math.MaxInt32 // DefaultCleanIntervalTime is the interval time to clean up goroutines DefaultCleanIntervalTime = 10 ) // Init a instance pool when importing ants -var defaultPool, _ = NewPool(DefaultPoolSize) +var defaultAntsPool, _ = NewPool(DefaultAntsPoolSize) // Submit submit a task to pool func Submit(task f) error { - return defaultPool.Submit(task) + return defaultAntsPool.Submit(task) } // Running returns the number of the currently running goroutines func Running() int { - return defaultPool.Running() + return defaultAntsPool.Running() } // Cap returns the capacity of this default pool func Cap() int { - return defaultPool.Cap() + return defaultAntsPool.Cap() } // Free returns the available goroutines to work func Free() int { - return defaultPool.Free() + return defaultAntsPool.Free() } // Release Closed the default pool func Release() { - defaultPool.Release() + defaultAntsPool.Release() + } // Errors for the Ants API From 9af581c8674229d2deb0cff7bb7d4cbf4ca8ce71 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 4 Aug 2018 09:29:53 +0800 Subject: [PATCH 2/6] optimization for default pool --- ants.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/ants.go b/ants.go index 3191b1f..6b3d51c 100644 --- a/ants.go +++ b/ants.go @@ -36,7 +36,26 @@ const ( ) // Init a instance pool when importing ants -var defaultAntsPool, _ = NewPool(DefaultAntsPoolSize) +var ( + defaultAntsPool *Pool + err error +) + +func init() { + defaultAntsPool, err = NewPool(DefaultAntsPoolSize) + if err != nil { + panic(err) + } +} + +// Release Closed the default pool +func Release() { + defaultAntsPool.Release() + defaultAntsPool, err = NewPool(DefaultAntsPoolSize) + if err != nil { + panic(err) + } +} // Submit submit a task to pool func Submit(task f) error { @@ -58,12 +77,6 @@ func Free() int { return defaultAntsPool.Free() } -// Release Closed the default pool -func Release() { - defaultAntsPool.Release() - -} - // Errors for the Ants API var ( ErrInvalidPoolSize = errors.New("invalid size for pool") From 87b15034c59fbd3340a67ebeae1e30b8ca5b54fb Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 4 Aug 2018 09:42:25 +0800 Subject: [PATCH 3/6] optimization for default pool --- ants.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/ants.go b/ants.go index 6b3d51c..2e018e8 100644 --- a/ants.go +++ b/ants.go @@ -48,15 +48,6 @@ func init() { } } -// Release Closed the default pool -func Release() { - defaultAntsPool.Release() - defaultAntsPool, err = NewPool(DefaultAntsPoolSize) - if err != nil { - panic(err) - } -} - // Submit submit a task to pool func Submit(task f) error { return defaultAntsPool.Submit(task) @@ -77,6 +68,11 @@ func Free() int { return defaultAntsPool.Free() } +// Release Closed the default pool +func Release() { + defaultAntsPool.Release() +} + // Errors for the Ants API var ( ErrInvalidPoolSize = errors.New("invalid size for pool") From 1833ff6764a0ca28bb1d2714c197925a3a06f7e5 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 4 Aug 2018 10:34:02 +0800 Subject: [PATCH 4/6] optimization for go test --- ants_test.go | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/ants_test.go b/ants_test.go index f61efe5..83519e8 100644 --- a/ants_test.go +++ b/ants_test.go @@ -32,6 +32,7 @@ import ( ) var n = 100000 +var curMem uint64 func TestAntsPoolWithFunc(t *testing.T) { var wg sync.WaitGroup @@ -50,7 +51,24 @@ func TestAntsPoolWithFunc(t *testing.T) { t.Logf("pool with func, running workers number:%d", p.Running()) mem := runtime.MemStats{} runtime.ReadMemStats(&mem) - t.Logf("memory usage:%d", mem.TotalAlloc/GiB) + curMem = mem.TotalAlloc / MiB - curMem + t.Logf("memory usage:%d", curMem) +} +func TestNoPool(t *testing.T) { + var wg sync.WaitGroup + for i := 0; i < n; i++ { + wg.Add(1) + go func() { + demoFunc() + wg.Done() + }() + } + + wg.Wait() + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + curMem = mem.TotalAlloc/MiB - curMem + t.Logf("memory usage:%d MB", curMem) } func TestAntsPool(t *testing.T) { @@ -72,23 +90,8 @@ func TestAntsPool(t *testing.T) { mem := runtime.MemStats{} runtime.ReadMemStats(&mem) - t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB) -} - -func TestNoPool(t *testing.T) { - var wg sync.WaitGroup - for i := 0; i < n; i++ { - wg.Add(1) - go func() { - demoFunc() - wg.Done() - }() - } - - wg.Wait() - mem := runtime.MemStats{} - runtime.ReadMemStats(&mem) - t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB) + curMem = mem.TotalAlloc/MiB - curMem + t.Logf("memory usage:%d MB", curMem) } func TestCodeCov(t *testing.T) { From bb816023421843b23f0d085963c30de8d2629c3f Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 4 Aug 2018 10:34:35 +0800 Subject: [PATCH 5/6] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 198ed04..b609cca 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,7 @@ In that benchmark-picture, the first and second benchmarks performed test with 1 ![](ants_bench_1000w.png) -There was only the test of `ants` Pool because my computer was crash when it reached 10M goroutines. +There was only the test of `ants` Pool because my computer was crash when it reached 10M goroutines without pool. **As you can see, `ants` can up to 2x~6x faster than goroutines without pool and the memory consumption is reduced by 10 to 20 times.** From 76cc065dbc622a70babbc0c2a3d17f2fc944e89e Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 4 Aug 2018 10:36:30 +0800 Subject: [PATCH 6/6] optimization --- ants.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ants.go b/ants.go index 2e018e8..3cbd5b6 100644 --- a/ants.go +++ b/ants.go @@ -35,12 +35,12 @@ const ( DefaultCleanIntervalTime = 10 ) -// Init a instance pool when importing ants var ( defaultAntsPool *Pool err error ) +// Init a instance pool when importing ants func init() { defaultAntsPool, err = NewPool(DefaultAntsPoolSize) if err != nil {