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

Add a safe stackalloc function that returns a Span #720

Open
teo-tsirpanis opened this Issue Jan 30, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@teo-tsirpanis
Copy link

teo-tsirpanis commented Jan 30, 2019

Add a safe stackalloc function that returns a Span

I propose we add a function that returns a Span pointing to stack memory.

The existing way of approaching this problem in F# is ...

open FSharp.NativeInterop
open System

let inline stackalloc<'a when 'a: unmanaged> size =
    let p = NativePtr.stackalloc<'a> size |> NativePtr.toVoidPtr
    Span<'a>(p, size)

I propose that the function's signature would be as the above. Furthermore, explicit type arguments should not be required. Even in constructs like Unchecked.defaultof and typeof, they are required, but can be circumvented by typing something like typeof<_> (when it doesn't make any sense at all).

The function above causes the compiler to complain twice about unverifiable code. The proposal should not produce any compiler warnings, just like in C#.

Also, returning a stackalloced Span from a function that created it should be forbidden in compile-time to avoid memory corruption, just like C#. For example, if I remove the inline modifier above, it would have led into some serious stack smashing occurrence.

Pros and Cons

The advantages of making this adjustment to F# are the ability to simply and safely exploit the memory in the stack and feature parity with the C# compiler.

The disadvantages of making this adjustment to F# are (the section is intentionally left blank)

Extra information

Estimated cost (XS, S, M, L, XL, XXL): M (for the last paragraph of my proposal, XS for the rest).

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this
@cartermp

This comment has been minimized.

Copy link
Member

cartermp commented Jan 31, 2019

The big blocker to something like this is that FSharp.Core would have to target either:

  • netcoreapp2.1 or higher
  • netstandard2.1

And likely do so as another binary. So this would increase the number of binaries in the package to three, which isn't ideal.

@teo-tsirpanis

This comment has been minimized.

Copy link
Author

teo-tsirpanis commented Jan 31, 2019

Not necessarily. We can reference System.Memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.