Skip to content

Commit

Permalink
always inline load/store in Intcode model
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleymackey committed Sep 8, 2020
1 parent eeed443 commit 87a4483
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Sources/AdventOfCode19/Models/Intcode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ final class Intcode {
return Instruction(code: code, parameters: parameters, instructionStartPosition: pointer)
}

func execute(_ i: Instruction) throws -> Action {
@inline(__always)
private func execute(_ i: Instruction) throws -> Action {
switch i.code {
case .add:
let total = load(i) + load(i)
Expand Down Expand Up @@ -233,20 +234,23 @@ final class Intcode {
return .continue
}

@inline(__always)
private func load(_ i: Instruction) -> Int {
defer { pointer += 1 }
let paramIndex = pointer - i.instructionStartPosition
return value(from: i.parameters[paramIndex])
}

@inline(__always)
private func store(val: Int, _ i: Instruction) {
defer { pointer += 1 }
let paramIndex = pointer - i.instructionStartPosition
let addr = address(from: i.parameters[paramIndex])
data[addr] = val
}

func value(from param: Instruction.Parameter) -> Int {
@inline(__always)
private func value(from param: Instruction.Parameter) -> Int {
switch param.mode {
case .immediate:
return param.value
Expand All @@ -255,7 +259,8 @@ final class Intcode {
}
}

func address(from param: Instruction.Parameter) -> Int {
@inline(__always)
private func address(from param: Instruction.Parameter) -> Int {
switch param.mode {
case .immediate, .position:
return param.value
Expand Down

0 comments on commit 87a4483

Please sign in to comment.