[PATCH v4 05/14] pylibfdt: Unpack Property.as_list() in a single struct.unpack() call

From: Douglas Anderson

Date: Mon Nov 16 2026 - 15:54:35 EST


`Property.as_list()` currently decodes multi-cell properties via
`list(map(lambda x: x[0], struct.iter_unpack('>' + fmt, self)))`, which
constructs an iterator and `map` wrapper and invokes a Python `lambda`
for every cell.

Compute the element size via `struct.calcsize('>' + fmt)` and unpack all
elements in a single C `struct.unpack()` call instead.

On one stress test (decoding ~301k `/__local_fixups__` cell lists across
438 arm64 Qualcomm DTBs) this reduced `Property.as_list()` time by
~2.1x.

Assisted-by: Gemini:gemini-pro-prerelease
Signed-off-by: Douglas Anderson <dianders@xxxxxxxxxxxx>
---
Found this speedup while benchmarking fdtfactor. It's not critical to
functionality, but seems like a nice win.

Changes in v4:
- New patch speeding up Property.as_list() with a single struct.unpack()
call

pylibfdt/libfdt.i | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
index ff9210d123cb..e4a9357d278a 100644
--- a/pylibfdt/libfdt.i
+++ b/pylibfdt/libfdt.i
@@ -931,7 +931,8 @@ class Property(bytes):
return self.as_cell('q')

def as_list(self, fmt):
- return list(map(lambda x: x[0], struct.iter_unpack('>' + fmt, self)))
+ size = struct.calcsize('>' + fmt)
+ return list(struct.unpack(f'>{len(self) // size}{fmt}', self))

def as_uint32_list(self):
return self.as_list('L')
--
2.56.0.679.gc527c65306-goog