-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[move-stdlib] Add vector::move_range native function (#14863)
## Description Memcopy (i.e. `ptr::copy_nonoverlapping` inside of `Vec`) is extremely efficient, and using Vec operations that use it directly is significantly faster (orders of magnitude on bigger vectors) than issuing operations in move. Operations on `vector` that can be speed-up: `insert`, `remove`, `append`, `split_off`. To keep amount of native functions short, instead of having native for each of those, providing one more general native function: `vector::move_range`, which is enough to support all 4 of the above, in addition to other uses. Internally, we shortcircuit a few special cases, for faster speed. ## How Has This Been Tested? Full performance evaluation is in the follow-up PR: #14862 ## Type of Change - [x] Performance improvement ## Which Components or Systems Does This Change Impact? - [x] Move/Aptos Virtual Machine
- Loading branch information
1 parent
6667ac6
commit 61e6563
Showing
13 changed files
with
289 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright © Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Copyright (c) The Diem Core Contributors | ||
// Copyright (c) The Move Contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Implementation of native functions (non-bytecode instructions) for vector. | ||
use aptos_gas_schedule::gas_params::natives::move_stdlib::{ | ||
VECTOR_MOVE_RANGE_BASE, VECTOR_MOVE_RANGE_PER_INDEX_MOVED, | ||
}; | ||
use aptos_native_interface::{ | ||
safely_pop_arg, RawSafeNative, SafeNativeBuilder, SafeNativeContext, SafeNativeError, | ||
SafeNativeResult, | ||
}; | ||
use aptos_types::error; | ||
use move_core_types::gas_algebra::NumArgs; | ||
use move_vm_runtime::native_functions::NativeFunction; | ||
use move_vm_types::{ | ||
loaded_data::runtime_types::Type, | ||
values::{Value, VectorRef}, | ||
}; | ||
use smallvec::{smallvec, SmallVec}; | ||
use std::collections::VecDeque; | ||
|
||
/// Given input positions/lengths are outside of vector boundaries. | ||
pub const EINDEX_OUT_OF_BOUNDS: u64 = 1; | ||
|
||
/// The feature is not enabled. | ||
pub const EFEATURE_NOT_ENABLED: u64 = 2; | ||
|
||
/*************************************************************************************************** | ||
* native fun move_range<T>(from: &mut vector<T>, removal_position: u64, length: u64, to: &mut vector<T>, insert_position: u64) | ||
* | ||
* gas cost: VECTOR_MOVE_RANGE_BASE + VECTOR_MOVE_RANGE_PER_INDEX_MOVED * num_elements_to_move | ||
* | ||
**************************************************************************************************/ | ||
fn native_move_range( | ||
context: &mut SafeNativeContext, | ||
ty_args: Vec<Type>, | ||
mut args: VecDeque<Value>, | ||
) -> SafeNativeResult<SmallVec<[Value; 1]>> { | ||
if !context | ||
.get_feature_flags() | ||
.is_native_memory_operations_enabled() | ||
{ | ||
return Err(SafeNativeError::Abort { | ||
abort_code: error::unavailable(EFEATURE_NOT_ENABLED), | ||
}); | ||
} | ||
|
||
context.charge(VECTOR_MOVE_RANGE_BASE)?; | ||
|
||
let map_err = |_| SafeNativeError::Abort { | ||
abort_code: error::invalid_argument(EINDEX_OUT_OF_BOUNDS), | ||
}; | ||
let insert_position = usize::try_from(safely_pop_arg!(args, u64)).map_err(map_err)?; | ||
let to = safely_pop_arg!(args, VectorRef); | ||
let length = usize::try_from(safely_pop_arg!(args, u64)).map_err(map_err)?; | ||
let removal_position = usize::try_from(safely_pop_arg!(args, u64)).map_err(map_err)?; | ||
let from = safely_pop_arg!(args, VectorRef); | ||
|
||
// We need to charge before executing, so fetching and checking sizes here. | ||
// We repeat fetching and checking of the sizes inside VectorRef::move_range call as well. | ||
// Not sure if possible to combine (as we are never doing charging there). | ||
let to_len = to.length_as_usize(&ty_args[0])?; | ||
let from_len = from.length_as_usize(&ty_args[0])?; | ||
|
||
if removal_position | ||
.checked_add(length) | ||
.map_or(true, |end| end > from_len) | ||
|| insert_position > to_len | ||
{ | ||
return Err(SafeNativeError::Abort { | ||
abort_code: EINDEX_OUT_OF_BOUNDS, | ||
}); | ||
} | ||
|
||
// We are moving all elements in the range, all elements after range, and all elements after insertion point. | ||
// We are counting "length" of moving block twice, as it both gets moved out and moved in. | ||
// From calibration testing, this seems to be a reasonable approximation of the cost of the operation. | ||
context.charge( | ||
VECTOR_MOVE_RANGE_PER_INDEX_MOVED | ||
* NumArgs::new( | ||
(from_len - removal_position) | ||
.checked_add(to_len - insert_position) | ||
.and_then(|v| v.checked_add(length)) | ||
.ok_or_else(|| SafeNativeError::Abort { | ||
abort_code: EINDEX_OUT_OF_BOUNDS, | ||
})? as u64, | ||
), | ||
)?; | ||
|
||
VectorRef::move_range( | ||
&from, | ||
removal_position, | ||
length, | ||
&to, | ||
insert_position, | ||
&ty_args[0], | ||
)?; | ||
|
||
Ok(smallvec![]) | ||
} | ||
|
||
/*************************************************************************************************** | ||
* module | ||
**************************************************************************************************/ | ||
pub fn make_all( | ||
builder: &SafeNativeBuilder, | ||
) -> impl Iterator<Item = (String, NativeFunction)> + '_ { | ||
let natives = [("move_range", native_move_range as RawSafeNative)]; | ||
|
||
builder.make_named_natives(natives) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.