Skip to content

Commit 490624e

Browse files
committed
Reduce memory usage of tests
- eth/vm/memory.py:Memory.read() used to return a bytes() initialized with the requested memory, this created an often unnecessary copy of the data. It now returns a memoryview(), which is a python wrapper around a pointer to the raw data.
1 parent 751c856 commit 490624e

File tree

13 files changed

+49
-26
lines changed

13 files changed

+49
-26
lines changed

eth/precompiles/ecadd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def ecadd(computation: BaseComputation) -> BaseComputation:
3232
computation.consume_gas(constants.GAS_ECADD, reason='ECADD Precompile')
3333

3434
try:
35-
result = _ecadd(computation.msg.data)
35+
result = _ecadd(bytes(computation.msg.data))
3636
except ValidationError:
3737
raise VMError("Invalid ECADD parameters")
3838

eth/precompiles/ecmul.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def ecmul(computation: BaseComputation) -> BaseComputation:
3232
computation.consume_gas(constants.GAS_ECMUL, reason='ECMUL Precompile')
3333

3434
try:
35-
result = _ecmull(computation.msg.data)
35+
result = _ecmull(bytes(computation.msg.data))
3636
except ValidationError:
3737
raise VMError("Invalid ECMUL parameters")
3838

eth/precompiles/ecpairing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Tuple
1+
from typing import Tuple, Union
22

33
from cytoolz import (
44
curry,
@@ -60,7 +60,7 @@ def ecpairing(computation: BaseComputation) -> BaseComputation:
6060
return computation
6161

6262

63-
def _ecpairing(data: bytes) -> bool:
63+
def _ecpairing(data: Union[bytes, memoryview]) -> bool:
6464
exponent = bn128.FQ12.one()
6565

6666
processing_pipeline = (

eth/precompiles/ecrecover.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@
2727

2828
def ecrecover(computation: BaseComputation) -> BaseComputation:
2929
computation.consume_gas(constants.GAS_ECRECOVER, reason="ECRecover Precompile")
30-
raw_message_hash = computation.msg.data[:32]
30+
data = bytes(computation.msg.data)
31+
raw_message_hash = data[:32]
3132
message_hash = pad32r(raw_message_hash)
3233

33-
v_bytes = pad32r(computation.msg.data[32:64])
34+
v_bytes = pad32r(data[32:64])
3435
v = big_endian_to_int(v_bytes)
3536

36-
r_bytes = pad32r(computation.msg.data[64:96])
37+
r_bytes = pad32r(data[64:96])
3738
r = big_endian_to_int(r_bytes)
3839

39-
s_bytes = pad32r(computation.msg.data[96:128])
40+
s_bytes = pad32r(data[96:128])
4041
s = big_endian_to_int(s_bytes)
4142

4243
try:

eth/precompiles/identity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ def identity(computation: BaseComputation) -> BaseComputation:
1414

1515
computation.consume_gas(gas_fee, reason="Identity Precompile")
1616

17-
computation.output = computation.msg.data
17+
computation.output = bytes(computation.msg.data)
1818
return computation

eth/precompiles/modexp.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import (
2-
Tuple,
2+
Tuple
33
)
44

55
from eth_utils import (
@@ -125,12 +125,14 @@ def modexp(computation: BaseComputation) -> BaseComputation:
125125
"""
126126
https://github.com/ethereum/EIPs/pull/198
127127
"""
128-
gas_fee = _compute_modexp_gas_fee(computation.msg.data)
128+
data = bytes(computation.msg.data)
129+
130+
gas_fee = _compute_modexp_gas_fee(data)
129131
computation.consume_gas(gas_fee, reason='MODEXP Precompile')
130132

131-
result = _modexp(computation.msg.data)
133+
result = _modexp(data)
132134

133-
_, _, modulus_length = _extract_lengths(computation.msg.data)
135+
_, _, modulus_length = _extract_lengths(data)
134136

135137
# Modulo 0 is undefined, return zero
136138
# https://math.stackexchange.com/questions/516251/why-is-n-mod-0-undefined

eth/vm/computation.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def memory_write(self, start_position: int, size: int, value: bytes) -> None:
243243
"""
244244
return self._memory.write(start_position, size, value)
245245

246-
def memory_read(self, start_position: int, size: int) -> bytes:
246+
def memory_read(self, start_position: int, size: int) -> memoryview:
247247
"""
248248
Read and return ``size`` bytes from memory starting at ``start_position``.
249249
"""
@@ -360,7 +360,7 @@ def prepare_child_message(self,
360360
gas: int,
361361
to: Address,
362362
value: int,
363-
data: bytes,
363+
data: Union[bytes, memoryview],
364364
code: bytes,
365365
**kwargs: Any) -> Message:
366366
"""
@@ -442,7 +442,12 @@ def get_accounts_for_deletion(self) -> Tuple[Tuple[bytes, bytes], ...]:
442442
#
443443
# EVM logging
444444
#
445-
def add_log_entry(self, account: Address, topics: List[int], data: bytes) -> None:
445+
def add_log_entry(
446+
self,
447+
account: Address,
448+
topics: List[int],
449+
data: Union[bytes, memoryview]) -> None:
450+
data = bytes(data)
446451
validate_canonical_address(account, title="Log entry address")
447452
for topic in topics:
448453
validate_uint256(topic, title="Log entry topic")

eth/vm/logic/context.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def calldataload(computation: BaseComputation) -> None:
4242
"""
4343
start_position = computation.stack_pop(type_hint=constants.UINT256)
4444

45-
value = computation.msg.data[start_position:start_position + 32]
45+
value = bytes(computation.msg.data[start_position:start_position + 32])
4646
padded_value = value.ljust(32, b'\x00')
4747
normalized_value = padded_value.lstrip(b'\x00')
4848

@@ -68,7 +68,9 @@ def calldatacopy(computation: BaseComputation) -> None:
6868

6969
computation.consume_gas(copy_gas_cost, reason="CALLDATACOPY fee")
7070

71-
value = computation.msg.data[calldata_start_position: calldata_start_position + size]
71+
value = bytes(
72+
computation.msg.data[calldata_start_position: calldata_start_position + size]
73+
)
7274
padded_value = value.ljust(size, b'\x00')
7375

7476
computation.memory_write(mem_start_position, size, padded_value)

eth/vm/logic/memory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def mload(computation: BaseComputation) -> None:
3333
computation.extend_memory(start_position, 32)
3434

3535
value = computation.memory_read(start_position, 32)
36-
computation.stack_push(value)
36+
computation.stack_push(bytes(value))
3737

3838

3939
def msize(computation: BaseComputation) -> None:

eth/vm/logic/sha3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ def sha3(computation: BaseComputation) -> None:
1818
gas_cost = constants.GAS_SHA3WORD * word_count
1919
computation.consume_gas(gas_cost, reason="SHA3: word gas cost")
2020

21-
result = keccak(sha3_bytes)
21+
result = keccak(bytes(sha3_bytes))
2222

2323
computation.stack_push(result)

0 commit comments

Comments
 (0)