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 stackallocspan function that returns a Span #720

Closed
4 of 5 tasks
teo-tsirpanis opened this issue Jan 30, 2019 · 4 comments
Closed
4 of 5 tasks

Add a safe stackallocspan function that returns a Span #720

teo-tsirpanis opened this issue Jan 30, 2019 · 4 comments

Comments

@teo-tsirpanis
Copy link

teo-tsirpanis commented Jan 30, 2019

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 stackallocspan<'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
Copy link
Member

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
Copy link
Author

teo-tsirpanis commented Jan 31, 2019

Not necessarily. We can reference System.Memory.

Edit: yeah, it doesn't seem like a good idea.

@Happypig375
Copy link
Contributor

This should be declined in favour of dotnet/runtime#52065.

@dsyme dsyme changed the title Add a safe stackalloc function that returns a Span Add a safe stackallocspan function that returns a Span Jun 16, 2022
@dsyme
Copy link
Collaborator

dsyme commented Apr 12, 2023

Declining per @Happypig375's comment

@dsyme dsyme closed this as completed Apr 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants