Skip to content

Commit

Permalink
Merge pull request #850 from ethereumjs/memory-as-buffer
Browse files Browse the repository at this point in the history
Make `memory.ts` use Buffers instead of Arrays
  • Loading branch information
holgerd77 committed Aug 27, 2020
2 parents bc30440 + 4aeaaed commit 7e47bb5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/vm/lib/evm/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface InterpreterStep {
pc: number
depth: number
address: Buffer
memory: number[]
memory: Buffer
memoryWordCount: BN
opcode: {
name: string
Expand Down
6 changes: 3 additions & 3 deletions packages/vm/lib/evm/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import * as assert from 'assert'
* for the ethereum virtual machine.
*/
export default class Memory {
_store: number[]
_store: Buffer

constructor() {
this._store = []
this._store = Buffer.alloc(0)
}

/**
Expand All @@ -23,7 +23,7 @@ export default class Memory {
const newSize = ceil(offset + size, 32)
const sizeDiff = newSize - this._store.length
if (sizeDiff > 0) {
this._store = this._store.concat(new Array(sizeDiff).fill(0))
this._store = Buffer.concat([this._store, Buffer.alloc(sizeDiff)])
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/vm/tests/api/evm/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tape('Memory', t => {
})

t.test('should return zeros from empty memory', st => {
st.deepEqual(m.read(0, 3), Buffer.from([0, 0, 0]))
st.ok(m.read(0, 3).equals(Buffer.from([0, 0, 0])))
st.end()
})

Expand All @@ -21,7 +21,7 @@ tape('Memory', t => {
})

t.test('should return zeros before writing', st => {
st.deepEqual(m.read(0, 2), Buffer.from([0, 0]))
st.ok(m.read(0, 2).equals(Buffer.from([0, 0])))
st.end()
})

Expand All @@ -32,7 +32,7 @@ tape('Memory', t => {

t.test('should write value', st => {
m.write(29, 3, Buffer.from([1, 2, 3]))
st.deepEqual(m.read(29, 5), Buffer.from([1, 2, 3, 0, 0]))
st.ok(m.read(29, 5).equals(Buffer.from([1, 2, 3, 0, 0])))
st.end()
})

Expand Down

1 comment on commit 7e47bb5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 7e47bb5 Previous: bc30440 Ratio
Block 9422905 1883 ops/sec (±3.94%) 2012 ops/sec (±5.41%) 1.07
Block 9422906 1904 ops/sec (±6.44%) 2088 ops/sec (±0.95%) 1.10
Block 9422907 1955 ops/sec (±1.10%) 1841 ops/sec (±6.60%) 0.94
Block 9422908 1707 ops/sec (±9.97%) 2037 ops/sec (±1.19%) 1.19
Block 9422909 1871 ops/sec (±1.07%) 1994 ops/sec (±1.46%) 1.07
Block 9422910 1863 ops/sec (±0.66%) 1950 ops/sec (±1.21%) 1.05
Block 9422911 1840 ops/sec (±1.12%) 1918 ops/sec (±1.34%) 1.04
Block 9422912 1812 ops/sec (±0.78%) 1900 ops/sec (±1.27%) 1.05
Block 9422913 1356 ops/sec (±14.22%) 1248 ops/sec (±16.10%) 0.92
Block 9422914 1701 ops/sec (±5.92%) 1904 ops/sec (±1.48%) 1.12

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.