Skip to content

Commit

Permalink
perf: reduce heap allocation (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
kahojyun authored Jul 1, 2023
1 parent e9976a4 commit 2c28940
Show file tree
Hide file tree
Showing 14 changed files with 13 additions and 41 deletions.
1 change: 0 additions & 1 deletion examples/WaveGenDemo/WaveGenDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<ServerGarbageCollection>true</ServerGarbageCollection>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Qynit.Pulsewave/Envelope.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using CommunityToolkit.Diagnostics;

namespace Qynit.Pulsewave;
public record Envelope
public readonly record struct Envelope
{
public IPulseShape? Shape { get; }
public double Width { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Qynit.Pulsewave/EnvelopeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Qynit.Pulsewave;

public sealed record class EnvelopeInfo
public readonly record struct EnvelopeInfo
{
private readonly double _indexOffset;
public double IndexOffset
Expand Down
2 changes: 1 addition & 1 deletion src/Qynit.Pulsewave/Instruction.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
namespace Qynit.Pulsewave;
public record Instruction(string Name, IReadOnlyList<Channel> Channels);
public record Instruction;
11 changes: 1 addition & 10 deletions src/Qynit.Pulsewave/IqPair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@
using System.Runtime.CompilerServices;

namespace Qynit.Pulsewave;
public readonly record struct IqPair<T>
public readonly record struct IqPair<T>(T I, T Q)
where T : unmanaged, INumber<T>, ITrigonometricFunctions<T>
{
public static readonly IqPair<T> Zero = new(T.Zero, T.Zero);
public static readonly IqPair<T> One = new(T.One, T.Zero);
public static readonly IqPair<T> ImaginaryOne = new(T.Zero, T.One);

public T I { get; }
public T Q { get; }

public IqPair(T i, T q)
{
I = i;
Q = q;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static IqPair<T> FromPolarCoordinates(T magnitude, T phase)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Qynit.Pulsewave/Play.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Qynit.Pulsewave;
public record Play(
public sealed record Play(
IPulseShape PulseShape,
double TStart,
double Width,
Expand All @@ -8,4 +8,4 @@ public record Play(
double DragCoefficient,
double Frequency,
double Phase,
Channel Channel) : Instruction(nameof(Play), new[] { Channel });
Channel Channel) : Instruction;
4 changes: 2 additions & 2 deletions src/Qynit.Pulsewave/PulseList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static IReadOnlyList<BinItem> ApplyInfo(IReadOnlyList<BinItem> list, dou
: (IReadOnlyList<PulseList<T>.BinItem>)list.Select(item => new BinItem(timeOffset + item.Time, item.Amplitude * multiplier)).ToArray();
}

internal record BinInfo(Envelope Envelope, double Frequency);
internal readonly record struct BinInfo(Envelope Envelope, double Frequency);
internal readonly record struct PulseAmplitude(IqPair<T> Amplitude, IqPair<T> DragAmplitude)
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -136,7 +136,7 @@ internal readonly record struct PulseAmplitude(IqPair<T> Amplitude, IqPair<T> Dr
return new PulseAmplitude(left * right.Amplitude, left * right.DragAmplitude);
}
}
internal record BinItem(double Time, PulseAmplitude Amplitude);
internal readonly record struct BinItem(double Time, PulseAmplitude Amplitude);
internal class Builder
{
private readonly Dictionary<BinInfo, List<BinItem>> _items = new();
Expand Down
2 changes: 1 addition & 1 deletion src/Qynit.Pulsewave/SetFrequency.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
namespace Qynit.Pulsewave;
public record SetFrequency(double Frequency, double ReferenceTime, Channel Channel) : Instruction(nameof(SetFrequency), new[] { Channel });
public sealed record SetFrequency(double Frequency, double ReferenceTime, Channel Channel) : Instruction;
2 changes: 1 addition & 1 deletion src/Qynit.Pulsewave/SetPhase.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
namespace Qynit.Pulsewave;
public record SetPhase(double Phase, Channel Channel) : Instruction(nameof(SetPhase), new[] { Channel });
public sealed record SetPhase(double Phase, Channel Channel) : Instruction;
2 changes: 1 addition & 1 deletion src/Qynit.Pulsewave/ShiftFrequency.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
namespace Qynit.Pulsewave;
public record ShiftFrequency(double Frequency, double ReferenceTime, Channel Channel) : Instruction(nameof(ShiftFrequency), new[] { Channel });
public sealed record ShiftFrequency(double Frequency, double ReferenceTime, Channel Channel) : Instruction;
2 changes: 1 addition & 1 deletion src/Qynit.Pulsewave/ShiftPhase.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
namespace Qynit.Pulsewave;
public record ShiftPhase(double Phase, Channel Channel) : Instruction(nameof(ShiftPhase), new[] { Channel });
public sealed record ShiftPhase(double Phase, Channel Channel) : Instruction;
2 changes: 1 addition & 1 deletion src/Qynit.Pulsewave/SwapPhase.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
namespace Qynit.Pulsewave;
public record SwapPhase(double ReferenceTime, Channel Channel1, Channel Channel2) : Instruction(nameof(SwapPhase), new[] { Channel1, Channel2 });
public sealed record SwapPhase(double ReferenceTime, Channel Channel1, Channel Channel2) : Instruction;
2 changes: 0 additions & 2 deletions tests/Qynit.Pulsewave.Tests/EnvelopeInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public void Comparison_NotEqual()
var envelopeInfo = new EnvelopeInfo(offset, sampleRate);
var envelopeInfo2 = new EnvelopeInfo(offset2, sampleRate);
Assert.True(envelopeInfo != envelopeInfo2);
Assert.True(envelopeInfo != null);
Assert.True(null != envelopeInfo);
}

[Fact]
Expand Down
16 changes: 0 additions & 16 deletions tests/Qynit.Pulsewave.Tests/InstructionTests.cs

This file was deleted.

0 comments on commit 2c28940

Please sign in to comment.