Partially revert "Favor record structs when there are four or fewer elements."

This reverts commit f5d49515c4eddfb415903a89d70654cf9b6de299.
This commit is contained in:
atravita-mods 2022-08-18 20:51:45 -04:00 committed by Jesse Plamondon-Willard
parent 627100509c
commit d29c01b815
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
4 changed files with 22 additions and 24 deletions

View File

@ -40,7 +40,7 @@ namespace StardewModdingAPI.Framework.Content
this.GetPatchBounds(ref sourceArea, ref targetArea, source.Width, source.Height); this.GetPatchBounds(ref sourceArea, ref targetArea, source.Width, source.Height);
// get the pixels for the source area // get the pixels for the source area
Color[] trimmedSourceData; Color[] sourceData;
{ {
int areaX = sourceArea.Value.X; int areaX = sourceArea.Value.X;
int areaY = sourceArea.Value.Y; int areaY = sourceArea.Value.Y;
@ -49,42 +49,40 @@ namespace StardewModdingAPI.Framework.Content
if (areaX == 0 && areaY == 0 && areaWidth == source.Width && areaHeight == source.Height) if (areaX == 0 && areaY == 0 && areaWidth == source.Width && areaHeight == source.Height)
{ {
trimmedSourceData = source.Data; sourceData = source.Data;
this.PatchImageImpl(trimmedSourceData, source.Width, source.Height, sourceArea.Value, targetArea.Value, patchMode); this.PatchImageImpl(sourceData, source.Width, source.Height, sourceArea.Value, targetArea.Value, patchMode);
} }
else else
{ {
int pixelCount = areaWidth * areaHeight; int pixelCount = areaWidth * areaHeight;
trimmedSourceData = ArrayPool<Color>.Shared.Rent(pixelCount); sourceData = ArrayPool<Color>.Shared.Rent(pixelCount);
// shortcut! If I want a horizontal slice of the texture if (areaX == 0 && areaWidth == source.Width)
// I can copy the whole array in one pass
// Likely ~uncommon but Array.Copy significantly benefits
// from being able to do this.
if (areaWidth == source.Width && areaX == 0)
{ {
int sourceIndex = areaY * source.Width; // shortcut copying because the area to copy is contiguous. This is
int targetIndex = 0; // probably uncommon, but Array.Copy benefits a lot.
int sourceIndex = areaY * areaWidth;
int targetIndex = 0;
Array.Copy(source.Data, sourceIndex, sourceData, targetIndex, areaWidth);
Array.Copy(source.Data, sourceIndex, trimmedSourceData, targetIndex, pixelCount);
} }
else else
{ {
// copying line-by-line // slower copying, line by line
// Array.Copy isn't great at small scale
for (int y = areaY, maxY = areaY + areaHeight; y < maxY; y++) for (int y = areaY, maxY = areaY + areaHeight; y < maxY; y++)
{ {
int sourceIndex = (y * source.Width) + areaX; int sourceIndex = (y * source.Width) + areaX;
int targetIndex = (y - areaY) * areaWidth; int targetIndex = (y - areaY) * areaWidth;
Array.Copy(source.Data, sourceIndex, trimmedSourceData, targetIndex, areaWidth); Array.Copy(source.Data, sourceIndex, sourceData, targetIndex, areaWidth);
} }
} }
// apply // apply
this.PatchImageImpl(trimmedSourceData, source.Width, source.Height, sourceArea.Value, targetArea.Value, patchMode); this.PatchImageImpl(sourceData, source.Width, source.Height, sourceArea.Value, targetArea.Value, patchMode);
// return // return
ArrayPool<Color>.Shared.Return(trimmedSourceData); ArrayPool<Color>.Shared.Return(sourceData);
} }
} }
} }
@ -176,9 +174,9 @@ namespace StardewModdingAPI.Framework.Content
// merge pixels // merge pixels
for (int i = 0; i < pixelCount; i++) for (int i = 0; i < pixelCount; i++)
{ {
// should probably benchmark this... // ref locals here? Not sure.
ref Color above = ref sourceData[i]; Color above = sourceData[i];
ref Color below = ref mergedData[i]; Color below = mergedData[i];
// shortcut transparency // shortcut transparency
if (above.A < MinOpacity) if (above.A < MinOpacity)

View File

@ -8,5 +8,5 @@ namespace StardewModdingAPI.Framework.Content
/// <param name="Priority">If there are multiple edits that apply to the same asset, the priority with which this one should be applied.</param> /// <param name="Priority">If there are multiple edits that apply to the same asset, the priority with which this one should be applied.</param>
/// <param name="OnBehalfOf">The content pack on whose behalf the edit is being applied, if any.</param> /// <param name="OnBehalfOf">The content pack on whose behalf the edit is being applied, if any.</param>
/// <param name="ApplyEdit">Apply the edit to an asset.</param> /// <param name="ApplyEdit">Apply the edit to an asset.</param>
internal readonly record struct AssetEditOperation(IModMetadata Mod, AssetEditPriority Priority, IModMetadata? OnBehalfOf, Action<IAssetData> ApplyEdit); internal record AssetEditOperation(IModMetadata Mod, AssetEditPriority Priority, IModMetadata? OnBehalfOf, Action<IAssetData> ApplyEdit);
} }

View File

@ -8,5 +8,5 @@ namespace StardewModdingAPI.Framework.Content
/// <param name="Priority">If there are multiple loads that apply to the same asset, the priority with which this one should be applied.</param> /// <param name="Priority">If there are multiple loads that apply to the same asset, the priority with which this one should be applied.</param>
/// <param name="OnBehalfOf">The content pack on whose behalf the asset is being loaded, if any.</param> /// <param name="OnBehalfOf">The content pack on whose behalf the asset is being loaded, if any.</param>
/// <param name="GetData">Load the initial value for an asset.</param> /// <param name="GetData">Load the initial value for an asset.</param>
internal readonly record struct AssetLoadOperation(IModMetadata Mod, IModMetadata? OnBehalfOf, AssetLoadPriority Priority, Func<IAssetInfo, object> GetData); internal record AssetLoadOperation(IModMetadata Mod, IModMetadata? OnBehalfOf, AssetLoadPriority Priority, Func<IAssetInfo, object> GetData);
} }

View File

@ -172,7 +172,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
where T : notnull where T : notnull
{ {
// find matching loader // find matching loader
AssetLoadOperation loader = default; AssetLoadOperation? loader = null;
if (loadOperations?.Count > 0) if (loadOperations?.Count > 0)
{ {
if (!this.AssertMaxOneRequiredLoader(info, loadOperations, out string? error)) if (!this.AssertMaxOneRequiredLoader(info, loadOperations, out string? error))
@ -183,7 +183,7 @@ namespace StardewModdingAPI.Framework.ContentManagers
loader = loadOperations.OrderByDescending(p => p.Priority).FirstOrDefault(); loader = loadOperations.OrderByDescending(p => p.Priority).FirstOrDefault();
} }
if (loader.Mod == null) // aka, this is default. if (loader == null)
return null; return null;
// fetch asset from loader // fetch asset from loader