Thread safety for status updates, periodically minify

This commit is contained in:
Ian Gulliver
2021-11-22 22:19:38 -08:00
parent 44c6a26f9d
commit 352e1b90be
6 changed files with 59 additions and 4 deletions

View File

@@ -48,7 +48,7 @@ func (def *Definition) Grow(statusChan chan<- Status) (*vm.Program, error) {
for {
status.Attempts++
Mutate(prog)
Mutate(def, prog)
score, err := def.score(prog)
if err != nil {
@@ -58,7 +58,7 @@ func (def *Definition) Grow(statusChan chan<- Status) (*vm.Program, error) {
if score > status.BestScore {
status.BestScore = score
status.BestProgram = prog
status.BestProgram = prog.Copy()
if statusChan != nil {
statusChan <- status
@@ -73,6 +73,7 @@ func (def *Definition) Grow(statusChan chan<- Status) (*vm.Program, error) {
return nil, err
}
status.BestProgram = prog.Copy()
if statusChan != nil {
statusChan <- status
@@ -142,6 +143,7 @@ func (def *Definition) minifyFunction(prog *vm.Program, f int) error {
for i := 0; i < len(prog.Functions[f].Instructions); i++ {
origInstructions := prog.Functions[f].Instructions
tmp := make([]*vm.Instruction, len(prog.Functions[f].Instructions))
copy(tmp, prog.Functions[f].Instructions)
prog.Functions[f].Instructions = append(tmp[:i], tmp[i+1:]...)