Skip to content

Commit

Permalink
il gen, avoids .newobj when emitting string operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmisek committed Apr 29, 2023
1 parent 78b888c commit 25e1b6f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Peachpie.CodeAnalysis/CodeGen/Graph/BoundExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3236,7 +3236,7 @@ internal override TypeSymbol Emit(CodeGenerator cg)

// new PhpString.Blob( capacity )
cg.Builder.EmitIntConstant(args.Length);
cg.EmitCall(ILOpCode.Newobj, cg.CoreMethods.Ctors.Blob_int);
cg.EmitCall(ILOpCode.Call, cg.CoreMethods.PhpStringBlob.Create_Int32);

// TODO: overload for 2, 3, 4 parameters directly

Expand All @@ -3255,7 +3255,7 @@ internal override TypeSymbol Emit(CodeGenerator cg)
}

// new PhpString( <Blob> )
return cg.EmitCall(ILOpCode.Newobj, cg.CoreMethods.Ctors.PhpString_Blob)
return cg.EmitCall(ILOpCode.Call, cg.CoreMethods.PhpString.From_Blob)
.Expect(cg.CoreTypes.PhpString);
}

Expand Down Expand Up @@ -3777,7 +3777,7 @@ static TypeSymbol EmitAppend(CodeGenerator cg, BoundReferenceExpression target,
// STACK: PhpString.Blob

// Template: new PhpString(blob)
var result_type = cg.EmitCall(ILOpCode.Newobj, cg.CoreMethods.Ctors.PhpString_Blob)
var result_type = cg.EmitCall(ILOpCode.Call, cg.CoreMethods.PhpString.From_Blob)
.Expect(cg.CoreTypes.PhpString);

// STACK: PhpString
Expand Down
12 changes: 8 additions & 4 deletions src/Peachpie.CodeAnalysis/Symbols/CoreMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,8 @@ public struct PhpStringHolder
{
public PhpStringHolder(CoreTypes ct)
{
From_Blob = ct.PhpString.Method("From", ct.PhpString_Blob);

ToString_Context = ct.PhpString.Method("ToString", ct.Context);
ToNumber = ct.PhpString.Method("ToNumber");
ToBytes_Context = ct.PhpString.Method("ToBytes", ct.Context);
Expand All @@ -831,6 +833,7 @@ public PhpStringHolder(CoreTypes ct)
}

public readonly CoreMethod
From_Blob,
ToString_Context, ToNumber, ToBytes_Context,
EnsureWritable, AsWritable_PhpString, AsArray_PhpString,
IsNull_PhpString;
Expand All @@ -850,6 +853,8 @@ public PhpStringBlobHolder(CoreTypes ct)
{
CoreTypes = ct;

Create_Int32 = ct.PhpString_Blob.Method("Create", ct.Int32);

Add_String = ct.PhpString_Blob.Method("Add", ct.String);
Add_PhpString = ct.PhpString_Blob.Method("Add", ct.PhpString);
Add_PhpValue_Context = ct.PhpString_Blob.Method("Add", ct.PhpValue, ct.Context);
Expand All @@ -858,6 +863,7 @@ public PhpStringBlobHolder(CoreTypes ct)
}

public readonly CoreMethod
Create_Int32,
Add_String, Add_PhpString, Add_PhpValue_Context;

public MethodSymbol Add_ByteArray
Expand Down Expand Up @@ -976,12 +982,10 @@ public struct ConstructorsHolder
{
public ConstructorsHolder(CoreTypes ct)
{
PhpString_Blob = ct.PhpString.Ctor(ct.PhpString_Blob);
PhpString_string_string = ct.PhpString.Ctor(ct.String, ct.String);
PhpString_PhpValue_Context = ct.PhpString.Ctor(ct.PhpValue, ct.Context);
PhpString_PhpString = ct.PhpString.Ctor(ct.PhpString);
Blob = ct.PhpString_Blob.Ctor();
Blob_int = ct.PhpString_Blob.Ctor(ct.Int32);
PhpArray = ct.PhpArray.Ctor();
PhpArray_int = ct.PhpArray.Ctor(ct.Int32);
IntStringKey_long = ct.IntStringKey.Ctor(ct.Long);
Expand All @@ -1006,8 +1010,8 @@ public ConstructorsHolder(CoreTypes ct)

public readonly CoreConstructor
PhpArray, PhpArray_int,
PhpString_Blob, PhpString_PhpString, PhpString_string_string, PhpString_PhpValue_Context,
Blob, Blob_int,
PhpString_PhpString, PhpString_string_string, PhpString_PhpValue_Context,
Blob,
IntStringKey_long, IntStringKey_string,
ScriptAttribute_string_long, PhpTraitAttribute, PharAttribute_string, PhpTypeAttribute_string_string, PhpTypeAttribute_string_string_byte, PhpFieldsOnlyCtorAttribute, PhpHiddenAttribute,
DefaultValueAttribute_string,
Expand Down
10 changes: 10 additions & 0 deletions src/Peachpie.Runtime/PhpString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,11 @@ static void InplaceDeepCopy(ref object chunk)
else chunk = ((Array)chunk).Clone(); // byte[], char[], BlobChar[]
}

/// <summary>
/// Creates instance of <see cref="Blob"/> with pre-allocated buckets.
/// </summary>
public static Blob Create(int capacity) => new Blob(capacity);

#endregion

#region Length
Expand Down Expand Up @@ -1647,6 +1652,11 @@ public PhpString(string x, string y)

public PhpString DeepCopy() => new PhpString(this);

/// <summary>
/// Creates <see cref="PhpString"/> referencing an existing <see cref="Blob"/>.
/// </summary>
public static PhpString/*!*/From(Blob blob) => new PhpString(blob);

#endregion

/// <summary>
Expand Down

0 comments on commit 25e1b6f

Please sign in to comment.