From ae4324032a48ee08a808354673f47536e76759d0 Mon Sep 17 00:00:00 2001 From: Andrey Sukharev Date: Thu, 19 Jan 2023 01:25:16 +0300 Subject: Optimize string memory usage. Use Spans and StringBuilders where possible (#3933) * Optimize string memory usage. Use ReadOnlySpan and StringBuilder where possible. * Fix copypaste error * Code generator review fixes * Use if statement instead of switch * Code style fixes Co-authored-by: TSRBerry <20988865+TSRBerry@users.noreply.github.com> * Another code style fix * Styling fix Co-authored-by: Mary-nyan * Styling fix Co-authored-by: gdkchan Co-authored-by: TSRBerry <20988865+TSRBerry@users.noreply.github.com> Co-authored-by: Mary-nyan Co-authored-by: gdkchan --- .../Diagnostics/Demangler/Ast/IntegerLiteral.cs | 3 ++- Ryujinx.HLE/HOS/Diagnostics/Demangler/Demangler.cs | 26 +++++++++++----------- 2 files changed, 15 insertions(+), 14 deletions(-) (limited to 'Ryujinx.HLE/HOS/Diagnostics') diff --git a/Ryujinx.HLE/HOS/Diagnostics/Demangler/Ast/IntegerLiteral.cs b/Ryujinx.HLE/HOS/Diagnostics/Demangler/Ast/IntegerLiteral.cs index 951faa55..ea048d76 100644 --- a/Ryujinx.HLE/HOS/Diagnostics/Demangler/Ast/IntegerLiteral.cs +++ b/Ryujinx.HLE/HOS/Diagnostics/Demangler/Ast/IntegerLiteral.cs @@ -1,4 +1,5 @@ using System.IO; +using System; namespace Ryujinx.HLE.HOS.Diagnostics.Demangler.Ast { @@ -25,7 +26,7 @@ namespace Ryujinx.HLE.HOS.Diagnostics.Demangler.Ast if (_literalValue[0] == 'n') { writer.Write("-"); - writer.Write(_literalValue.Substring(1)); + writer.Write(_literalValue.AsSpan(1)); } else { diff --git a/Ryujinx.HLE/HOS/Diagnostics/Demangler/Demangler.cs b/Ryujinx.HLE/HOS/Diagnostics/Demangler/Demangler.cs index be5b7539..a6618eca 100644 --- a/Ryujinx.HLE/HOS/Diagnostics/Demangler/Demangler.cs +++ b/Ryujinx.HLE/HOS/Diagnostics/Demangler/Demangler.cs @@ -32,9 +32,9 @@ namespace Ryujinx.HLE.HOS.Diagnostics.Demangler private bool ConsumeIf(string toConsume) { - string mangledPart = Mangled.Substring(_position); + var mangledPart = Mangled.AsSpan(_position); - if (mangledPart.StartsWith(toConsume)) + if (mangledPart.StartsWith(toConsume.AsSpan())) { _position += toConsume.Length; @@ -44,14 +44,14 @@ namespace Ryujinx.HLE.HOS.Diagnostics.Demangler return false; } - private string PeekString(int offset = 0, int length = 1) + private ReadOnlySpan PeekString(int offset = 0, int length = 1) { if (_position + offset >= length) { return null; } - return Mangled.Substring(_position + offset, length); + return Mangled.AsSpan(_position + offset, length); } private char Peek(int offset = 0) @@ -101,8 +101,8 @@ namespace Ryujinx.HLE.HOS.Diagnostics.Demangler private int ParseSeqId() { - string part = Mangled.Substring(_position); - int seqIdLen = 0; + ReadOnlySpan part = Mangled.AsSpan(_position); + int seqIdLen = 0; for (; seqIdLen < part.Length; seqIdLen++) { @@ -114,7 +114,7 @@ namespace Ryujinx.HLE.HOS.Diagnostics.Demangler _position += seqIdLen; - return FromBase36(part.Substring(0, seqIdLen)); + return FromBase36(new string(part[..seqIdLen])); } // ::= S _ @@ -900,8 +900,8 @@ namespace Ryujinx.HLE.HOS.Diagnostics.Demangler private int ParsePositiveNumber() { - string part = Mangled.Substring(_position); - int numberLength = 0; + ReadOnlySpan part = Mangled.AsSpan(_position); + int numberLength = 0; for (; numberLength < part.Length; numberLength++) { @@ -918,7 +918,7 @@ namespace Ryujinx.HLE.HOS.Diagnostics.Demangler return -1; } - return int.Parse(part.AsSpan(0, numberLength)); + return int.Parse(part[..numberLength]); } private string ParseNumber(bool isSigned = false) @@ -933,8 +933,8 @@ namespace Ryujinx.HLE.HOS.Diagnostics.Demangler return null; } - string part = Mangled.Substring(_position); - int numberLength = 0; + ReadOnlySpan part = Mangled.AsSpan(_position); + int numberLength = 0; for (; numberLength < part.Length; numberLength++) { @@ -946,7 +946,7 @@ namespace Ryujinx.HLE.HOS.Diagnostics.Demangler _position += numberLength; - return part.Substring(0, numberLength); + return new string(part[..numberLength]); } // ::= -- cgit v1.2.3