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

[QUESTION] - Keyboard event loop is slower than old version? #21

Closed
tramhao opened this issue Nov 17, 2021 · 10 comments
Closed

[QUESTION] - Keyboard event loop is slower than old version? #21

tramhao opened this issue Nov 17, 2021 · 10 comments
Assignees
Labels
question Further information is requested

Comments

@tramhao
Copy link
Contributor

tramhao commented Nov 17, 2021

I'm using below configuration:

        EventListenerCfg::default()
            .default_input_listener(Duration::from_millis(50))
            .poll_timeout(Duration::from_millis(40))
            .tick_interval(Duration::from_secs(1)),

however, in such cases, the movement in treeview is slower than before. When I change the first value to 20, movement is ok, but cpu usage is around 0.7-1.0 %. and I haven't implement music player yet, just user interface.
In old version, I use tick rate 20ms and cpu usage is only 0.2%.

It's not a very big deal, but just asking as I don't know if my implementation is all right. Basically I copy from example.

@tramhao tramhao added the question Further information is requested label Nov 17, 2021
@veeso
Copy link
Owner

veeso commented Nov 17, 2021

Hi,

you should remove the sleep in the main loop sleep(Duration::from_millis(20));. The tick() method of Application already sleeps for the provided poll_timeout time.

So try to remove the sleep and to set poll_timeout to 20ms.

@tramhao
Copy link
Contributor Author

tramhao commented Nov 17, 2021

I copy the main loop also from example so there is no sleep inside. I am on mobile so probably did not see the whole picture. Any ideas?

@veeso
Copy link
Owner

veeso commented Nov 17, 2021

Oh, maybe you didn't push yet. I was looking at this main loop https://github.com/tramhao/termusic/blob/tui-realm1.0/src/app.rs.
Anyway try to set the poll timeout to 20. 50ms for the input listener is anyway kinda high, so try to set lower.

You could also try to use PollStrategy::UpTo(2) or 3 for the tick method, since it may work better for scrolling.

@tramhao
Copy link
Contributor Author

tramhao commented Nov 18, 2021

Sorry that file is not in working tree. Just left for reference. I'll try it and get back to you.

@tramhao
Copy link
Contributor Author

tramhao commented Nov 18, 2021

After trying several combination, and below is best for scrolling.

      EventListenerCfg::default()
            .default_input_listener(Duration::from_millis(20))
            .poll_timeout(Duration::from_millis(40))
            .tick_interval(Duration::from_secs(1)),

Meanwhile, the cpu usage is still higher than old version. Not a big deal though.

@tramhao
Copy link
Contributor Author

tramhao commented Nov 18, 2021

I tried PollStrategy::UpTo(2) and 3, and it seems jumping and not consecutive scroll.

@veeso
Copy link
Owner

veeso commented Nov 18, 2021

Cpu usage will always be higher due to a dedicated thread for input listener.
For concurrency reasons the input listener interval should be less than the poll timeout, half is best.

In my application I also give the user the possibility to specify the tick interval in order to allow them to find the best solution. For example I have a good pc, so I prefer best performance, but if someone has a potato pc I give them the possibility to set the ticks from CLI options in order to have worse performance, but less cpu usage.

Another solution could be, that since termusic is more like a background application for most of the time I guess, you could implement a sleep in your main loop to perform only when there hasn't been any incoming input event for a certain amount of time.

@tramhao
Copy link
Contributor Author

tramhao commented Nov 19, 2021

It's not a very big difference so I think I'll keep it simple. Thanks for the sleep advice!

@tramhao tramhao closed this as completed Nov 19, 2021
@veeso
Copy link
Owner

veeso commented Nov 20, 2021

I've just released tui-realm 1.0.1, which should reduce cpu usage in input listener.

@tramhao
Copy link
Contributor Author

tramhao commented Nov 21, 2021

Thanks I tested, and it's lower than before. About 0.3% in my laptop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants