From 30ee70a9bc9529772198a51b1b9a605932d2ea96 Mon Sep 17 00:00:00 2001 From: Mary Date: Fri, 24 Jun 2022 19:04:57 +0200 Subject: time: Make TimeZoneRule blittable and avoid copies (#3361) * time: Make TimeZoneRule blittable and avoid copies This drastically reduce overhead of using TimeZoneRule around the codebase. Effect on games is unknown * Add missing Box type * Ensure we clean the structure still This doesn't perform any copies * Address gdkchan's comments * Simplify Box --- .../Time/StaticService/ITimeZoneServiceForGlue.cs | 14 +++++++------- .../Time/StaticService/ITimeZoneServiceForPsc.cs | 19 +++++++++++-------- 2 files changed, 18 insertions(+), 15 deletions(-) (limited to 'Ryujinx.HLE/HOS/Services/Time/StaticService') diff --git a/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForGlue.cs b/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForGlue.cs index 98d4b617..265cec5d 100644 --- a/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForGlue.cs +++ b/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForGlue.cs @@ -2,7 +2,10 @@ using Ryujinx.Common.Logging; using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Services.Time.TimeZone; using Ryujinx.HLE.Utilities; +using Ryujinx.Memory; using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Text; namespace Ryujinx.HLE.HOS.Services.Time.StaticService @@ -100,15 +103,12 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService string locationName = StringUtils.ReadInlinedAsciiString(context.RequestData, 0x24); - ResultCode resultCode = _timeZoneContentManager.LoadTimeZoneRule(out TimeZoneRule rules, locationName); - - // Write TimeZoneRule if success - if (resultCode == ResultCode.Success) + using (WritableRegion region = context.Memory.GetWritableRegion(bufferPosition, Unsafe.SizeOf())) { - MemoryHelper.Write(context.Memory, bufferPosition, rules); - } + ref TimeZoneRule rules = ref MemoryMarshal.Cast(region.Memory.Span)[0]; - return resultCode; + return _timeZoneContentManager.LoadTimeZoneRule(ref rules, locationName); + } } [CommandHipc(100)] diff --git a/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForPsc.cs b/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForPsc.cs index 202099b0..24032999 100644 --- a/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForPsc.cs +++ b/Ryujinx.HLE/HOS/Services/Time/StaticService/ITimeZoneServiceForPsc.cs @@ -4,9 +4,12 @@ using Ryujinx.Cpu; using Ryujinx.HLE.HOS.Services.Time.Clock; using Ryujinx.HLE.HOS.Services.Time.TimeZone; using Ryujinx.HLE.Utilities; +using Ryujinx.Memory; using System; using System.Diagnostics; using System.IO; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace Ryujinx.HLE.HOS.Services.Time.StaticService { @@ -165,11 +168,11 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService using (MemoryStream timeZoneBinaryStream = new MemoryStream(temp)) { - result = _timeZoneManager.ParseTimeZoneRuleBinary(out TimeZoneRule timeZoneRule, timeZoneBinaryStream); - - if (result == ResultCode.Success) + using (WritableRegion region = context.Memory.GetWritableRegion(timeZoneRuleBufferPosition, Unsafe.SizeOf())) { - MemoryHelper.Write(context.Memory, timeZoneRuleBufferPosition, timeZoneRule); + ref TimeZoneRule rule = ref MemoryMarshal.Cast(region.Memory.Span)[0]; + + result = _timeZoneManager.ParseTimeZoneRuleBinary(ref rule, timeZoneBinaryStream); } } @@ -199,9 +202,9 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService throw new InvalidOperationException(); } - TimeZoneRule rules = MemoryHelper.Read(context.Memory, bufferPosition); + ReadOnlySpan rules = MemoryMarshal.Cast(context.Memory.GetSpan(bufferPosition, (int)bufferSize)); - ResultCode resultCode = _timeZoneManager.ToCalendarTime(rules, posixTime, out CalendarInfo calendar); + ResultCode resultCode = _timeZoneManager.ToCalendarTime(in rules[0], posixTime, out CalendarInfo calendar); if (resultCode == 0) { @@ -244,9 +247,9 @@ namespace Ryujinx.HLE.HOS.Services.Time.StaticService throw new InvalidOperationException(); } - TimeZoneRule rules = MemoryHelper.Read(context.Memory, inBufferPosition); + ReadOnlySpan rules = MemoryMarshal.Cast(context.Memory.GetSpan(inBufferPosition, (int)inBufferSize)); - ResultCode resultCode = _timeZoneManager.ToPosixTime(rules, calendarTime, out long posixTime); + ResultCode resultCode = _timeZoneManager.ToPosixTime(in rules[0], calendarTime, out long posixTime); if (resultCode == ResultCode.Success) { -- cgit v1.2.3