Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: don’t unnecessarily copy promise indices #8175

Merged
merged 4 commits into from
Dec 9, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions runtime/near-vm-logic/src/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,6 @@ impl<'a> VMLogic<'a> {
memory_get!(u16, memory_get_u16);
memory_get!(u8, memory_get_u8);

/// Reads an array of `u64` elements.
fn memory_get_vec_u64(&mut self, offset: u64, num_elements: u64) -> Result<Vec<u64>> {
let memory_len = num_elements
.checked_mul(size_of::<u64>() as u64)
.ok_or(HostError::MemoryAccessViolation)?;
let data = self.memory_get_vec(offset, memory_len)?;
let mut res = vec![0u64; num_elements as usize];
byteorder::LittleEndian::read_u64_into(&data, &mut res);
Ok(res)
}

fn get_vec_from_memory_or_register(&mut self, offset: u64, len: u64) -> Result<Vec<u8>> {
if len != u64::MAX {
self.memory_get_vec(offset, len)
Expand Down Expand Up @@ -1367,28 +1356,32 @@ impl<'a> VMLogic<'a> {
promise_idx_ptr: u64,
promise_idx_count: u64,
) -> Result<PromiseIndex> {
let memory_len = promise_idx_count
.checked_mul(size_of::<u64>() as u64)
.ok_or(HostError::IntegerOverflow)?;
mina86 marked this conversation as resolved.
Show resolved Hide resolved

self.gas_counter.pay_base(base)?;
if self.context.is_view() {
return Err(
HostError::ProhibitedInView { method_name: "promise_and".to_string() }.into()
);
}
self.gas_counter.pay_base(promise_and_base)?;
self.gas_counter.pay_per(
promise_and_per_promise,
promise_idx_count
.checked_mul(size_of::<u64>() as u64)
.ok_or(HostError::IntegerOverflow)?,
)?;
self.gas_counter.pay_per(promise_and_per_promise, memory_len)?;

let promise_indices = self.memory_get_vec_u64(promise_idx_ptr, promise_idx_count)?;
// Read indices as little endian u64.
let promise_indices = self.memory_get_vec(promise_idx_ptr, memory_len)?;
let promise_indices = stdx::as_chunks_exact::<{ size_of::<u64>() }, u8>(&promise_indices)
.unwrap()
.into_iter()
.map(|bytes| u64::from_le_bytes(*bytes));

let mut receipt_dependencies = vec![];
for promise_idx in &promise_indices {
for promise_idx in promise_indices {
let promise = self
.promises
.get(*promise_idx as usize)
.ok_or(HostError::InvalidPromiseIndex { promise_idx: *promise_idx })?;
.get(promise_idx as usize)
.ok_or(HostError::InvalidPromiseIndex { promise_idx })?;
match &promise {
Promise::Receipt(receipt_idx) => {
receipt_dependencies.push(*receipt_idx);
Expand Down