Skip to content

Can't use a sync.Pool with eaopt.GA #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
elwinar opened this issue Apr 19, 2025 · 0 comments
Open

Can't use a sync.Pool with eaopt.GA #59

elwinar opened this issue Apr 19, 2025 · 0 comments

Comments

@elwinar
Copy link

elwinar commented Apr 19, 2025

I've got code that use eaopt.GA to generate the solution for a problem, and the quality of the result is ok.

My Genome struct is a wrapper around a pair of (potentially) huge slices, and this is creating an important pressure on the GC, which has a lot to do to manage them. It's taking more than 60% of the execution time of my program dixit pprof, which is probably optimizable.

One way to optimize would be using a sync.Pool to re-use the struct, like this (many parts omitted for simplicity's sake):

func main() {
	// initializaton code

	err = ga.Minimize(func(rng *rand.Rand) eaopt.Genome {
		return pool.Get().(*Genome)
	})

	// exploitation code
}

type Genome struct {
	set []bool
	refs []uint8
}

pool := sync.Pool{
	New: func() any {
		return &Genome{
			set:  make([]bool, len(x)),
			refs: make([]uint8, len(y)),
		}
	},
}

func (ge Genome) Clone() eaopt.Genome {
	c := pool.Get().(*Genome)
	copy(c.set, ge.set)
	copy(c.refs, ge.refs)
	return c
}

The issue is that there isn't anything that allow me to put the Genome back into the pool when it's not used anymore.

I would suggest adding a Release method to the eaopt.Genome interface that would allow releasing resources, like puting the struct back into the pool or anything else.

Any advice on this would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant