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

refactor: Use span instead of vectors in ExternalFunction #668

Merged
merged 3 commits into from
Jan 4, 2021

Conversation

gumb0
Copy link
Collaborator

@gumb0 gumb0 commented Nov 26, 2020

This allows to avoid extra vector allocation when going from C FizzyExternalFunction to C++ fizzy::ExternalFunction and also when C++ API returns from find_exported_function().

Also I it helps with std::function refactoring in #643

The downside is that user has to ensure that arrays pointed to by spans live long enough (usually these arrays are in Instance, and you need this Instance to run ExternalFunction anyway).

This leaves ImportedFunction still using vector<ValType> for input types, I think it's convenient to leave it like this, otherwise it wouldn't be possible to pass input types as inline initializer list like this:

        auto imports = fizzy::resolve_imported_functions(
            *module, {
                         {"env", "adler32", {fizzy::ValType::i32, fizzy::ValType::i32},
                             fizzy::ValType::i32, env_adler32},
                     });

neither like this

    std::vector<ImportedFunction> imported_functions = {
        {"mod1", "foo1", {}, ValType::i32, function_returning_value(0)},
        {"mod1", "foo2", {ValType::i32}, ValType::i32, function_returning_value(1)},
        {"mod2", "foo1", {ValType::i32}, ValType::i32, function_returning_value(2)},
        {"mod2", "foo2", {ValType::i64, ValType::i32}, std::nullopt, function_returning_void},
    };

(Spans have to point to some container, so explicit vector declarations would be required on the user side)

Also note that to make above use cases work, resolve_imported_functions() produces ExternalFunctions with type spans pointing to the type vector inside Module, not to the passed vector. Not sure whether it's confusing from the API standpoint, but it works.

@codecov
Copy link

codecov bot commented Nov 26, 2020

Codecov Report

Merging #668 (1171838) into master (2ee7be6) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #668   +/-   ##
=======================================
  Coverage   99.28%   99.28%           
=======================================
  Files          72       72           
  Lines        9918     9954   +36     
=======================================
+ Hits         9847     9883   +36     
  Misses         71       71           
Flag Coverage Δ
spectests 91.50% <100.00%> (+0.02%) ⬆️
unittests 99.28% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
lib/fizzy/capi.cpp 98.64% <100.00%> (+0.01%) ⬆️
lib/fizzy/cxx20/span.hpp 100.00% <100.00%> (ø)
lib/fizzy/instantiate.cpp 100.00% <100.00%> (ø)
lib/fizzy/instantiate.hpp 100.00% <100.00%> (ø)
test/unittests/api_test.cpp 100.00% <100.00%> (ø)
test/unittests/cxx20_span_test.cpp 100.00% <100.00%> (ø)
test/unittests/instantiate_test.cpp 100.00% <100.00%> (ø)

@gumb0 gumb0 force-pushed the external-function-type-span branch 9 times, most recently from fc6bff3 to 38844d9 Compare December 7, 2020 16:55
@gumb0 gumb0 marked this pull request as ready for review December 7, 2020 16:56
@gumb0 gumb0 force-pushed the external-function-type-span branch from 38844d9 to 27fb67c Compare December 7, 2020 18:42
@gumb0 gumb0 requested review from chfast and axic December 8, 2020 11:08
Copy link
Collaborator

@chfast chfast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok, but this even more complicates the function-related types like "imported function", "exported function", "execute function", "execute fn", etc.

@gumb0 gumb0 force-pushed the external-function-type-span branch from 27fb67c to 06c2923 Compare December 9, 2020 14:46
@gumb0
Copy link
Collaborator Author

gumb0 commented Dec 9, 2020

Looks ok, but this even more complicates the function-related types like "imported function", "exported function", "execute function", "execute fn", etc.

Added comments to execute_function and ExternalFunction, not sure if it helps with clarification.

@@ -59,6 +59,8 @@ class span
constexpr T* data() const noexcept { return m_begin; }
constexpr std::size_t size() const noexcept { return m_size; }

constexpr bool empty() const noexcept { return m_size == 0; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -22,9 +22,14 @@ namespace fizzy
struct ExecutionResult;
struct Instance;

// Function representing WebAssembly or host function execution.
Copy link
Member

@axic axic Dec 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make these (and on the struct) comments doxygen?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

@gumb0 gumb0 force-pushed the external-function-type-span branch from 06c2923 to 1171838 Compare January 4, 2021 14:44
@gumb0 gumb0 merged commit 12c0423 into master Jan 4, 2021
@gumb0 gumb0 deleted the external-function-type-span branch January 4, 2021 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants