Skip to content
New issue

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

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

Already on GitHub? # to your account

Replace expensive seat_get_config() functions with pointer #8269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

iguanajuice
Copy link

First time contributor to Sway here. After studying Sway's codebase for a bit, due to wanting to implement something unrelated, I noticed that the function seat_get_config() (and it's sister function seat_get_config_by_name()) are both needlessly expensive and ultimately unneeded.

Currently, Sway finds the config related to a seat by looping through all the seats and strcmp'ing their names until a match is found. This is obviously not very efficient.

My change simply replaces these functions with a pointer in struct sway_seat.

Again, first time contributor, so I'm not super familiar with Sway's codebase, but I have tested the following with my patch:

  1. Individual seat configuration
  2. Global (*) seat configuration
  3. Mixed seat configuration
  4. Multi-seat/multi-pointer support

So far, no crashes or unexpected behaviour; but I do believe more rigorous testing is needed. I am daily driving this patch.

@Nefsen402
Copy link
Member

I believe this will break if the seat config changes during runtime. (e.g. you reload the config file) The old configs will be freed and replaced with the new ones. This code will continue to reference the stale values.

@iguanajuice
Copy link
Author

iguanajuice commented Jul 31, 2024

I believe this will break if the seat config changes during runtime.

Just tested it. This is not the case. Changing seat configs works without having to restart Sway :)

@iguanajuice
Copy link
Author

Update: I have been daily driving this patch for 3 weeks now and nothing is out of the ordinary. I think this patch is well tested and would like to see this get merged, thanks.

@iguanajuice
Copy link
Author

Gentle ping.

Copy link
Member

@Nefsen402 Nefsen402 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not convinced that this PR is handling lifetimes properly. There are a couple calls to free_seat_config that won't end up nulling or recalculating the pointer that the actual seats have meaning they will start having invalid references.

It might make sense to add a destroy style signal to a seat config, and then have seats listen to this destroy signal, then do the appropriate recalculation then.

Also, please make sure you do a rebase instead of a merge. The commits should carry meaning on its own (see atomic git commits). This should all be squashed.

Comment on lines 178 to 186
struct seat_config *seat_config = seat_get_config(seat);
struct seat_config *seat_config = NULL;
struct seat_config *sc = NULL;
for (int i = 0; i < config->seat_configs->length; ++i ) {
sc = config->seat_configs->items[i];
if (strcmp(seat->wlr_seat->name, sc->name) == 0) {
seat_config = sc;
break;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we iterating the seat configs here? I thought that the seat->config should be used now.

Copy link
Author

@iguanajuice iguanajuice Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because input_manager_verify_fallback_seat() runs before seat_apply_config() can set the pointer (according to a backtrace).

Attempting to use the pointer now will result in undefined behavior and a "soft crash", hence this is the only time we need to iterate of the seats by name.

I recant this, sway doesn't crash but instead no default (fallback) seat is created and thus no user input.

@iguanajuice
Copy link
Author

Update: A bug has finally been found. Setting seat [seat_name] fallback true does not work properly.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants