sipsorcery icon indicating copy to clipboard operation
sipsorcery copied to clipboard

Reduce Memory allocations

Open weltmeyer opened this issue 1 year ago • 9 comments

Reduce Memory allocations on H264 Sender HotPath by reusing byte[] and using ArrayPool

weltmeyer avatar Oct 04 '24 20:10 weltmeyer

I feel overloading is better than refactoring the method since it is public, minimizing breaking changes.

Then use the optimized overload for any internal use.

ha-ves avatar Oct 05 '24 05:10 ha-ves

Thanks for the PR. it does look like a big improvement.

Along the same lines as @ha-ves would it be possible to have this somehow opt-in for a bedding in period? Maybe behind a pre-processor directive. It would be good to get some confidence that there are no side effect before throwing it into the wild.

sipsorcery avatar Oct 07 '24 21:10 sipsorcery

I am currently working on readding the older method signatures. do we have some tests to make sure, no side effects are there?

weltmeyer avatar Oct 07 '24 21:10 weltmeyer

Just heads up @weltmeyer , @sipsorcery I have a much larger work in the same vein here: https://github.com/BorgGames/sipsorcery

It gives 40MB/s data channel bandwidth vs 10MB/s on the same machine with the current master. It also includes BouncyCastle 2 changes. I am generally interested in trying to merge it, if you guys are OK to review a PR of this size.

It does introduce breaking API changes though. (In a way it is inevitable - if your app that consumes SIPSorcery overrides one of the classes that takes T[] to change some behavior, there's no way it could be supported when the base class switches to Span<T>)

lostmsu avatar Oct 07 '24 21:10 lostmsu

I am currently working on readding the older method signatures. do we have some tests to make sure, no side effects are there?

thre public api should now also function with the old calls - except the NAL parser, but i dont know if this needs to be public at all.

weltmeyer avatar Oct 07 '24 21:10 weltmeyer

I am currently working on readding the older method signatures. do we have some tests to make sure, no side effects are there?

I would suggest doing benchmarks since it's an optimization.

Basic Benchmark.NET should show enough comparison.

ha-ves avatar Oct 08 '24 07:10 ha-ves

Thanks for the PR!

There seems to be a build failure in the latest commit.

Severity	Code	Description	Project	File	Line	Suppression State	Details
Error (active)	CS0535	'SrtpTransformer' does not implement interface member 'IPacketTransformer.Transform(byte[], int, int)'	SIPSorcery (net461), SIPSorcery (net5.0), SIPSorcery (net6.0), SIPSorcery (net8.0), SIPSorcery (netcoreapp3.1), SIPSorcery (netstandard2.0), SIPSorcery (netstandard2.1)	C:\Dev\github\sipsorcery\src\net\DtlsSrtp\Transform\SrtpTransformer.cs	48		
Error (active)	CS0535	'SrtpTransformer' does not implement interface member 'IPacketTransformer.Transform(byte[], int, int)'	SIPSorcery (net461), SIPSorcery (net5.0), SIPSorcery (net6.0), SIPSorcery (net8.0), SIPSorcery (netcoreapp3.1), SIPSorcery (netstandard2.0), SIPSorcery (netstandard2.1)	C:\Dev\github\sipsorcery\src\net\DtlsSrtp\Transform\SrtpTransformer.cs	48		
Error (active)	CS0535	'SrtpTransformer' does not implement interface member 'IPacketTransformer.Transform(byte[])'	SIPSorcery (net461), SIPSorcery (net5.0), SIPSorcery (net6.0), SIPSorcery (net8.0), SIPSorcery (netcoreapp3.1), SIPSorcery (netstandard2.0), SIPSorcery (netstandard2.1)	C:\Dev\github\sipsorcery\src\net\DtlsSrtp\Transform\SrtpTransformer.cs	48		
Error (active)	CS0535	'SrtpTransformer' does not implement interface member 'IPacketTransformer.Transform(byte[])'	SIPSorcery (net461), SIPSorcery (net5.0), SIPSorcery (net6.0), SIPSorcery (net8.0), SIPSorcery (netcoreapp3.1), SIPSorcery (netstandard2.0), SIPSorcery (netstandard2.1)	C:\Dev\github\sipsorcery\src\net\DtlsSrtp\Transform\SrtpTransformer.cs	48		

sipsorcery avatar Oct 14 '24 21:10 sipsorcery

Thanks for the PR!

There seems to be a build failure in the latest commit.

Severity	Code	Description	Project	File	Line	Suppression State	Details
Error (active)	CS0535	'SrtpTransformer' does not implement interface member 'IPacketTransformer.Transform(byte[], int, int)'	SIPSorcery (net461), SIPSorcery (net5.0), SIPSorcery (net6.0), SIPSorcery (net8.0), SIPSorcery (netcoreapp3.1), SIPSorcery (netstandard2.0), SIPSorcery (netstandard2.1)	C:\Dev\github\sipsorcery\src\net\DtlsSrtp\Transform\SrtpTransformer.cs	48		
Error (active)	CS0535	'SrtpTransformer' does not implement interface member 'IPacketTransformer.Transform(byte[], int, int)'	SIPSorcery (net461), SIPSorcery (net5.0), SIPSorcery (net6.0), SIPSorcery (net8.0), SIPSorcery (netcoreapp3.1), SIPSorcery (netstandard2.0), SIPSorcery (netstandard2.1)	C:\Dev\github\sipsorcery\src\net\DtlsSrtp\Transform\SrtpTransformer.cs	48		
Error (active)	CS0535	'SrtpTransformer' does not implement interface member 'IPacketTransformer.Transform(byte[])'	SIPSorcery (net461), SIPSorcery (net5.0), SIPSorcery (net6.0), SIPSorcery (net8.0), SIPSorcery (netcoreapp3.1), SIPSorcery (netstandard2.0), SIPSorcery (netstandard2.1)	C:\Dev\github\sipsorcery\src\net\DtlsSrtp\Transform\SrtpTransformer.cs	48		
Error (active)	CS0535	'SrtpTransformer' does not implement interface member 'IPacketTransformer.Transform(byte[])'	SIPSorcery (net461), SIPSorcery (net5.0), SIPSorcery (net6.0), SIPSorcery (net8.0), SIPSorcery (netcoreapp3.1), SIPSorcery (netstandard2.0), SIPSorcery (netstandard2.1)	C:\Dev\github\sipsorcery\src\net\DtlsSrtp\Transform\SrtpTransformer.cs	48		

Sorry for that. it is in now :)

weltmeyer avatar Oct 14 '24 22:10 weltmeyer

Thanks, build is good now.

There are 4 unit test failures which I suspect are related to a new bug. The test all pass on he master branch.

image

sipsorcery avatar Oct 15 '24 20:10 sipsorcery

@weltmeyer I've opened a PR in #1212 to revert this PR as it's causing corruption on audio streams.

The easiest was to replicate the issue is to use the SIP examples:

  1. Start examples\SIPExamples\SIPCallServer using dontnet run
  2. Start the UserAgentClient examples\SIPExamples\UserAgentClient using dotnet run sip:[email protected]

With #1190 the stream is barely recognisable as music.

sipsorcery avatar Nov 03 '24 21:11 sipsorcery