mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-05-21 13:02:10 +02:00
gmain: Eliminate use of ABS from g_timeout_set_expiration()
It’s not clear why `ABS()` was ever needed here so, as per the previous commit, let’s eliminate it. It was added in d9f5ab56c3 owing to the need for a pseudo-random value in the range (0, 1000000). Previously the code calculated a hash (unsigned pseudo-random value in the full range of `unsigned int`), then cast it to `int` (making half its possible values negative), then took `ABS()` of that (forcing the negative values positive, so effectively halving the pseudo-random state space and doubling the possibility of any particular value being chosen), then did mod 1000000 to limit the upper bound. We can simplify this by eliminating the cast to `int`, which removes the need for `ABS()`. The whole `unsigned int` state space is then passed to mod 1000000. This should give the same probability distribution as before. The `timer_perturb` variable itself is signed to allow for `-1` to indicate it’s not been set yet (it’s `static`). This could be split out into a separate boolean, but there’s no point as the desired `timer_perturb` state space is only (0, 1000000) anyway, which is easily representable as a signed `int`. Spotted by Thomas Haller in https://gitlab.gnome.org/GNOME/glib/-/merge_requests/4559#note_2384322. Signed-off-by: Philip Withnall <pwithnall@gnome.org>
This commit is contained in:
parent
fe7b5132f6
commit
ecfb143bbf
@ -5056,8 +5056,7 @@ g_timeout_set_expiration (GTimeoutSource *timeout_source,
|
||||
session_bus_address = g_getenv ("HOSTNAME");
|
||||
if (session_bus_address)
|
||||
{
|
||||
int hash = g_str_hash (session_bus_address);
|
||||
timer_perturb = ABS (hash) % 1000000;
|
||||
timer_perturb = (int) (g_str_hash (session_bus_address) % 1000000);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user