From ca6a16cedddc8970bcd63020988a9fe1b1fe1ea5 Mon Sep 17 00:00:00 2001 From: Parnic Date: Tue, 21 Jun 2022 12:20:37 -0500 Subject: [PATCH] Intcode Reset optimization Initially I noticed that I was copying twice unnecessarily (once in init() after nulling out memory, and again after returning from init()). After cleaning that up, I realized that we don't need to create a new buffer at all if the program never malloc-ed, so sometimes we can skip the re-init and we can always avoid the double-copy. I don't know if this is actually measurable anywhere, but I spot-checked some results and I still seem to be getting the same answers, so I'm gonna roll with it. --- utilities/intcode.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/utilities/intcode.go b/utilities/intcode.go index efdeb44..04008e1 100644 --- a/utilities/intcode.go +++ b/utilities/intcode.go @@ -127,9 +127,15 @@ func (p *IntcodeProgram) ensureMemoryCapacity(address int) { } func (p *IntcodeProgram) Reset() { - p.memory = nil + wiped := false + if len(p.memory) != len(p.program) { + wiped = true + p.memory = nil + } p.init() - copy(p.memory, p.program) + if !wiped { + copy(p.memory, p.program) + } p.relativeBase = 0 }