Skip to content

Commit 51f953f

Browse files
committed
Switch to using ThreadStatic instead of ArrayPool
1 parent 51a8b12 commit 51f953f

File tree

3 files changed

+115
-55
lines changed

3 files changed

+115
-55
lines changed

src/Peachpie.CodeAnalysis/Peachpie.CodeAnalysis.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>net6.0</TargetFrameworks>
55
<OutputType>exe</OutputType>
6-
<NoWarn>$(NoWarn);1591</NoWarn>
6+
<NoWarn>$(NoWarn);1591;RS1009</NoWarn>
77
<AssemblyName>Peachpie.CodeAnalysis</AssemblyName>
88
<PackageId>Peachpie.CodeAnalysis</PackageId>
99
<PackageTags>php;peachpie;dotnet;compiler</PackageTags>

src/Peachpie.Runtime/PhpCallable.ArrayPool.tt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
<#@ output extension=".tt.cs" #>
22
using System;
3-
using System.Buffers;
4-
namespace Pchp.Core;
5-
using System.Runtime.CompilerServices;
63
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+
}
721

822
partial interface IPhpCallable
923
{
@@ -33,17 +47,19 @@ partial interface IPhpCallable
3347
<# } #>
3448
PhpValue p<#= arity - 1 #>)
3549
{
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 #>];
3753
try
3854
{
3955
<# for(int p = 0; p < arity; p++) { #>
4056
phpArgs[<#= p #>] = p<#= p #>;
4157
<# } #>
42-
return Invoke(ctx, new ReadOnlySpan<PhpValue>(phpArgs, 0, <#= arity #>));
58+
return Invoke(ctx, phpArgs);
4359
}
4460
finally
4561
{
46-
ArrayPool<PhpValue>.Shared.Return(phpArgs, true);
62+
Array.Clear(array, 0, <#= arity #>);
4763
}
4864
}
4965

0 commit comments

Comments
 (0)