Skip to content

Commit

Permalink
avoids some allocs
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmisek committed Aug 5, 2024
1 parent 08a409f commit 83f887b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Peachpie.CodeAnalysis/CodeGen/CodeGenerator.Emit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2583,11 +2583,11 @@ protected virtual void WriteBackAndFree(CodeGenerator cg)

public static void WriteBackAndFree(CodeGenerator cg, IList<WriteBackInfo> writebacks)
{
if (writebacks != null && writebacks.Count != 0)
if (writebacks != null)
{
foreach (var w in writebacks)
for (int i = 0; i < writebacks.Count; i++)
{
w.WriteBackAndFree(cg);
writebacks[i].WriteBackAndFree(cg);
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/Peachpie.Library/Arrays.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Pchp.Core;
using Pchp.Core.Resources;
using Pchp.Core.Utilities;
using Pchp.Library.Resources;
using System;
using System.Collections;
Expand Down Expand Up @@ -533,8 +534,15 @@ public static PhpValue array_rand(PhpArray array, int count = 1)
{
if (count == 1)
{
var result = new List<PhpValue>(1);
return RandomSubset(array.Keys, result, count, PhpMath.Generator) ? result[0] : PhpValue.Null;
var result = ListPool<PhpValue>.Pool.Get();
try
{
return RandomSubset(array.Keys, result, count, PhpMath.Generator) ? result[0] : PhpValue.Null;
}
finally
{
ListPool<PhpValue>.Pool.Return(result);
}
}
else
{
Expand Down
20 changes: 16 additions & 4 deletions src/Peachpie.Library/FileSystem.Glob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ public string MakeString()
}
}

sealed class GlobMatcher
sealed class GlobMatcher : IDisposable
{
readonly Context/*!*/_ctx;
readonly string/*!*/ _pattern;
readonly GlobOptions _flags;
readonly List<string>/*!*/ _result;
readonly bool _dirOnly;
List<string>/*!*/ _result;
bool _stripTwo;
bool _relative;
FnMatchOptions _fnMatchFlags;
Expand All @@ -172,12 +172,19 @@ public GlobMatcher(Context ctx, string/*!*/pattern, GlobOptions flags)
_ctx = ctx;
_pattern = pattern;
_flags = flags;
_result = new List<string>();
_result = ListPool<string>.Pool.Get();
_dirOnly = PathUtils.IsDirectorySeparator((char)_pattern.LastCharacter()) || (flags & GlobOptions.OnlyDir) != 0;

_fnMatchFlags = NoEscapes ? FnMatchOptions.NoEscape : FnMatchOptions.None;
}

public void Dispose()
{
ListPool<string>.Pool.Return(_result);
_result = null;
}

bool IsDisposed => _result == null;

private static string/*!*/ Unescape(string/*!*/ path, int start)
{
Expand Down Expand Up @@ -243,6 +250,11 @@ private void TestPath(string path, int patternEnd, bool isLastPathSegment)

internal List<string>/*!*/ DoGlob()
{
if (IsDisposed)
{
throw new ObjectDisposedException(nameof(GlobMatcher));
}

if (_pattern.Length == 0)
{
return _result;
Expand Down Expand Up @@ -790,7 +802,7 @@ static ValueList<string> UngroupGlobs(string/*!*/ pattern, bool noEscape, bool b

foreach (string group in groups)
{
var matcher = new GlobMatcher(ctx, group, flags);
using var matcher = new GlobMatcher(ctx, group, flags);

foreach (string filename in matcher.DoGlob())
{
Expand Down

0 comments on commit 83f887b

Please sign in to comment.