From 43f3d06988c1c025feb4f88a949ad96e6dc829e3 Mon Sep 17 00:00:00 2001 From: Marco Peereboom Date: Wed, 8 Apr 2015 16:56:35 -0500 Subject: [PATCH] Iterate over pointers instead of copies. Modify addressable to become a pointer because Go's range does not provide a pointer to the element but a copy of the element (urgh). Go's range operator really can be considered broken. In this case it copies the addressable all the time which significantly slows things down. To boot, it isn't writing in the memory you think it is (it ends up in the copy of memory). Before the change: $ go test -v -test.run Klaus === RUN TestKlausDormann6502 Running Klaus Dormann' 6502 functional tests. This may take some time... Klaus Dormann's 6502 functional tests passed. --- PASS: TestKlausDormann6502 (13.51s) PASS ok github.com/ariejan/i6502 13.522s After the change: $ go test -v -test.run Klaus === RUN TestKlausDormann6502 Running Klaus Dormann' 6502 functional tests. This may take some time... Klaus Dormann's 6502 functional tests passed. --- PASS: TestKlausDormann6502 (4.87s) PASS ok github.com/ariejan/i6502 4.877s --- address_bus.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/address_bus.go b/address_bus.go index 01b632f..40bb942 100644 --- a/address_bus.go +++ b/address_bus.go @@ -11,7 +11,7 @@ address space of the Cpu to the relative memory addressing of each component. */ type AddressBus struct { - addressables []addressable // Different components + addressables []*addressable // Different components } type addressable struct { @@ -26,7 +26,7 @@ func (a *addressable) String() string { // Creates a new, empty 16-bit AddressBus func NewAddressBus() (*AddressBus, error) { - return &AddressBus{addressables: make([]addressable, 0)}, nil + return &AddressBus{addressables: make([]*addressable, 0)}, nil } // Returns a string with details about the AddressBus and attached memory @@ -54,7 +54,7 @@ func (a *AddressBus) Attach(memory Memory, offset uint16) { end := offset + memory.Size() - 1 addressable := addressable{memory: memory, start: start, end: end} - a.addressables = append(a.addressables, addressable) + a.addressables = append(a.addressables, &addressable) } /* @@ -112,7 +112,7 @@ func (a *AddressBus) Write16(address uint16, data uint16) { func (a *AddressBus) addressableForAddress(address uint16) (*addressable, error) { for _, addressable := range a.addressables { if addressable.start <= address && addressable.end >= address { - return &addressable, nil + return addressable, nil } }