From 48424caf7a65ef1c69bd9d3327fc3d381eeda5c8 Mon Sep 17 00:00:00 2001 From: karmacoma Date: Thu, 16 Jan 2025 11:16:12 -0800 Subject: [PATCH] avoid copying returndata when possible --- src/halmos/sevm.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/halmos/sevm.py b/src/halmos/sevm.py index eeedd50c..d7e2d3f2 100644 --- a/src/halmos/sevm.py +++ b/src/halmos/sevm.py @@ -2172,10 +2172,17 @@ def callback(new_ex: Exec, stack, step_id): new_ex.jumpis = deepcopy(ex.jumpis) # copy return data to memory - effective_ret_size = min(ret_size, new_ex.returndatasize()) + actual_ret_size = new_ex.returndatasize() + effective_ret_size = min(ret_size, actual_ret_size) if effective_ret_size > 0: - returndata_slice = subcall.output.data.slice(0, effective_ret_size) - new_ex.st.set_mslice(ret_loc, returndata_slice) + # fast path: if the requested ret size is the actual size of the return data, + # we can skip the slice (copy) operation and directly write the return data to memory + ret_data = ( + subcall.output.data.slice(0, effective_ret_size) + if effective_ret_size < actual_ret_size + else subcall.output.data + ) + new_ex.st.set_mslice(ret_loc, ret_data) # set status code on the stack subcall_success = subcall.output.error is None