Skip to content

Commit 555a897

Browse files
committed
#473: Remove redundant copies where possible
1 parent 3912895 commit 555a897

File tree

2 files changed

+6
-9
lines changed

2 files changed

+6
-9
lines changed

libraries/chain/host_api.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ int32_t host_api::invoke_system_call( uint32_t sid, char* ret_ptr, uint32_t ret_
8282
[&]() {
8383
if ( _ctx.system_call_exists( sid ) )
8484
{
85-
#pragma message "TODO: Brainstorm how to avoid arg/ret copy and validate pointers"
8685
std::string args( arg_ptr, arg_len );
8786
auto res = _ctx.system_call( sid, args );
8887
code = res.code;

libraries/chain/include/koinos/chain/thunk_dispatcher.hpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,11 @@ namespace detail
278278

279279
ret = std::apply( thunk, thunk_args );
280280

281-
std::string s;
282-
ret.SerializeToString( &s );
283-
KOINOS_ASSERT( s.size() <= ret_len, insufficient_return_buffer_exception, "return buffer is not large enough for the return value" );
284-
#pragma message "TODO: We should avoid making copies where possible (Issue #473)"
285-
std::memcpy( ret_ptr, s.c_str(), s.size() );
286-
*bytes_written = uint32_t( s.size() );
281+
std::size_t byte_size = ret.ByteSizeLong();
282+
KOINOS_ASSERT( byte_size <= ret_len, insufficient_return_buffer_exception, "return buffer is not large enough for the return value" );
283+
284+
ret.SerializeToArray( ret_ptr, uint32_t( byte_size ) );
285+
*bytes_written = uint32_t( byte_size );
287286
}
288287

289288
} // detail
@@ -325,8 +324,7 @@ class thunk_dispatcher
325324
{
326325
ArgStruct args;
327326
ctx.resource_meter().use_compute_bandwidth( ctx.get_compute_bandwidth( "deserialize_message_per_byte" ) * arg_len );
328-
std::string s( arg_ptr, arg_len );
329-
args.ParseFromString( s );
327+
args.ParseFromArray( arg_ptr, arg_len );
330328
detail::call_thunk_impl< ArgStruct, RetStruct >( thunk, ctx, ret_ptr, ret_len, args, bytes_written );
331329
});
332330
_pass_through_map.insert_or_assign( id, thunk );

0 commit comments

Comments
 (0)