-
Notifications
You must be signed in to change notification settings - Fork 769
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
ibus with input method not shared among windows causes massive re-grabbing churn when switching windows #3924
Comments
I don’t see a link to logs.i3wm.org. Did you follow https://i3wm.org/docs/debugging.html? (In case you actually provided a link to a logfile, please ignore me.) |
i3 listens for keyboard change events (see commit dcba0b4) and it looks like ibus sends a lot of these. Not sure if there is a certain property of these events that i3 should ignore. |
I believe this problem is not limited to configurations where "shared input method" is disabled. Disabling this mode merely makes it more apparent. I can reproduce this lag with "shared input method" enabled by switching the input method and immediately trying to switch to other window: the switching will be delayed the same way. I have traced what's happening a bit, and here are my observations:
By looking at the profile and flame graph, the slow function with the most "self" time is
|
This problem surfaces in another way with certain applications, like Firefox and Thunderbird. They tend to send some kind of messages to i3 (type 28, to be exact) when typing, so when i3 is blocked, you can't input text in Firefox. Basically you have to wait 0.5s or more after keyboard layout switch, which is annoying, to put it mildly. Moreover, performance seems to degrade over time for some reason. Eventually i3 starts to block for several seconds after switching keyboard layout. |
FWIW, when running fcitx (alternative to ibus), when switching layouts, i3 receives |
@bluetech It seems like libxkbcommon’s Thanks for any insights! And thanks @WGH- for profiling and clarifying :) Much appreciated! |
Hmm interesting. That's over a local X server, not SSH or such? The amount of notifications ibus sends does seem excessive, but first I'd like to understand where the 60ms figure actually comes from. If @WGH-'s profile is accurate, and the time is indeed spent in I'll try to reproduce this when I get the chance. |
This is indeed a local server. |
I might have done a grave mistake of having a library missing its debug symbols, leading to incorrect profile results. I've added the debug symbols for libxkbcommon, and captured callstack using a different method ("dwarf" in perf). Although there's still something a bit wrong (it missed the inlined static functions despite debug data being available), it looks more believable. I think the conclusion " |
I tried a few ways to reproduce this but couldn't see any serious slowness on the libxkbcommon side, although I didn't try the actual i3+ibus scenario (a bit too much for me to setup). Your profile shows the time being spent in Regardless we should find what is causing ibus to issue all of the keymap update notifications. I wouldn't know where to look in the ibus code myself though. |
Which keyboard layout and xmodmap are you all using? Maybe that’s related? |
Could you please tell me where to look? I configured the X server once, I frankly don't remember. I can say that in IBus I use |
I think the output of |
Depending on current ibus input method, correspondingly:
|
@bluetech this patch certainly helps a lot. It's still somewhat slow, but not that much anymore. When I rapidly switch between open terminal windows in i3, i3 CPU consumption stays relatively low, and there's no visible lag. However, at the same time, CPU spikes in the X server process and Firefox, and Firefox becomes unresponsive for a while (even though it was open on the different workspace). Perhaps, Firefox doesn't use xkbcommon, and it has similar code? Needs another round of profiling, oh well. (The trace log above had "ms" and "_xcb_map_remove" mixed up, but the magnitude was similar)
|
Thanks for testing @WGH-. I'll definitely merge it then and do a release some time after. I also added a benchmark to the PR which shows the keymap refresh time improving from about 4ms on my laptop to about 0.6ms (after the first one).
Firefox on X11 doesn't use xkbcommon, but every libX11 user has some similar code to refresh the local keymap in response to keymap update events. One difference is that xkbcommon's keymaps are immutable, so even if something small is changed, the entire keymap is re-fetched, while libX11 only updates the parts that changed, and so might be a bit faster. I couldn't say if the Firefox slowness comes from this code however.
I couldn't understand from the trace if it still showing slowness. If it does, it would not be difficult to speed it up in libxcb. |
More likely it never did, to be honest. |
I'll continue debugging though, as I feel like there's something going on on the X server side. |
I know this is likely not an i3 message anymore, but it seems that every time I change input methods, Firefox bombards the X server with tens of thousands
Sample output (I changed the input method only once):
|
FWIW I filed it to Firefox bug tracker https://bugzilla.mozilla.org/show_bug.cgi?id=1680909 |
After caching was added in libxkbcommon, and Firefox patched, this issue is longer that severe for me. The ibus mode with remembering layouts is still causes noticeable lag, but it is not as bad as it was (I don't really use the mode, however). I guess in my case Firefox was the main culprit, as hammering the X server slows it down for everyone. I'll try to collect more profile data to analyze whether things can optimized further, but I can now live with it as things are now. |
Just FYI, there was a loosely related issue in AwesomeWM: Apparently, there are people who provide The fix in AwesomeWM was not to deal with the events immediately, but instead remember to do them later. A bit more detailed: A (So, after the fix, AwesomeWM gets the event and updates the keymap. While the keymap is (slowly) updating, all the other events are received and the keymap is only updated once more in the following main loop iteration.) (Thank you @bluetech for sending me here) Edit: Oh, hey, the same reproducer works with i3 (but only produces a three second freeze)!
|
This testcases causes 5000 keyboard events to be generated, meaning that i3 (needlessly) updates the keymap 5000 times. This currently takes about 13 seconds on my computer. This may or may not be a testcase for i3#3924. I am not sure. Signed-off-by: Uli Schlachter <psychon@znc.in>
Instead of updating the keyboard state and regrabbing all the keys immediately when requested, this simply starts an ev idle watcher. This has the effect of batching updates: When multiple updates are requested during a main loop iteration, only one update is actually done. This fixes the new test case that I just added. This might or might not fix i3#3924. Signed-off-by: Uli Schlachter <psychon@znc.in>
Hello. When using ibus launched by
ibus-daemon -drx
from the.xinitrc
, with the option to "Share the same input method among all applications" disabled, switching windows causes noticable lag. This is especially annoying on my low-powered laptop where it can take up to half a second to switch windows.The cause is apparently i3 re-grabbing all bindings many times over on every switch.
Attached is a log file capturing the situation. Note it is over 11 000 lines long, generated by a single window switch.
i3log.txt | https://clbin.com/ML9na
I have reproduced this issue both with the default config and my own, with the
i3-gaps
package currently in archlinux repos, thegaps-next
master branch, thei3
master branch and have been experiencing it for over a year on multiple machines. As such, I believe the specific version is irrelevant to the problem. I don't know whether any particular version of i3 first introduced the issue, as it was already present when I started using ibus. Nevertheless, this is the version of i3 which generated the attached log:The text was updated successfully, but these errors were encountered: