peachpiecompiler / peachpie

PeachPie - the PHP compiler and runtime for .NET and .NET Core
https://www.peachpie.io
Apache License 2.0
2.33k stars 202 forks source link

improve the performance of 'fread' for tcp socket #1038

Closed avriltank closed 2 years ago

avriltank commented 2 years ago

I found the .net recv becomes very slow when the buffer size is big,it mallocs memory for the byte[] array,but the gc do not free the memory soon

jakubmisek commented 2 years ago

we try to avoid "unsafe" code, isn't there an issue on dotnet for this? Can we just make the buffer smaller?

avriltank commented 2 years ago

There is no issue,the Socket.Receive method is very slowly when the buffer size is big.

avriltank commented 2 years ago

I test in my project,the unsafe method can improve Almost 2.5x performance

jakubmisek commented 2 years ago

We can make the "length" smaller and receive it subsequently, or something. This is an extremely hacky solution hardcoded just for a single case.

PeachPie provides this PhpStream.Read method that should handle such cases. We can't put it just to the fread function.

avriltank commented 2 years ago

Yes,you are right!this is not a good idea for peachpie code,i try to find another solution.

jakubmisek commented 2 years ago

I test in my project,the unsafe method can improve Almost 2.5x performance

it is possible, but we can't implement it like this.

I would recommend implementing a C# method somewhere in your project, for example

PhpString fread_fast(Context ctx, PhpResource handle, int length) { ... }

Or we would need to find a "cleaner" workaround, so we can test it and maintain it.

avriltank commented 2 years ago

Yes,that is a good idea,thanks for your reply