Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid while (!_closeHandler) { ... } in XBeeFrameHandler.Start() #49

Open
nicolaiw opened this issue May 6, 2019 · 7 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@nicolaiw
Copy link
Contributor

nicolaiw commented May 6, 2019

This might be resolved with a TaskCompletionSource. This might be a breaking change because we have to change the Start() method to return a Task instead of void.

Here is an example of a refactored while loop with a TaskCompletionSource:
https://github.com/zigbeenet/ZigbeeNet/blob/master/src/ZigBeeNet/Transaction/ZigBeeTransaction.cs#L82

@nicolaiw nicolaiw added the enhancement New feature or request label May 6, 2019
@andreasfedermann
Copy link
Contributor

Hello @nicolaiw,

I will refactor this as soon as possible. I think there are some more refactoring actions necessary. Some code is not very c# style I think.
First of all I will check all existing while loops and refactor this if appropriate.

@nicolaiw
Copy link
Contributor Author

nicolaiw commented May 8, 2019

This issue is not just related to Xbee

image

This is the CPU usage of the PlayGround app with a TIC2531 while waiting for input. So you are right .. removing or avoid while loops where ever we can should be our next Task :D

nicolaiw added a commit that referenced this issue May 20, 2019
@nicolaiw
Copy link
Contributor Author

nicolaiw commented May 20, 2019

perf

I could reduce the CPU usage (same machine) by refactor the while loop which reads from the serial port. The previous versions always checked for BytesToRead in a while loop which is not necessary and very inefficient. The refactored version waits until data available through ReadByte(). After that it reads the rest of the data. I think it could be done even more elegant. Instead of using ReadByte() to wait for data we might use the SerialPort.Read(byte[] buffer, int offset, int count) methode to read the header, parse it for the payload length and read the payload. I'm not sure but this might be Hardware/Dongle dependent (size of the header). I guess this won't be decrease the cpu usage but might be more elegant.

About 34% CPU usage still seems too high. I think there is another loop or something like that which utilizes the CPU.

@Mr-Markus @andreasfedermann
Maybe you could help me to find those places in the code.

@andreasfedermann
Could you please pull the latest version and try out if this version still works with your Xbee hardware ?

Best regards

@nicolaiw
Copy link
Contributor Author

@nicolaiw
Copy link
Contributor Author

nicolaiw commented May 20, 2019

perf

Looks much better to me and that's about what I would expect when there is no data to read :)

best regards

@andreasfedermann
Copy link
Contributor

Hi Nicolai,

sorry for being offline that long but I’ve moved. So I had less time to write some code. My aim for this week is to test your code changes.

Best regards!

@Mr-Markus
Copy link
Owner

Hi @andreasfedermann,
good luck with your new home. I hope you will use this library for your smart home solution there :-)

Best,
Markus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants