Skip to content

Commit

Permalink
Strings should not be concatenated using '+' in a loop (#5664)
Browse files Browse the repository at this point in the history
* Strings should not be concatenated using '+' in a loop

* fix IDE0090

* undo GenerateLoadOrStore

* prefer string interpolation

* Update src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGen.cs

Co-authored-by: Mary <thog@protonmail.com>

---------

Co-authored-by: Mary <thog@protonmail.com>
  • Loading branch information
marco-carvalho and marysaka authored Oct 5, 2023
1 parent 0aceb53 commit 7835968
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 51 deletions.
8 changes: 5 additions & 3 deletions src/ARMeilleure/Diagnostics/Symbols.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace ARMeilleure.Diagnostics
{
Expand Down Expand Up @@ -48,14 +49,15 @@ public static string Get(ulong address)
ulong diff = address - symbol.Start;
ulong rem = diff % symbol.ElementSize;

result = symbol.Name + "_" + diff / symbol.ElementSize;
StringBuilder resultBuilder = new();
resultBuilder.Append($"{symbol.Name}_{diff / symbol.ElementSize}");

if (rem != 0)
{
result += "+" + rem;
resultBuilder.Append($"+{rem}");
}

_symbols.TryAdd(address, result);
_symbols.TryAdd(address, resultBuilder.ToString());

return result;
}
Expand Down
11 changes: 5 additions & 6 deletions src/Ryujinx.Ava/UI/ViewModels/AmiiboWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ private void SetAmiiboDetails()

string imageUrl = _amiiboList.Find(amiibo => amiibo.Equals(selected)).Image;

string usageString = "";
StringBuilder usageStringBuilder = new();

for (int i = 0; i < _amiiboList.Count; i++)
{
Expand All @@ -341,20 +341,19 @@ private void SetAmiiboDetails()
{
foreach (AmiiboApiUsage usageItem in item.AmiiboUsage)
{
usageString += Environment.NewLine +
$"- {usageItem.Usage.Replace("/", Environment.NewLine + "-")}";
usageStringBuilder.Append($"{Environment.NewLine}- {usageItem.Usage.Replace("/", Environment.NewLine + "-")}");

writable = usageItem.Write;
}
}
}

if (usageString.Length == 0)
if (usageStringBuilder.Length == 0)
{
usageString = LocaleManager.Instance[LocaleKeys.Unknown] + ".";
usageStringBuilder.Append($"{LocaleManager.Instance[LocaleKeys.Unknown]}.");
}

Usage = $"{LocaleManager.Instance[LocaleKeys.Usage]} {(writable ? $" ({LocaleManager.Instance[LocaleKeys.Writable]})" : "")} : {usageString}";
Usage = $"{LocaleManager.Instance[LocaleKeys.Usage]} {(writable ? $" ({LocaleManager.Instance[LocaleKeys.Writable]})" : "")} : {usageStringBuilder}";
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Ryujinx.Graphics.Shader/CodeGen/Glsl/CodeGenContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ private void UpdateIndentation()

private static string GetIndentation(int level)
{
string indentation = string.Empty;
StringBuilder indentationBuilder = new();

for (int index = 0; index < level; index++)
{
indentation += Tab;
indentationBuilder.Append(Tab);
}

return indentation;
return indentationBuilder.ToString();
}
}
}
13 changes: 7 additions & 6 deletions src/Ryujinx.Graphics.Shader/CodeGen/Glsl/Instructions/InstGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Ryujinx.Graphics.Shader.StructuredIr;
using Ryujinx.Graphics.Shader.Translation;
using System;
using System.Text;

using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenBallot;
using static Ryujinx.Graphics.Shader.CodeGen.Glsl.Instructions.InstGenCall;
Expand Down Expand Up @@ -67,19 +68,19 @@ private static string GetExpression(CodeGenContext context, AstOperation operati

int arity = (int)(info.Type & InstType.ArityMask);

string args = string.Empty;
StringBuilder builder = new();

if (atomic && (operation.StorageKind == StorageKind.StorageBuffer || operation.StorageKind == StorageKind.SharedMemory))
{
args = GenerateLoadOrStore(context, operation, isStore: false);
builder.Append(GenerateLoadOrStore(context, operation, isStore: false));

AggregateType dstType = operation.Inst == Instruction.AtomicMaxS32 || operation.Inst == Instruction.AtomicMinS32
? AggregateType.S32
: AggregateType.U32;

for (int argIndex = operation.SourcesCount - arity + 2; argIndex < operation.SourcesCount; argIndex++)
{
args += ", " + GetSoureExpr(context, operation.GetSource(argIndex), dstType);
builder.Append($", {GetSoureExpr(context, operation.GetSource(argIndex), dstType)}");
}
}
else
Expand All @@ -88,16 +89,16 @@ private static string GetExpression(CodeGenContext context, AstOperation operati
{
if (argIndex != 0)
{
args += ", ";
builder.Append(", ");
}

AggregateType dstType = GetSrcVarType(inst, argIndex);

args += GetSoureExpr(context, operation.GetSource(argIndex), dstType);
builder.Append(GetSoureExpr(context, operation.GetSource(argIndex), dstType));
}
}

return info.OpName + '(' + args + ')';
return $"{info.OpName}({builder})";
}
else if ((info.Type & InstType.Op) != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,17 +779,18 @@ private static string GetMask(int index)

private static string GetMaskMultiDest(int mask)
{
string swizzle = ".";
StringBuilder swizzleBuilder = new();
swizzleBuilder.Append('.');

for (int i = 0; i < 4; i++)
{
if ((mask & (1 << i)) != 0)
{
swizzle += "xyzw"[i];
swizzleBuilder.Append("xyzw"[i]);
}
}

return swizzle;
return swizzleBuilder.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;

namespace Ryujinx.Graphics.Shader.Translation.Optimizations
Expand Down Expand Up @@ -785,30 +786,31 @@ private static (int, int) UnpackCbSlotAndOffset(uint packed)

private static string GetFunctionName(Operation baseOp, bool isMultiTarget, IReadOnlyList<uint> targetCbs)
{
string name = baseOp.Inst.ToString();
StringBuilder nameBuilder = new();
nameBuilder.Append(baseOp.Inst.ToString());

name += baseOp.StorageKind switch
nameBuilder.Append(baseOp.StorageKind switch
{
StorageKind.GlobalMemoryS8 => "S8",
StorageKind.GlobalMemoryS16 => "S16",
StorageKind.GlobalMemoryU8 => "U8",
StorageKind.GlobalMemoryU16 => "U16",
_ => string.Empty,
};
});

if (isMultiTarget)
{
name += "Multi";
nameBuilder.Append("Multi");
}

foreach (uint targetCb in targetCbs)
{
(int sbCbSlot, int sbCbOffset) = UnpackCbSlotAndOffset(targetCb);

name += $"_c{sbCbSlot}o{sbCbOffset}";
nameBuilder.Append($"_c{sbCbSlot}o{sbCbOffset}");
}

return name;
return nameBuilder.ToString();
}

private static bool TryGenerateStorageOp(
Expand Down
9 changes: 5 additions & 4 deletions src/Ryujinx.HLE/FileSystem/ContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using Path = System.IO.Path;

namespace Ryujinx.HLE.FileSystem
Expand Down Expand Up @@ -817,13 +818,13 @@ SystemVersion VerifyAndGetVersionZip(ZipArchive archive)

if (updateNcas.Count > 0)
{
string extraNcas = string.Empty;
StringBuilder extraNcas = new();

foreach (var entry in updateNcas)
{
foreach (var (type, path) in entry.Value)
{
extraNcas += path + Environment.NewLine;
extraNcas.AppendLine(path);
}
}

Expand Down Expand Up @@ -954,13 +955,13 @@ SystemVersion VerifyAndGetVersion(IFileSystem filesystem)

if (updateNcas.Count > 0)
{
string extraNcas = string.Empty;
StringBuilder extraNcas = new();

foreach (var entry in updateNcas)
{
foreach (var (type, path) in entry.Value)
{
extraNcas += path + Environment.NewLine;
extraNcas.AppendLine(path);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/Ryujinx.HLE/HOS/Kernel/Process/HleProcessDebugger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,14 @@ private static ElfSymbol GetSymbol64(IVirtualMemoryManager memory, ulong address

uint nameIndex = sym.NameOffset;

string name = string.Empty;
StringBuilder nameBuilder = new();

for (int chr; (chr = memory.Read<byte>(strTblAddr + nameIndex++)) != 0;)
{
name += (char)chr;
nameBuilder.Append((char)chr);
}

return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
return new ElfSymbol(nameBuilder.ToString(), sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
}

private static ElfSymbol GetSymbol32(IVirtualMemoryManager memory, ulong address, ulong strTblAddr)
Expand All @@ -452,14 +452,14 @@ private static ElfSymbol GetSymbol32(IVirtualMemoryManager memory, ulong address

uint nameIndex = sym.NameOffset;

string name = string.Empty;
StringBuilder nameBuilder = new();

for (int chr; (chr = memory.Read<byte>(strTblAddr + nameIndex++)) != 0;)
{
name += (char)chr;
nameBuilder.Append((char)chr);
}

return new ElfSymbol(name, sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
return new ElfSymbol(nameBuilder.ToString(), sym.Info, sym.Other, sym.SectionIndex, sym.ValueAddress, sym.Size);
}
}
}
7 changes: 4 additions & 3 deletions src/Ryujinx.HLE/HOS/Services/Sm/IUserInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Ryujinx.HLE.HOS.Services.Sm
{
Expand Down Expand Up @@ -235,7 +236,7 @@ public ResultCode UnregisterService(ServiceCtx context)

private static string ReadName(ServiceCtx context)
{
string name = string.Empty;
StringBuilder nameBuilder = new();

for (int index = 0; index < 8 &&
context.RequestData.BaseStream.Position <
Expand All @@ -245,11 +246,11 @@ private static string ReadName(ServiceCtx context)

if (chr >= 0x20 && chr < 0x7f)
{
name += (char)chr;
nameBuilder.Append((char)chr);
}
}

return name;
return nameBuilder.ToString();
}

public override void DestroyAtExit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,19 @@ public ResultCode ListDisplays(ServiceCtx context)
// OpenDisplay(nn::vi::DisplayName) -> u64 display_id
public ResultCode OpenDisplay(ServiceCtx context)
{
string name = "";
StringBuilder nameBuilder = new();

for (int index = 0; index < 8 && context.RequestData.BaseStream.Position < context.RequestData.BaseStream.Length; index++)
{
byte chr = context.RequestData.ReadByte();

if (chr >= 0x20 && chr < 0x7f)
{
name += (char)chr;
nameBuilder.Append((char)chr);
}
}

return OpenDisplayImpl(context, name);
return OpenDisplayImpl(context, nameBuilder.ToString());
}

[CommandCmif(1011)]
Expand Down
7 changes: 4 additions & 3 deletions src/Ryujinx.Horizon/Sdk/Sm/ServiceName.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Ryujinx.Horizon.Sdk.Sm
{
Expand Down Expand Up @@ -78,7 +79,7 @@ public override int GetHashCode()

public override string ToString()
{
string name = string.Empty;
StringBuilder nameBuilder = new();

for (int index = 0; index < sizeof(ulong); index++)
{
Expand All @@ -89,10 +90,10 @@ public override string ToString()
break;
}

name += (char)character;
nameBuilder.Append((char)character);
}

return name;
return nameBuilder.ToString();
}
}
}
13 changes: 8 additions & 5 deletions src/Ryujinx.Ui.LocaleGenerator/LocaleGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.CodeAnalysis;
using System.Linq;
using System.Text;

namespace Ryujinx.Ui.LocaleGenerator
{
Expand All @@ -15,15 +16,17 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterSourceOutput(contents, (spc, content) =>
{
var lines = content.Split('\n').Where(x => x.Trim().StartsWith("\"")).Select(x => x.Split(':')[0].Trim().Replace("\"", ""));
string enumSource = "namespace Ryujinx.Ava.Common.Locale;\n";
enumSource += "internal enum LocaleKeys\n{\n";
StringBuilder enumSourceBuilder = new();
enumSourceBuilder.AppendLine("namespace Ryujinx.Ava.Common.Locale;");
enumSourceBuilder.AppendLine("internal enum LocaleKeys");
enumSourceBuilder.AppendLine("{");
foreach (var line in lines)
{
enumSource += $" {line},\n";
enumSourceBuilder.AppendLine($" {line},");
}
enumSource += "}\n";
enumSourceBuilder.AppendLine("}");
spc.AddSource("LocaleKeys", enumSource);
spc.AddSource("LocaleKeys", enumSourceBuilder.ToString());
});
}
}
Expand Down

0 comments on commit 7835968

Please sign in to comment.