[Bug]: Using certain tools causes performance drops until next bake
Fix status:
- [x] Path eraser
- [x] Lasso select / rectangle select
- [ ] Eraser
- [ ] Undo
- [ ] Redo
What happened?
Steps to reproduce:
- Open a large or intricate file, e.g. lots of handwriting. Here's a file that is big enough to make this issue noticeable: concepts.bfly.txt
- Use the hand tool to move the canvas
- Use the pen tool to write a word, notice that it works pretty well
- Use one of the following tools: Path eraser or eraser (make sure to actually erase something), undo, lasso select tool (and then move the selection)
- Without moving the canvas, switch back to the pen tool and try to write the same word, notice that it has significant lag and the stroke details are not captured well
- Move the canvas a little bit with the hand tool
- Try writing the word again, and it should work well again now
Here's a video showing the issue. In this video, I write the same thing 3 times:
- After opening the file (good)
- After using the path eraser (bad)
- After using the hand tool (good again)
Interestingly, you can actually see the lag start before I start drawing the second one (around 0:08), because you can see the path eraser cursor start updating very slowly as I move my pen before I start drawing again. So the lag starts immediately after lifting up the pen after using the path eraser (the laggy cursor movement is me moving the pen close to the screen but not touching it)
https://github.com/user-attachments/assets/794bd055-ddcd-42d0-b9a8-13f8e307c096
Semi-related to #667
Version
2.3.2-beta.3
Platform
Android
Relevant log output
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
Hmm interesting... The thing is: Baking cannot be put in a different thread sadly because of flutter restrictions. But this looks like there is a bug inside the app that the app wants to fully bake the canvas where it shouldnt
I'm off to bed, but so far I've tracked it down to this line:
https://github.com/LinwoodDev/Butterfly/blob/c1ec2168805dc81e3cea7dc6b0b6f93fd6de3ed5/app/lib/cubits/current_index.dart#L1194
which is called from
https://github.com/LinwoodDev/Butterfly/blob/c1ec2168805dc81e3cea7dc6b0b6f93fd6de3ed5/app/lib/bloc/document_bloc.dart#L1044
which is called from
https://github.com/LinwoodDev/Butterfly/blob/c1ec2168805dc81e3cea7dc6b0b6f93fd6de3ed5/app/lib/bloc/document_bloc.dart#L348
which is triggered from
https://github.com/LinwoodDev/Butterfly/blob/c1ec2168805dc81e3cea7dc6b0b6f93fd6de3ed5/app/lib/handlers/path_eraser.dart#L73
If loadElements(current) is commented out, there's no lag (but of course that causes all sorts of problems). I haven't quite wrapped my brain around the baking system yet, so feel free to take this if this gives you any ideas!
Hmm, loadElements should only be called if you load the file and if the renderer list is out of sync. On remove we should instead just remove the renderer and bake with reset: true. That should remove some overhead on remove but i dont know if it removes all the lag
By the way: we need to use ids for element events since butterfly should also work in collaboration mode. What we could try to optimize is to use override the element to use the id hashCode for saving the reference but I'm not really sure if this really helps performance since these searches can also run in background in compute
https://api.dart.dev/stable/2.6.0/dart-core/Set/lookup.html
This was my issue, I mentioned on discord, thank you!
I'm having the same issue, but moving the canvas does not make the lag go away. Also, even while avoiding things that trigger the lag, some things still don't work properly. Don't know if I should report this as a separate issue with more detail since it seems to otherwise be the same issue.
Maybe this could be the same issue. Can you explain it a bit further how this problem happens and where you are running the app
Maybe this could be the same issue. Can you explain it a bit further how this problem happens and where you are running the app
I am running the app on a Galaxy Tab S6 Lite (2020 version) with Android 12.
The issue only occurs on a canvas that has a decent amount of lines. It runs fine at first, I can write/draw just fine. The issues start when I try to undo.
When I place a line, don't move the camera, then press undo, the whole app starts to lag really hard, and it becomes impossible to write properly. It might even crash if I try to write.
On the other hand, if I place a line, do move the camera, then press undo, the undoing fails, and no lag occurs. The line it's undoing disappears for a split second, then just reappears. Sometimes it does finally manage to undo after a few attempts, but then it lags again like described before.
The lag does not go away until I exit out of the file. I am not having any issues with the path eraser or lasso tool, though they are also affected by the lag. Auto-save is turned off.
I am experiencing the very same thing. I am using it on my mobile phone, Xiaomi Mi 11i, Android 13, custom ROM (MIUI by xiaomi.eu version 14.0.7.0. TKKCNXM Stable).
The version of the app is 2.3.1 and I downloaded it from F-Droid.
Whenever I create a new canvas and start writing things down, it's alright. Then as the size increases (i.e. the amount of text lines), it starts lagging. Some letters look weird, with sharp angles, do not follow my input at all. Then I move the canvas a little and it stops. Then it starts again. It is bearable most of the time, that is until I use the "undo" or "eraser" button. When I undo last stroke or erase it, writing it correctly is a nightmare since the letters are so choppy (see the screenshot below, where letters in red circles are written after using "undo" and "eraser" button and others before). The only way I've managed to temporarily fix it (until I need to use these two actions again) is exit the canvas and open them again. The autosave feature is turned ON. The eraser is lagging every time I use it, no matter what, I do not know if this is connected to this issue.
Exiting and/or entering fullscreen mode does not help (I use fullscreen mode most of the time).
Hope this helps somehow in narrowing this issue down.
Some useful infos reported by a user:
- Serious problem with erasing, it causes lags and sometimes undoes the erasing, and it causes some highlightings (modified pen) to become slightly more opaque!
- Scrolling up and down frequently causes lag even in performance mode.
- Continues heavy lag when using undo function (happens randomly)
- stuttering when frequently switching between tools like writing then erasing then highlighting (Disabling auto save lowers the chance of the stuttering, but it still happens)
- All strokes except the last one become somewhat pixelated because it is getting baked. (They look ugly, is it because im too zoomed in?)