dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.04k stars 1.73k forks source link

Maui receive event of EASessionController is lower than Xamarin. #23388

Closed Jiang-Junjie closed 2 months ago

Jiang-Junjie commented 3 months ago

Description

My prvious project(Xamarin Forms) is using EASessionController refers to Apple's sample:

and C# code now as follows:

        public override void HandleEvent(NSStream theStream, NSStreamEvent streamEvent)
        {

            switch (streamEvent)
            {

                case NSStreamEvent.None:
                    Console.WriteLine("StreamEventNone");
                    break;
                case NSStreamEvent.HasBytesAvailable:
                    //Console.WriteLine("StreamEventHasBytesAvailable1");
                    try
                    {
                        _pool.Release();
                    }
                    catch (Exception error)
                    {
                        //Console.Write("_pool.Release()...{0}", error.Message);
                    }
                    break;
                case NSStreamEvent.HasSpaceAvailable:
                    Console.WriteLine("StreamEventHasSpaceAvailable");
                    WriteDirect();
                    // Do write operations to the device here
                    break;
                case NSStreamEvent.OpenCompleted:
                    Console.WriteLine("StreamEventOpenCompleted");
                    break;
                case NSStreamEvent.ErrorOccurred:
                    Console.WriteLine("StreamEventErroOccurred");
                    break;
                case NSStreamEvent.EndEncountered:
                    Console.WriteLine("StreamEventEndEncountered");
                    break;
                default:
                    Console.WriteLine("Stream present but no event");
                    break;

            }
        }

The different is NSStreamEvent.HasSpaceAvailable between XF and Maui.

XF as follows:

2024-07-02 08:45:37.369 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable===rt==1037=sendindex===173179
2024-07-02 08:45:37.369 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable
2024-07-02 08:45:37.371 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable===rt==1037=sendindex===172142
2024-07-02 08:45:37.371 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable
2024-07-02 08:45:37.373 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable===rt==1037=sendindex===171105
2024-07-02 08:45:37.373 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable
2024-07-02 08:45:37.374 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable===rt==1037=sendindex===170068
2024-07-02 08:45:37.374 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable
2024-07-02 08:45:37.376 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable===rt==1037=sendindex===169031
2024-07-02 08:45:37.376 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable
2024-07-02 08:45:37.377 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable===rt==1037=sendindex===167994
2024-07-02 08:45:37.377 XamarinUI.iOS[2747:1739743] StreamEventHasSpaceAvailable

Maui as follows:

2024-07-02 09:09:48.147 PalmUI[2797:2103329] StreamEventHasSpaceAvailable
2024-07-02 09:09:48.209 PalmUI[2797:2103329] StreamEventHasSpaceAvailable===rt==1037=sendindex===171105
2024-07-02 09:09:48.209 PalmUI[2797:2103329] StreamEventHasSpaceAvailable
2024-07-02 09:09:48.271 PalmUI[2797:2103329] StreamEventHasSpaceAvailable===rt==1037=sendindex===170068
2024-07-02 09:09:48.271 PalmUI[2797:2103329] StreamEventHasSpaceAvailable
2024-07-02 09:09:48.333 PalmUI[2797:2103329] StreamEventHasSpaceAvailable===rt==1037=sendindex===169031
2024-07-02 09:09:48.333 PalmUI[2797:2103329] StreamEventHasSpaceAvailable
2024-07-02 09:09:48.388 PalmUI[2797:2103492] no device to send!
2024-07-02 09:09:48.395 PalmUI[2797:2103329] StreamEventHasSpaceAvailable===rt==1037=sendindex===167994
2024-07-02 09:09:48.395 PalmUI[2797:2103329] StreamEventHasSpaceAvailable
2024-07-02 09:09:48.458 PalmUI[2797:2103329] StreamEventHasSpaceAvailable===rt==1037=sendindex===166957
2024-07-02 09:09:48.458 PalmUI[2797:2103329] StreamEventHasSpaceAvailable
2024-07-02 09:09:48.519 PalmUI[2797:2103329] StreamEventHasSpaceAvailable===rt==1037=sendindex===165920

