|
1 | 1 | <#@ output extension=".tt.cs" #> |
2 | 2 | using System; |
3 | | -using System.Buffers; |
4 | | -namespace Pchp.Core; |
5 | | -using System.Runtime.CompilerServices; |
6 | 3 | using System.Runtime.InteropServices; |
| 4 | +namespace Pchp.Core; |
| 5 | + |
| 6 | +file static class PhpCallableArrayCache |
| 7 | +{ |
| 8 | + // Since dotnet 8 |
| 9 | + // ThreadStatic performance is much faster |
| 10 | + // than ArrayPool |
| 11 | + // https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-8/#threadstatic |
| 12 | + |
| 13 | + [ThreadStatic] |
| 14 | + private static PhpValue[] _cache; |
| 15 | + |
| 16 | + public static PhpValue[] GetArray() |
| 17 | + { |
| 18 | + return _cache ??= new PhpValue[16]; |
| 19 | + } |
| 20 | +} |
7 | 21 |
|
8 | 22 | partial interface IPhpCallable |
9 | 23 | { |
@@ -33,17 +47,19 @@ partial interface IPhpCallable |
33 | 47 | <# } #> |
34 | 48 | PhpValue p<#= arity - 1 #>) |
35 | 49 | { |
36 | | - var phpArgs = ArrayPool<PhpValue>.Shared.Rent(<#= arity #>); |
| 50 | + var array = PhpCallableArrayCache.GetArray(); |
| 51 | + var memory = new Memory<PhpValue>(array); |
| 52 | + var phpArgs = memory.Span[..<#= arity #>]; |
37 | 53 | try |
38 | 54 | { |
39 | 55 | <# for(int p = 0; p < arity; p++) { #> |
40 | 56 | phpArgs[<#= p #>] = p<#= p #>; |
41 | 57 | <# } #> |
42 | | - return Invoke(ctx, new ReadOnlySpan<PhpValue>(phpArgs, 0, <#= arity #>)); |
| 58 | + return Invoke(ctx, phpArgs); |
43 | 59 | } |
44 | 60 | finally |
45 | 61 | { |
46 | | - ArrayPool<PhpValue>.Shared.Return(phpArgs, true); |
| 62 | + Array.Clear(array, 0, <#= arity #>); |
47 | 63 | } |
48 | 64 | } |
49 | 65 |
|
|
0 commit comments