-
Notifications
You must be signed in to change notification settings - Fork 22
Description
I propose we support UnsafeAccessorAttribute to substitute reflection
This runtime feature is already supported by C# to give a performance boost compared to the reflection
Existing C# syntax:
private static class Accessors<T>
{
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_items")]
public static extern ref T[] GetItems(List<T> list);
}Suggested F# syntax:
module Accessors =
[<UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_items")>]
extern byref<'T[]> GetItems(ResizeArray<'T> resizeArray)Concrete Example
This code:
let inline private getUnderlyingDict<'T> =
let param = Expression.Parameter(typeof<'T>)
let storeProp = Expression.Property(param, "Store")
let getStoreExpr = Expression.Lambda<_>(storeProp, param)
let getStore: Func<'T, Dictionary<string, StringValues>> = getStoreExpr.Compile()
fun collection -> getStore.Invoke(collection)
let formCollectionDict = getUnderlyingDict<FormCollection>
let queryCollectionDict = getUnderlyingDict<QueryCollection>can be substituted by this code:
[<UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_Store")>]
extern Dictionary<string, StringValues> formCollectionDict(FormCollection formCollection)
[<UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_Store")>]
extern Dictionary<string, StringValues> queryCollectionDict(QueryCollection queryCollection)gaining both performance and readability
Related docs:
https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/functions/external-functions
https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.unsafeaccessorattribute
https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-9/#reflection
https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-10/#reflection
dotnet/runtime#81741
The existing way of approaching this problem in F# is using regular reflection
Concrete issues (per @Happypig375, see discussion):
- extern functions don't support generic types
- extern functions have an extraneous throw in implementation
Pros and Cons
The advantages of making this adjustment to F# are better performance in applicable cases
The disadvantages of making this adjustment to F# are don't see disadvantages
Extra information
Estimated cost (XS, S, M, L, XL, XXL): M
Related suggestions: (put links to related suggestions here)
Affidavit (please submit!)
Please tick these items 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
- This is a language change and not purely a tooling change (e.g. compiler bug, editor support, warning/error messages, new warning, non-breaking optimisation) belonging to the compiler and tooling repository
- 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
- I have searched both open and closed suggestions on this site and believe this is not a duplicate
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
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.