You will see that the same code, however each event of HasSpaceAvailable wll use more time than XF. The interval between the signals is more than 50ms.

This confused me, because after I plugged in the USB, it would take 5S longer to respond than before, because the data size needed to be sent for the first connection was 171105 and the packet size was 1037.

This has greatly affected my experience of using the program, and I have not found any other solution to solve this problem, including this one issue.

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

Unknown/Other

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

7.0.101

Affected platforms

iOS

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

jfversluis commented 3 months ago

@rolfbjarne ?

rolfbjarne commented 3 months ago

If I understand correctly, the problem is that your code is executing slower with MAUI than it did with Xamarin.Forms.

Do you see the same behavior with a Release build? Is it faster than a Debug build, or is it just as slow?

Jiang-Junjie commented 3 months ago

If I understand correctly, the problem is that your code is executing slower with MAUI than it did with Xamarin.Forms.

Do you see the same behavior with a Release build? Is it faster than a Debug build, or is it just as slow?

Yes, understand correctly. The problem was found in our release version. Therefore the release build not works faster. The release mode could make mostly process faster besides the EA function. I have no idea to make it faster now. If you could found other solutions for us to try, we will appreciate very much.

rolfbjarne commented 3 months ago

Can you try adding this to your csproj:

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <MtouchExtraArgs>--require-pinvoke-wrappers:true</MtouchExtraArgs>
    <UseInterpreter>false</UseInterpreter>
</PropertyGroup>

and see if it makes a difference?

Jiang-Junjie commented 3 months ago

Can you try adding this to your csproj:

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <MtouchExtraArgs>--require-pinvoke-wrappers:true</MtouchExtraArgs>
    <UseInterpreter>false</UseInterpreter>
</PropertyGroup>

and see if it makes a difference?

Thanks for sharing this, however it makes the application crashes when tapping it to start.

There is a little difference in my project that MtouchExtraArgs already exists following configuration:

<MtouchExtraArgs>--require-pinvoke-wrappers:true --optimize=experimental-xforms-product-type -cxx -gcc_flags ...<MtouchExtraArgs>

I just put --require-pinvoke-wrappers:true in the front of my current configuration.

rolfbjarne commented 3 months ago

Thanks for sharing this, however it makes the application crashes when tapping it to start.

It sounds like your app requires the interpreter for some reason. The interpreter is slower than AOT-compiled code, but it shouldn't make this much of a difference.

So we can try to enable the interpreter, but AOT-compile every assembly, which should hopefully make your app not crash at launch:

<PropertyGroup>
    <MtouchInterpreter>-all</MtouchInterpreter>
</PropertyGroup>

Can you see if that works / changes anything?

Jiang-Junjie commented 2 months ago

Thanks for sharing this, however it makes the application crashes when tapping it to start.

It sounds like your app requires the interpreter for some reason. The interpreter is slower than AOT-compiled code, but it shouldn't make this much of a difference.

So we can try to enable the interpreter, but AOT-compile every assembly, which should hopefully make your app not crash at launch:

<PropertyGroup>
    <MtouchInterpreter>-all</MtouchInterpreter>
</PropertyGroup>

Can you see if that works / changes anything?

Unfortunatly, it also not works as the same phenomenon. In addition, it will need more time to finish the build process almost above 6 mins.

As you said that, our project need UseInterpreter, and it exists in .csproj:

<PropertyGroup>
     ...
     <TargetFramework>net7.0-ios</TargetFramework>
     <OutputType>Exe</OutputType>
     <RootNamespace>PalmUI</RootNamespace>
     <UseMaui>true</UseMaui>
     <SingleProject>true</SingleProject>
     <ImplicitUsings>enable</ImplicitUsings>
     <UseInterpreter>true</UseInterpreter>
    ...
</PropertyGroup>
Jiang-Junjie commented 2 months ago

@rolfbjarne Finally, we have used <MtouchInterpreter>-all</MtouchInterpreter> to solve this problem, and the crashed because some code. It's strange that these code work on debug mode. Thans for sharing the solution for us, it saves our project! I have already closed this case when you have a look at this.