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.
This commit is contained in:
@ -127,9 +127,15 @@ func (p *IntcodeProgram) ensureMemoryCapacity(address int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *IntcodeProgram) Reset() {
|
func (p *IntcodeProgram) Reset() {
|
||||||
p.memory = nil
|
wiped := false
|
||||||
|
if len(p.memory) != len(p.program) {
|
||||||
|
wiped = true
|
||||||
|
p.memory = nil
|
||||||
|
}
|
||||||
p.init()
|
p.init()
|
||||||
copy(p.memory, p.program)
|
if !wiped {
|
||||||
|
copy(p.memory, p.program)
|
||||||
|
}
|
||||||
p.relativeBase = 0
|
p.relativeBase = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user