From 671679ce630040e801ebbba24d86662256ca5c20 Mon Sep 17 00:00:00 2001 From: Herman Semenoff Date: Thu, 30 Apr 2026 03:59:27 +0300 Subject: [PATCH] up-device-battery: align structure UpBatteryInfo for 64-bit Reduces size struct 120 bytes -> 104 bytes This commit will decrease costs copying, moving, and creating object-structures only for common 64bit processors due to the 8-byte data alignment. Smaller size structure or class, higher chance putting into CPU cache. Most processors are already 64 bit, so the change won't make it any worse. I noticed unaligned structs when analyzing the memory layout CPU cache using utilities Cachegrind and Pahole (Red Hat tool). Example ideal align strutures in mem layout: - https://www.phoronix.com/news/Linux-6.8-Networking - https://lore.kernel.org/netdev/20231129072756.3684495-1-lixiaoyan@google.com/ References about technique: - https://hpc.rz.rptu.de/Tutorials/AVX/alignment.shtml - https://wr.informatik.uni-hamburg.de/_media/teaching/wintersemester_2013_2014/epc-14-haase-svenhendrik-alignmentinc-presentation.pdf - https://en.wikipedia.org/wiki/Data_structure_alignment - https://stackoverflow.com/a/20882083 - https://zijishi.xyz/post/optimization-technique/learning-to-use-data-alignment/ --- src/up-device-battery.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/up-device-battery.h b/src/up-device-battery.h index dd74510..58ec0a7 100644 --- a/src/up-device-battery.h +++ b/src/up-device-battery.h @@ -68,12 +68,12 @@ typedef struct { } UpBatteryValues; typedef struct { - gboolean present; - const char *vendor; const char *model; const char *serial; + gboolean present; + UpBatteryUnits units; union { @@ -87,8 +87,8 @@ typedef struct { } charge; }; - UpDeviceTechnology technology; gdouble voltage_design; + UpDeviceTechnology technology; gint charge_cycles; gdouble voltage_max_design; gdouble voltage_min_design;