toolong icon indicating copy to clipboard operation
toolong copied to clipboard

Fix Very high CPU usage when piping

Open mckaymatt opened this issue 11 months ago • 2 comments

Insert a small sleep when polling stdin

Resolves https://github.com/Textualize/toolong/issues/48

Here is how to reproduce this issue

$ tl --version
tl, version 1.4.0
$ echo '{"foo": "bar"}' > /tmp/log.json
$ cat  /tmp/log.json | tl

This is what happens in main now. 100% CPU usage when piping Screenshot 2025-01-23 at 11 34 21 AM

I added a small sleep when there is nothing to read. I tested 1 ms and 5 ms.

1 ms used about 2% cpu. Screenshot 2025-01-23 at 11 34 47 AM

5 ms used about 0.7% cpu. Screenshot 2025-01-23 at 11 35 14 AM

I would guess 5 ms will not be very noticable, but if it is we can go down to 1.

Testing

I tried piping in a 40MB file and did not notice any difference in performance with or without the sleep.

mckaymatt avatar Jan 23 '25 17:01 mckaymatt

@willmcgugan Can you please take a look

mattviasat avatar Feb 07 '25 18:02 mattviasat

Works for me :)

nickma82 avatar Nov 06 '25 12:11 nickma82