From: Alice Ryhl Date: Wed, 29 Oct 2025 11:50:58 +0000 (+0000) Subject: rust_binder: move BC_FREE_BUFFER drop inside if statement X-Git-Tag: v6.19-rc1~65^2~41 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c1437332e4d351e86049c275d879110f4dabe7b7;p=thirdparty%2Fkernel%2Flinux.git rust_binder: move BC_FREE_BUFFER drop inside if statement When looking at flamegraphs, there is a pretty large entry for the function call drop_in_place::> which in turn calls drop_in_place::. Combined with the looper_need_return condition, this means that the generated code looks like this: if let Some(buffer) = buffer { if buffer.looper_need_return_on_free() { self.inner.lock().looper_need_return = true; } } drop_in_place::>() { // not inlined if let Some(buffer) = buffer { drop_in_place::(buffer); } } This kind of situation where you check X and then check X again is normally optimized into a single condition, but in this case due to the non-inlined function call to drop_in_place::>, that optimization does not happen. Furthermore, the drop_in_place:: call is only two-thirds of the drop_in_place::> call in the flamegraph. This indicates that this double condition is not performing well. Also, last time I looked at Binder perf, I remember finding that the destructor of Allocation was involved with many branch mispredictions. Thus, change this code to look like this: if let Some(buffer) = buffer { if buffer.looper_need_return_on_free() { self.inner.lock().looper_need_return = true; } drop_in_place::(buffer); } by dropping the Allocation directly. Flamegraphs confirm that the drop_in_place::> call disappears from this change. Signed-off-by: Alice Ryhl Acked-by: Carlos Llamas Link: https://patch.msgid.link/20251029-binder-bcfreebuf-option-v1-1-4d282be0439f@google.com Signed-off-by: Greg Kroah-Hartman --- diff --git a/drivers/android/binder/thread.rs b/drivers/android/binder/thread.rs index 7e34ccd394f80..1a8e6fdc0dc42 100644 --- a/drivers/android/binder/thread.rs +++ b/drivers/android/binder/thread.rs @@ -1323,12 +1323,12 @@ impl Thread { } BC_FREE_BUFFER => { let buffer = self.process.buffer_get(reader.read()?); - if let Some(buffer) = &buffer { + if let Some(buffer) = buffer { if buffer.looper_need_return_on_free() { self.inner.lock().looper_need_return = true; } + drop(buffer); } - drop(buffer); } BC_INCREFS => { self.process