Skip to content

Commit f459391

Browse files
committed
Optimization complete!
I no longer create new objects for ops, modes, or values. Instead I always update the same objects (modes / values are arrays) in place, which helps a lot! Looks like around a 3x speed improvement. Modes is a single array which is possible since we know the number of params. We loop through params which means we don't hit the end of the modes if the op doesn't need it. Values needed to be separate arrays on each op since we spread this out into the function call. Not a big deal since we create these arrays in the constructor.
1 parent de3b9e7 commit f459391

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

2019/19/intcode-computer.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,13 @@ class Computer {
141141
},
142142
};
143143

144-
this.maxParams = Math.max(...Object.values(this.OPS).map(v => v.params));
145-
this.sharedModes = Array(this.maxParams).fill('0');
144+
const ops_list = Object.values(this.OPS);
145+
const max_params = Math.max(...ops_list.map(v => v.params));
146+
const shared_modes = Array(max_params).fill('0');
147+
for (let op of ops_list) {
148+
op.modes = shared_modes;
149+
op.values = Array(op.params).fill(0);
150+
}
146151

147152
this.halted = false;
148153
}
@@ -176,8 +181,6 @@ class Computer {
176181

177182
let full_op = temp_op.padStart(op.params + 2, '0');
178183

179-
let modes = this.sharedModes;
180-
181184
// "Parameter modes are single digits, one per parameter, read **right-to-left** from the opcode"
182185
for (let i = op.params - 1; i >= 0; i--) {
183186
// [0,1,2,3,4,5]
@@ -188,18 +191,16 @@ class Computer {
188191
// 2 -> 3 # |(2 - 6 + 1)| = |-3| = 3
189192
// 1 -> 4 # |(1 - 6 + 1)| = |-4| = 4
190193
// 0 -> 5 # |(0 - 6 + 1)| = |-5| = 5
191-
modes[Math.abs(i - op.params + 1)] = full_op[i];
194+
op.modes[Math.abs(i - op.params + 1)] = full_op[i];
192195
}
193196

194-
op.modes = modes;
195197
let end = new Date();
196198
this.parseOpTime += (end - start);
197199
return op;
198200
}
199201

200-
runOp({ modes, params, fn, jumps, write }) {
202+
runOp({ modes, values, params, fn, jumps, write }) {
201203
this.pointer++;
202-
let values = [];
203204
for (let i = 0; i < params; i++) {
204205
let mode = modes[i];
205206
let value = this.memory[this.pointer + i];
@@ -294,7 +295,7 @@ class Computer {
294295
value = 0;
295296
}
296297

297-
values.push(value);
298+
values[i] = value;
298299
}
299300

300301
// If result is `true`, we moved the pointer

2019/19/part-two.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ const { input } = require('./input');
22
const { TractorBeam } = require('./tractor-beam');
33

44
let tractor_beam = new TractorBeam(input);
5-
// console.log(tractor_beam.partTwo());
6-
console.log(tractor_beam.partTwoOptimized());
5+
console.log(tractor_beam.partTwo());
6+
// console.log(tractor_beam.partTwoOptimized());
77
console.log(`parseOp took ${tractor_beam.parseOpTime} ms`);

0 commit comments

Comments
 (0)