Unsafe
and Span
optimizations
#434
Closed
TimothyMakkison
started this conversation in
Ideas
Replies: 1 comment 1 reply
-
Thank you for those ideas! I think the goal of Mapperly is to keep a good balance between performance and readability. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Related to #433. One of the main benefits Mapperly has over other mappers is that it generates readable code. What is Mapperly's stance on prioritizing high-performance code over readability? Furthermore, is the use of
System.Runtime.CompilerServices.Unsafe
code approved?There are a couple of places in
EnumerableMapping
whereSpan
could be used to boost the performance of mappings. Mainly when dealing withList
andArray
. Some of my proposed changes (ie operating on the array Span) are probably worth it, whereas others (mutating a Lists underlying Span) are obtuse and micro benchmarky.We can increase
List
iteration performance by usingCollectionsMarshal.AsSpan
and getting the span to the internal array. Note that this is unsafe but iirc should be safe as we are not altering the sourceList
. This is faster than usingEnumerable.ToArray
but is more verbose and harder to understand.Mutating a
Lists
underlyingArray
and then updating its length using a hackyUnsafe
method, 33-60% or 15-40% faster for lengths 100-1,000,000 depending on the element type.Beta Was this translation helpful? Give feedback.
All reactions