Skip to content

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

@elwinar

Description

@elwinar

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions