|
| 1 | +open System |
| 2 | +open System.Runtime.InteropServices |
| 3 | + |
| 4 | +let createSpanFromArray () = |
| 5 | + // <Snippet1> |
| 6 | + // Create a span over an array. |
| 7 | + let array = Array.zeroCreate<byte> 100 |
| 8 | + let arraySpan = Span<byte> array |
| 9 | + |
| 10 | + let mutable data = 0uy |
| 11 | + for i = 0 to arraySpan.Length - 1 do |
| 12 | + arraySpan[i] <- data |
| 13 | + data <- data + 1uy |
| 14 | + |
| 15 | + let mutable arraySum = 0 |
| 16 | + for value in array do |
| 17 | + arraySum <- arraySum + int value |
| 18 | + |
| 19 | + printfn $"The sum is {arraySum}" |
| 20 | + // Output: The sum is 4950 |
| 21 | + // </Snippet1> |
| 22 | + |
| 23 | +let createSpanFromNativeMemory () = |
| 24 | + // <Snippet2> |
| 25 | + // Create a span from native memory. |
| 26 | + let native = Marshal.AllocHGlobal 100 |
| 27 | + let nativeSpan = Span<byte>(native.ToPointer(), 100) |
| 28 | + |
| 29 | + let mutable data = 0uy |
| 30 | + for i = 0 to nativeSpan.Length - 1 do |
| 31 | + nativeSpan[i] <- data |
| 32 | + data <- data + 1uy |
| 33 | + |
| 34 | + let mutable nativeSum = 0 |
| 35 | + for value in nativeSpan do |
| 36 | + nativeSum <- nativeSum + int value |
| 37 | + |
| 38 | + printfn $"The sum is {nativeSum}" |
| 39 | + Marshal.FreeHGlobal native |
| 40 | + // Output: The sum is 4950 |
| 41 | + // </Snippet2> |
| 42 | + |
| 43 | +let createSpanFromStack () = |
| 44 | + // <Snippet3> |
| 45 | + // Create a span on the stack. |
| 46 | + let mutable data = 0uy |
| 47 | + let stackSpan = |
| 48 | + let p = NativeInterop.NativePtr.stackalloc<byte> 100 |> NativeInterop.NativePtr.toVoidPtr |
| 49 | + Span<byte>(p, 100) |
| 50 | + |
| 51 | + for i = 0 to stackSpan.Length - 1 do |
| 52 | + stackSpan[i] <- data |
| 53 | + data <- data + 1uy |
| 54 | + |
| 55 | + let mutable stackSum = 0 |
| 56 | + for value in stackSpan do |
| 57 | + stackSum <- stackSum + int value |
| 58 | + |
| 59 | + printfn $"The sum is {stackSum}" |
| 60 | +// Output: The sum is 4950 |
| 61 | +// </Snippet3> |
| 62 | + |
| 63 | +module Program2 = |
| 64 | + // <Snippet4> |
| 65 | + open System |
| 66 | + open System.Runtime.InteropServices |
| 67 | + open FSharp.NativeInterop |
| 68 | + |
| 69 | + // Package FSharp.NativeInterop.NativePtr.stackalloc for reuse. |
| 70 | + let inline stackalloc<'a when 'a: unmanaged> length : Span<'a> = |
| 71 | + let voidPointer = NativePtr.stackalloc<'a> length |> NativePtr.toVoidPtr |
| 72 | + Span<'a>(voidPointer, length) |
| 73 | + |
| 74 | + let initializeSpan (span: Span<byte>) = |
| 75 | + let mutable value = 0uy |
| 76 | + for i = 0 to span.Length - 1 do |
| 77 | + span[i] <- value |
| 78 | + value <- value + 1uy |
| 79 | + |
| 80 | + let computeSum (span: Span<byte>) = |
| 81 | + let mutable sum = 0 |
| 82 | + for value in span do |
| 83 | + sum <- sum + int value |
| 84 | + sum |
| 85 | + |
| 86 | + let workWithSpans () = |
| 87 | + // Create a span over an array. |
| 88 | + let array = Array.zeroCreate<byte> 100 |
| 89 | + let arraySpan = Span<byte> array |
| 90 | + |
| 91 | + initializeSpan arraySpan |
| 92 | + printfn $"The sum is {computeSum arraySpan:N0}" |
| 93 | + |
| 94 | + // Create an array from native memory. |
| 95 | + let native = Marshal.AllocHGlobal 100 |
| 96 | + let nativeSpan = Span<byte>(native.ToPointer(), 100) |
| 97 | + |
| 98 | + initializeSpan nativeSpan |
| 99 | + printfn $"The sum is {computeSum nativeSpan:N0}" |
| 100 | + |
| 101 | + Marshal.FreeHGlobal native |
| 102 | + |
| 103 | + // Create a span on the stack. |
| 104 | + let stackSpan = stackalloc 100 |
| 105 | + |
| 106 | + initializeSpan stackSpan |
| 107 | + printfn $"The sum is {computeSum stackSpan:N0}" |
| 108 | + |
| 109 | + // The example displays the following output: |
| 110 | + // The sum is 4,950 |
| 111 | + // The sum is 4,950 |
| 112 | + // The sum is 4,950 |
| 113 | + // </Snippet4> |
| 114 | + |
| 115 | +createSpanFromArray () |
| 116 | +printfn "-----" |
| 117 | +createSpanFromNativeMemory () |
| 118 | +printfn "-----" |
| 119 | +createSpanFromStack () |
| 120 | +printfn "-----" |
| 121 | +Program2.workWithSpans () |
0 commit comments