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

Implement support for collections of collections marshalling #1226

Conversation

jkoritzinsky
Copy link
Member

The current code in the feature/DllImportGenerator branch almost supports passing collections of collections (like jagged arrays) by value or by-ref in (it only needs the first commit that handles nested for loops), but it does not correctly support passing collections of collections in a way that requires unmarshalling.

This PR adds the functionality required to enable marshalling collections of collections like jagged arrays from native code back to managed code.

This code is designed such that if n levels of indirection is supported, then n+1 levels is also supported. The CollectionsOfCollectionsStress code snippet test validates this by testing 0-11 dimensional jagged arrays.

To enable safely tracing dependencies of "CountElementName" metadata values, this PR enhances the cycle-breaking in the MarshallingAttributeInfoParser to correctly break cycles.

To identify dependencies between parameters/return values, this PR also introduces a topological sorting algorithm to order the parameter/return value unmarshalling in an order that ensures dependencies are marshalled before elements that depend on them.

This algorithm is specified generically so developers of interop source generators could use this mechanism to support their own scenarios. For example, WinRT's implicit array length parameters could have part of their implementation supported by the topological sort.

Use a topological sort of the parameter+return marshallers to unmarshal "CountElementName"-referenced parameters/return before unmarshalling the elements that have a dependency on them through "CountElementName".
…abstractions to enable more perf optimizations.
… native-only parameters as distinct nodes in the graph model.
…types of the CountElementName'd elements, even in nested scenarios.
@jkoritzinsky jkoritzinsky added the area-DllImportGenerator Source Generated stubs for P/Invokes in C# label Jun 10, 2021
@jkoritzinsky jkoritzinsky merged commit aa2c08c into dotnet:feature/DllImportGenerator Jun 12, 2021
jkoritzinsky added a commit to jkoritzinsky/runtime that referenced this pull request Sep 20, 2021
…runtimelab#1226)

* Refactor indexer naming to be nested-collection safe.

* Implement initial collections-of-collections/jagged array support.

Use a topological sort of the parameter+return marshallers to unmarshal "CountElementName"-referenced parameters/return before unmarshalling the elements that have a dependency on them through "CountElementName".

* Fixes for edge cases around HResult/Exception handling.

* Flip the edgeMap indices so we can use Array.IndexOf (which is optimized) to search for edges.

* Comments and optimizations.

* Add citation for algorithm,

* Hoist elementIndex out of the loop.

* Encapsulate the edgeMap in a private struct type and add some simple abstractions to enable more perf optimizations.

* Make Topological sort more flexible for element ids

* Use native index if managed index is unset to allow handling multiple native-only parameters as distinct nodes in the graph model.

* Fix using the return value for passing collection size. Validate the types of the CountElementName'd elements, even in nested scenarios.

* Change how we initialize the numRows array.

* Fix cycle breaking in count info.

* Update nested indexer creation for num elements expressions to handle non-collection sub contexts.

* Add stress test for collections of collections that uses 0-11 nested arrays.

* PR feedback.

Commit migrated from dotnet/runtimelab@aa2c08c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-DllImportGenerator Source Generated stubs for P/Invokes in C#
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants