Skip to content

Commit

Permalink
Made some more minor optimizations to allocations and inlining.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Oct 23, 2023
1 parent 545c9e7 commit 904eea5
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 15 deletions.
5 changes: 5 additions & 0 deletions FinModelUtility/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<Project>
<PropertyGroup>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<PropertyGroup Condition="$(MSBuildProjectName.Contains('Tests'))">
<CollectCoverage>true</CollectCoverage>
<CoverletOutput>TestResults/</CoverletOutput>
Expand Down
2 changes: 1 addition & 1 deletion FinModelUtility/Fin/Fin.Ui/src/rendering/gl/GlTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static unsafe void UniformMatrix4(int location, Matrix4x4 matrix) {
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void UniformMatrix4s(int location, Matrix4x4[] matrices) {
public static unsafe void UniformMatrix4s(int location, ReadOnlySpan<Matrix4x4> matrices) {
fixed (float* ptr = &(matrices[0].M11)) {
GL.UniformMatrix4(location, matrices.Length, false, ptr);
}
Expand Down
8 changes: 3 additions & 5 deletions FinModelUtility/Fin/Fin/src/image/formats/BImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public override unsafe bool Equals(object? obj) {
out var otherB,
out var otherA);

if (thisR != otherR ||
thisG != otherG ||
if (thisR != otherR ||
thisG != otherG ||
thisB != otherG ||
thisA != otherA) {
match = false;
Expand Down Expand Up @@ -125,9 +125,7 @@ public override unsafe int GetHashCode() {
var span = new Span<TPixel>(ptr, pixelCount).AsBytes();

var hash = FluentHash.Start();
for (var i = 0; i < span.Length; ++i) {
hash.With(span[i]);
}
hash.With(span);

this.cachedHash_ = hash;
return hash;
Expand Down
24 changes: 22 additions & 2 deletions FinModelUtility/Fin/Fin/src/util/hash/FluentHash.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace fin.util.hash {
using System;
using System.Runtime.CompilerServices;

namespace fin.util.hash {
public struct FluentHash {
public int Hash { get; private set; }
private readonly int primeCoefficient_;
Expand All @@ -8,21 +11,38 @@ private FluentHash(int startingPrimeHash, int primeCoefficient) {
this.primeCoefficient_ = primeCoefficient;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static FluentHash Start() => FluentHash.Start(17, 23);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static FluentHash Start(int startingPrimeHash,
int primeCoefficient)
=> new FluentHash(startingPrimeHash, primeCoefficient);
=> new(startingPrimeHash, primeCoefficient);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public FluentHash With<T>(T other) where T : notnull {
this.Hash = this.Hash * this.primeCoefficient_ + other.GetHashCode();
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public FluentHash With(ReadOnlySpan<byte> other) {
var hash = this.Hash;
for (var i = 0; i < other.Length; ++i) {
hash = hash * this.primeCoefficient_ + other[i];
}

this.Hash = hash;
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator int(FluentHash d) => d.Hash;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() => this.Hash;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object? other)
=> other?.GetHashCode() == this.GetHashCode();
}
Expand Down
1 change: 0 additions & 1 deletion FinModelUtility/Formats/F3dzex2/F3dzex2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</PropertyGroup>

<PropertyGroup>
<DebugType>embedded</DebugType>
<SelfContained>true</SelfContained>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace modl.schema.anim {
internal static class WeirdFloatMath {
public static class WeirdFloatMath {
public static ulong Concat44(uint first, uint second)
=> ((ulong) first << 32) | second;

Expand Down
10 changes: 8 additions & 2 deletions FinModelUtility/Games/SuperMario64/SuperMario64/src/BitLogic.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
namespace sm64 {
using System.Runtime.CompilerServices;

namespace sm64 {
public static class BitLogic {
public static uint BytesToInt(byte[] b, int offset, int length) {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint BytesToInt(
ReadOnlySpan<byte> b,
int offset,
int length) {
switch (length) {
case 1: return b[0 + offset];
case 2: return (uint) (b[0 + offset] << 8 | b[1 + offset]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
</PropertyGroup>

<PropertyGroup>
<DebugType>full</DebugType>
<SelfContained>true</SelfContained>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
Expand Down

0 comments on commit 904eea5

Please sign in to comment.