Skip to content

Conversation

@H4ad
Copy link
Contributor

@H4ad H4ad commented May 20, 2025

Hey, here's some changes to improve memory allocation of SendAudioFrame, I tried to minimize changes, there's a lot of things that we can improve but I decided to start making small changes.

Method Job Mean Error StdDev Exceptions Gen0 Allocated
SendData New Implementation 6.515 ms 0.0619 ms 0.0579 ms 874.0000 117.1875 1.95 MB
SendData Old Implementation 6.426 ms 0.0380 ms 0.0355 ms 874.0000 242.1875 3.97 MB

Benchmark:

[MemoryDiagnoser]
[ThreadingDiagnoser]
[ExceptionDiagnoser]
public class AudioStreamBenchmark
{
    private const int N = 10000;
    private readonly AudioStream audioStream;
    private byte[] _buffer;

    public AudioStreamBenchmark()
    {
        audioStream = new AudioStream(new RtpSessionConfig
        {
            BindAddress = IPAddress.Loopback,
            BindPort = 0,
            IsMediaMultiplexed = false,
            IsRtcpMultiplexed = false,
            RtpPortRange = new(10000, 12999),
            RtpSecureMediaOption = RtpSecureMediaOptionEnum.None
        }, 0);
        audioStream.MediaType = SDPMediaTypesEnum.audio;
        audioStream.LocalTrack= new MediaStreamTrack(SDPMediaTypesEnum.audio, true, [new SDPAudioVideoMediaFormat(SDPMediaTypesEnum.audio, 0, "test")], MediaStreamStatusEnum.SendRecv);
        audioStream.RemoteTrack = new MediaStreamTrack(SDPMediaTypesEnum.audio, true, [new SDPAudioVideoMediaFormat(SDPMediaTypesEnum.audio, 0, "test")], MediaStreamStatusEnum.SendRecv);
        audioStream.AddRtpChannel(new RTPChannel(true, IPAddress.Loopback, 0, new(10000, 12999)));
        audioStream.DestinationEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
        _buffer = new byte[1 * 1024 * 1024];
    }

    [Benchmark]
    public void SendData() => audioStream.SendAudio(0, _buffer);
}

@sipsorcery
Copy link
Member

Thanks for the PR.

I'm getting a compiler error buildinghte RTPPacket class.

'ArraySegment<byte>' does not contain a definition for 'Slice' and no accessible extension method 'Slice' accepting a first argument of type 'ArraySegment<byte>' could be found (are you missing a using directive or an assembly reference?)

@H4ad H4ad force-pushed the perf/reduce-memory-allocation branch from d5198e5 to 59905c7 Compare May 21, 2025 23:01
@H4ad
Copy link
Contributor Author

H4ad commented May 21, 2025

I think it will compiles now, I'm using Rider on Linux, I was not even able to build because some issues on Assembly, so I had to just remove the other frameworks and keep the .NET8.

@paulomorgado
Copy link
Contributor

I think it will compiles now, I'm using Rider on Linux, I was not even able to build because some issues on Assembly, so I had to just remove the other frameworks and keep the .NET8.

Have you tried Microsoft.NETFramework.ReferenceAssemblies.net481? See .NET Framework Targeting Pack Nuget Packages.

@paulomorgado
Copy link
Contributor

I think I came up with something in this branch.

Adding that and a few more things that depend on #1414 allowed me to reduce to almost half the allocations on my service serving 1 video and that doesn't show significant increase with the number of clients.

@H4ad H4ad force-pushed the perf/reduce-memory-allocation branch from 59905c7 to 9d136a4 Compare May 26, 2025 19:54
@H4ad
Copy link
Contributor Author

H4ad commented May 26, 2025

Have you tried Microsoft.NETFramework.ReferenceAssemblies.net481? See .NET Framework Targeting Pack Nuget Packages.

@paulomorgado I tried but I'm still getting for each target framework.

0>.NETCoreApp,Version=v9.0.AssemblyAttributes.cs(4,12): Error CS0579 : Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' attribute
0>SIPSorcery.AssemblyInfo.cs(13,12): Error CS0579 : Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute
0>SIPSorcery.AssemblyInfo.cs(14,12): Error CS0579 : Duplicate 'System.Reflection.AssemblyConfigurationAttribute' attribute
0>SIPSorcery.AssemblyInfo.cs(15,12): Error CS0579 : Duplicate 'System.Reflection.AssemblyCopyrightAttribute' attribute
0>SIPSorcery.AssemblyInfo.cs(16,12): Error CS0579 : Duplicate 'System.Reflection.AssemblyDescriptionAttribute' attribute
0>SIPSorcery.AssemblyInfo.cs(18,12): Error CS0579 : Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute
0>SIPSorcery.AssemblyInfo.cs(19,12): Error CS0579 : Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute
0>SIPSorcery.AssemblyInfo.cs(20,12): Error CS0579 : Duplicate 'System.Reflection.AssemblyProductAttribute' attribute
0>SIPSorcery.AssemblyInfo.cs(21,12): Error CS0579 : Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute
0>SIPSorcery.AssemblyInfo.cs(22,12): Error CS0579 : Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute

It's probably caused by some weird issue with asdf or some misconfiguration :/

@paulomorgado
Copy link
Contributor

@H4ad,

I tried this on Windows and had no isssues:

<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net462" Version="1.0.3">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

If it still gives you issues, try this:

<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
  <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies.net462" Version="1.0.3">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  </PackageReference>
</ItemGroup>

@paulomorgado
Copy link
Contributor

I'm working on #1422 (that builds upon #1414) and I think it has a more consistent API. Can you have a go at that one, @H4ad?

@sipsorcery sipsorcery merged commit ff41b1f into sipsorcery-org:master Jun 1, 2025
@H4ad H4ad deleted the perf/reduce-memory-allocation branch June 1, 2025 10:33
ispysoftware pushed a commit to ispysoftware/sipsorcery that referenced this pull request Jun 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants