-
Notifications
You must be signed in to change notification settings - Fork 131
Optimize serialization of function calls #444
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
Draft
ludfjig
wants to merge
4
commits into
hyperlight-dev:main
Choose a base branch
from
ludfjig:avoid_vec_allocation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b14634a
Serialize FunctionCall into &[u8] instead of Vec<u8>, avoiding unnece…
ludfjig 56bf4a2
Remove need for buffer validation by slightly modifying function sign…
ludfjig 24586f5
Remove direct dependency on flatbuffers in hyperlight-host and hyperl…
ludfjig 9d05679
Add benchmark for guest call with large parameters
ludfjig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 |
---|---|---|
|
@@ -18,7 +18,7 @@ use alloc::string::{String, ToString}; | |
use alloc::vec::Vec; | ||
|
||
use anyhow::{bail, Error, Result}; | ||
use flatbuffers::{size_prefixed_root, WIPOffset}; | ||
use flatbuffers::{size_prefixed_root, FlatBufferBuilder, WIPOffset}; | ||
#[cfg(feature = "tracing")] | ||
use tracing::{instrument, Span}; | ||
|
||
|
@@ -41,7 +41,6 @@ pub enum FunctionCallType { | |
} | ||
|
||
/// `Functioncall` represents a call to a function in the guest or host. | ||
#[derive(Clone)] | ||
pub struct FunctionCall { | ||
/// The function name | ||
pub function_name: String, | ||
|
@@ -72,91 +71,29 @@ impl FunctionCall { | |
pub fn function_call_type(&self) -> FunctionCallType { | ||
self.function_call_type.clone() | ||
} | ||
} | ||
|
||
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))] | ||
pub fn validate_guest_function_call_buffer(function_call_buffer: &[u8]) -> Result<()> { | ||
let guest_function_call_fb = size_prefixed_root::<FbFunctionCall>(function_call_buffer) | ||
.map_err(|e| anyhow::anyhow!("Error reading function call buffer: {:?}", e))?; | ||
match guest_function_call_fb.function_call_type() { | ||
FbFunctionCallType::guest => Ok(()), | ||
other => { | ||
bail!("Invalid function call type: {:?}", other); | ||
} | ||
} | ||
} | ||
|
||
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))] | ||
pub fn validate_host_function_call_buffer(function_call_buffer: &[u8]) -> Result<()> { | ||
let host_function_call_fb = size_prefixed_root::<FbFunctionCall>(function_call_buffer) | ||
.map_err(|e| anyhow::anyhow!("Error reading function call buffer: {:?}", e))?; | ||
match host_function_call_fb.function_call_type() { | ||
FbFunctionCallType::host => Ok(()), | ||
other => { | ||
bail!("Invalid function call type: {:?}", other); | ||
} | ||
} | ||
} | ||
/// Encodes self into the given builder and returns the encoded data. | ||
pub fn encode<'a>(&self, builder: &'a mut FlatBufferBuilder) -> &'a [u8] { | ||
let function_name = builder.create_string(&self.function_name); | ||
|
||
impl TryFrom<&[u8]> for FunctionCall { | ||
type Error = Error; | ||
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))] | ||
fn try_from(value: &[u8]) -> Result<Self> { | ||
let function_call_fb = size_prefixed_root::<FbFunctionCall>(value) | ||
.map_err(|e| anyhow::anyhow!("Error reading function call buffer: {:?}", e))?; | ||
let function_name = function_call_fb.function_name(); | ||
let function_call_type = match function_call_fb.function_call_type() { | ||
FbFunctionCallType::guest => FunctionCallType::Guest, | ||
FbFunctionCallType::host => FunctionCallType::Host, | ||
other => { | ||
bail!("Invalid function call type: {:?}", other); | ||
} | ||
}; | ||
let expected_return_type = function_call_fb.expected_return_type().try_into()?; | ||
|
||
let parameters = function_call_fb | ||
.parameters() | ||
.map(|v| { | ||
v.iter() | ||
.map(|p| p.try_into()) | ||
.collect::<Result<Vec<ParameterValue>>>() | ||
}) | ||
.transpose()?; | ||
|
||
Ok(Self { | ||
function_name: function_name.to_string(), | ||
parameters, | ||
function_call_type, | ||
expected_return_type, | ||
}) | ||
} | ||
} | ||
|
||
impl TryFrom<FunctionCall> for Vec<u8> { | ||
type Error = Error; | ||
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))] | ||
fn try_from(value: FunctionCall) -> Result<Vec<u8>> { | ||
let mut builder = flatbuffers::FlatBufferBuilder::new(); | ||
let function_name = builder.create_string(&value.function_name); | ||
|
||
let function_call_type = match value.function_call_type { | ||
let function_call_type = match self.function_call_type { | ||
FunctionCallType::Guest => FbFunctionCallType::guest, | ||
FunctionCallType::Host => FbFunctionCallType::host, | ||
}; | ||
|
||
let expected_return_type = value.expected_return_type.into(); | ||
let expected_return_type = self.expected_return_type.into(); | ||
|
||
let parameters = match &value.parameters { | ||
let parameters = match &self.parameters { | ||
Some(p) => { | ||
let num_items = p.len(); | ||
let mut parameters: Vec<WIPOffset<Parameter>> = Vec::with_capacity(num_items); | ||
|
||
for param in p { | ||
match param { | ||
ParameterValue::Int(i) => { | ||
let hlint = hlint::create(&mut builder, &hlintArgs { value: *i }); | ||
let hlint = hlint::create(builder, &hlintArgs { value: *i }); | ||
let parameter = Parameter::create( | ||
&mut builder, | ||
builder, | ||
&ParameterArgs { | ||
value_type: FbParameterValue::hlint, | ||
value: Some(hlint.as_union_value()), | ||
|
@@ -165,9 +102,9 @@ impl TryFrom<FunctionCall> for Vec<u8> { | |
parameters.push(parameter); | ||
} | ||
ParameterValue::UInt(ui) => { | ||
let hluint = hluint::create(&mut builder, &hluintArgs { value: *ui }); | ||
let hluint = hluint::create(builder, &hluintArgs { value: *ui }); | ||
let parameter = Parameter::create( | ||
&mut builder, | ||
builder, | ||
&ParameterArgs { | ||
value_type: FbParameterValue::hluint, | ||
value: Some(hluint.as_union_value()), | ||
|
@@ -176,9 +113,9 @@ impl TryFrom<FunctionCall> for Vec<u8> { | |
parameters.push(parameter); | ||
} | ||
ParameterValue::Long(l) => { | ||
let hllong = hllong::create(&mut builder, &hllongArgs { value: *l }); | ||
let hllong = hllong::create(builder, &hllongArgs { value: *l }); | ||
let parameter = Parameter::create( | ||
&mut builder, | ||
builder, | ||
&ParameterArgs { | ||
value_type: FbParameterValue::hllong, | ||
value: Some(hllong.as_union_value()), | ||
|
@@ -187,10 +124,9 @@ impl TryFrom<FunctionCall> for Vec<u8> { | |
parameters.push(parameter); | ||
} | ||
ParameterValue::ULong(ul) => { | ||
let hlulong = | ||
hlulong::create(&mut builder, &hlulongArgs { value: *ul }); | ||
let hlulong = hlulong::create(builder, &hlulongArgs { value: *ul }); | ||
let parameter = Parameter::create( | ||
&mut builder, | ||
builder, | ||
&ParameterArgs { | ||
value_type: FbParameterValue::hlulong, | ||
value: Some(hlulong.as_union_value()), | ||
|
@@ -199,9 +135,9 @@ impl TryFrom<FunctionCall> for Vec<u8> { | |
parameters.push(parameter); | ||
} | ||
ParameterValue::Float(f) => { | ||
let hlfloat = hlfloat::create(&mut builder, &hlfloatArgs { value: *f }); | ||
let hlfloat = hlfloat::create(builder, &hlfloatArgs { value: *f }); | ||
let parameter = Parameter::create( | ||
&mut builder, | ||
builder, | ||
&ParameterArgs { | ||
value_type: FbParameterValue::hlfloat, | ||
value: Some(hlfloat.as_union_value()), | ||
|
@@ -210,10 +146,9 @@ impl TryFrom<FunctionCall> for Vec<u8> { | |
parameters.push(parameter); | ||
} | ||
ParameterValue::Double(d) => { | ||
let hldouble = | ||
hldouble::create(&mut builder, &hldoubleArgs { value: *d }); | ||
let hldouble = hldouble::create(builder, &hldoubleArgs { value: *d }); | ||
let parameter = Parameter::create( | ||
&mut builder, | ||
builder, | ||
&ParameterArgs { | ||
value_type: FbParameterValue::hldouble, | ||
value: Some(hldouble.as_union_value()), | ||
|
@@ -223,9 +158,9 @@ impl TryFrom<FunctionCall> for Vec<u8> { | |
} | ||
ParameterValue::Bool(b) => { | ||
let hlbool: WIPOffset<hlbool<'_>> = | ||
hlbool::create(&mut builder, &hlboolArgs { value: *b }); | ||
hlbool::create(builder, &hlboolArgs { value: *b }); | ||
let parameter = Parameter::create( | ||
&mut builder, | ||
builder, | ||
&ParameterArgs { | ||
value_type: FbParameterValue::hlbool, | ||
value: Some(hlbool.as_union_value()), | ||
|
@@ -236,10 +171,10 @@ impl TryFrom<FunctionCall> for Vec<u8> { | |
ParameterValue::String(s) => { | ||
let hlstring = { | ||
let val = builder.create_string(s.as_str()); | ||
hlstring::create(&mut builder, &hlstringArgs { value: Some(val) }) | ||
hlstring::create(builder, &hlstringArgs { value: Some(val) }) | ||
}; | ||
let parameter = Parameter::create( | ||
&mut builder, | ||
builder, | ||
&ParameterArgs { | ||
value_type: FbParameterValue::hlstring, | ||
value: Some(hlstring.as_union_value()), | ||
|
@@ -251,13 +186,13 @@ impl TryFrom<FunctionCall> for Vec<u8> { | |
let vec_bytes = builder.create_vector(v); | ||
|
||
let hlvecbytes = hlvecbytes::create( | ||
&mut builder, | ||
builder, | ||
&hlvecbytesArgs { | ||
value: Some(vec_bytes), | ||
}, | ||
); | ||
let parameter = Parameter::create( | ||
&mut builder, | ||
builder, | ||
&ParameterArgs { | ||
value_type: FbParameterValue::hlvecbytes, | ||
value: Some(hlvecbytes.as_union_value()), | ||
|
@@ -279,7 +214,7 @@ impl TryFrom<FunctionCall> for Vec<u8> { | |
}; | ||
|
||
let function_call = FbFunctionCall::create( | ||
&mut builder, | ||
builder, | ||
&FbFunctionCallArgs { | ||
function_name: Some(function_name), | ||
parameters, | ||
|
@@ -288,9 +223,40 @@ impl TryFrom<FunctionCall> for Vec<u8> { | |
}, | ||
); | ||
builder.finish_size_prefixed(function_call, None); | ||
let res = builder.finished_data().to_vec(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
builder.finished_data() | ||
} | ||
} | ||
|
||
impl TryFrom<&[u8]> for FunctionCall { | ||
type Error = Error; | ||
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))] | ||
fn try_from(value: &[u8]) -> Result<Self> { | ||
let function_call_fb = size_prefixed_root::<FbFunctionCall>(value) | ||
.map_err(|e| anyhow::anyhow!("Error reading function call buffer: {:?}", e))?; | ||
let function_name = function_call_fb.function_name(); | ||
let function_call_type = match function_call_fb.function_call_type() { | ||
FbFunctionCallType::guest => FunctionCallType::Guest, | ||
FbFunctionCallType::host => FunctionCallType::Host, | ||
other => { | ||
bail!("Invalid function call type: {:?}", other); | ||
} | ||
}; | ||
let expected_return_type = function_call_fb.expected_return_type().try_into()?; | ||
let parameters = function_call_fb | ||
.parameters() | ||
.map(|v| { | ||
v.iter() | ||
.map(|p| p.try_into()) | ||
.collect::<Result<Vec<ParameterValue>>>() | ||
}) | ||
.transpose()?; | ||
|
||
Ok(res) | ||
Ok(Self { | ||
function_name: function_name.to_string(), | ||
parameters, | ||
function_call_type, | ||
expected_return_type, | ||
}) | ||
} | ||
} | ||
|
||
|
@@ -303,7 +269,8 @@ mod tests { | |
|
||
#[test] | ||
fn read_from_flatbuffer() -> Result<()> { | ||
let test_data: Vec<u8> = FunctionCall::new( | ||
let mut builder = FlatBufferBuilder::new(); | ||
let test_data = FunctionCall::new( | ||
"PrintTwelveArgs".to_string(), | ||
Some(vec![ | ||
ParameterValue::String("1".to_string()), | ||
|
@@ -322,10 +289,9 @@ mod tests { | |
FunctionCallType::Guest, | ||
ReturnType::Int, | ||
) | ||
.try_into() | ||
.unwrap(); | ||
.encode(&mut builder); | ||
|
||
let function_call = FunctionCall::try_from(test_data.as_slice())?; | ||
let function_call = FunctionCall::try_from(test_data)?; | ||
assert_eq!(function_call.function_name, "PrintTwelveArgs"); | ||
assert!(function_call.parameters.is_some()); | ||
let parameters = function_call.parameters.unwrap(); | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I missed it, but where was this function called?
If it wasn't used at all, I am curious how the compiler hasn't warned us.