Giter Club home page Giter Club logo

itogami's Introduction

Itogami

An experimental goroutine pool implemented using a lock-free stack

By limiting concurrency with a fixed pool size and recycling goroutines using a stack, itogami saves a lot of memory as compared to using unlimited goroutines and remaining just as fast.

Benchmarks to support the above claims here

Note:- This work is experimental and should not be used in production

Installation

You need Golang 1.19.x or above

$ go get github.com/alphadose/itogami

Usage

package main

import (
	"fmt"
	"sync"
	"sync/atomic"
	"time"

	"github.com/alphadose/itogami"
)

const runTimes uint32 = 1000

var sum uint32

func myFunc(i uint32) {
	atomic.AddUint32(&sum, i)
	fmt.Printf("run with %d\n", i)
}

func demoFunc() {
	time.Sleep(10 * time.Millisecond)
	println("Hello World")
}

func examplePool() {
	var wg sync.WaitGroup
	// Use the common pool
	pool := itogami.NewPool(10)

	syncCalculateSum := func() {
		demoFunc()
		wg.Done()
	}
	for i := uint32(0); i < runTimes; i++ {
		wg.Add(1)
		// Submit task to the pool
		pool.Submit(syncCalculateSum)
	}
	wg.Wait()
	println("finished all tasks")
}

func examplePoolWithFunc() {
	var wg sync.WaitGroup
	// Use the pool with a pre-defined function
	pool := itogami.NewPoolWithFunc(10, func(i uint32) {
		myFunc(i)
		wg.Done()
	})
	for i := uint32(0); i < runTimes; i++ {
		wg.Add(1)
		// Invoke the function with a value
		pool.Invoke(i)
	}
	wg.Wait()
	fmt.Printf("finish all tasks, result is %d\n", sum)
}

func main() {
	examplePool()
	examplePoolWithFunc()
}

Benchmarks

Benchmarking was performed against:-

  1. Unlimited goroutines
  2. Ants
  3. Gamma-Zero-Worker-Pool
  4. golang.org/x/sync/errgroup
  5. Bytedance GoPool

Pool size -> 50k

CPU -> M1, arm64, 8 cores, 3.2 GHz

OS -> darwin

Results were computed from benchstat of 30 cases

name                   time/op
UnlimitedGoroutines-8   331ms ± 4%
ErrGroup-8              515ms ± 9%
AntsPool-8              582ms ± 9%
GammaZeroPool-8         740ms ±13%
BytedanceGoPool-8       572ms ±18%
ItogamiPool-8           337ms ± 1%

name                   alloc/op
UnlimitedGoroutines-8  96.3MB ± 0%
ErrGroup-8              120MB ± 0%
AntsPool-8             22.4MB ± 6%
GammaZeroPool-8        18.8MB ± 1%
BytedanceGoPool-8      82.2MB ± 2%
ItogamiPool-8          25.6MB ± 2%

name                   allocs/op
UnlimitedGoroutines-8   2.00M ± 0%
ErrGroup-8              3.00M ± 0%
AntsPool-8              1.10M ± 2%
GammaZeroPool-8         1.08M ± 0%
BytedanceGoPool-8       2.59M ± 1%
ItogamiPool-8           1.08M ± 0%

The following conclusions can be drawn from the above results:-

  1. Itogami is the fastest among all goroutine pool implementations and slightly slower than unlimited goroutines
  2. Itogami has the least allocs/op and hence the memory usage scales really well with high load
  3. The memory used per operation is in the acceptable range of other pools and drastically lower than unlimited goroutines
  4. The tolerance (± %) for Itogami is quite low for all 3 metrics indicating that the algorithm is quite stable overall

Benchmarking code available here

itogami's People

Contributors

alphadose avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

itogami's Issues

About fatal error when triggering gc

hello, I found a number of errors, which were seems concentrated in the gc of go runtime.

The following is a summary of the error messages:

1 =>
runtime: gp: gp=0xc00041e820, goid=347, gp->atomicstatus=1
runtime:  g:  g=0x1392220, goid=0,  g->atomicstatus=0
fatal error: bad g->status in ready

2 =>
fatal error: casgstatus: waiting for Gwaiting but is Grunnable

When I solved the above problems, the next fatal error came:

fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x330 pc=0x1038847]

The error stack is too long, so complete information is not provided here🤔

BTW, The tested go version is 1.19.2 and 1.19.3

Here is my test code:

package itogami_test

import (
	"github.com/alphadose/itogami"
	"runtime/debug"
	"sync"
	"testing"
)

func doing() {
	times := 1000
	size := 100
	task := func(i int) {
		_ = i + 1
	}

	pool := itogami.NewPoolWithFunc(uint64(size), task)

	var wg sync.WaitGroup
	wg.Add(size)

	for i := 0; i < size; i++ {
		go func() {
			for n := 0; n < times; n++ {
				pool.Invoke(n)
			}
			wg.Done()
		}()
	}
	wg.Wait()
}

func TestTriggerGC(t *testing.T) {
	debug.SetMemoryLimit((1 << 20) * 100) // 100m

	for i := 0; i < 100; i++ {
		doing()
	}
}

Hope to get your help, thank you!

About the version format of tag

hi, I see that you have two tags, 0.1.0 and 0.2.0 respectively, but this seems to be inconsistent with the version number specification of go modules. I used go get github.com/alphadose/[email protected], pointing to v0.0.0-20220617064735-3fb829d9078f. The implication is whether the prefix v is missing, such as v0.2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.