[PATCH v4 06/14] pylibfdt: Speed up FdtRo.properties() with a C helper
From: Douglas Anderson
Date: Mon Nov 16 2026 - 15:54:00 EST
Iterating over a node's properties via `FdtRo.properties()` currently
crosses the Python/SWIG boundary `1 + 2 * num_props` times per node
(`first_property_offset()`, `get_property_by_offset()`, and
`next_property_offset()`, along with their Python `check_err()` and
`check_err_null()` wrappers). When walking every property across large
device trees from Python, those per-property SWIG round-trips dominate
runtime.
Add a C helper `fdt_node_properties_stub()` in `pylibfdt/libfdt.i`
backing `FdtRo.properties()` so all `Property` objects for a node are
collected in a single C call per node, interning property name strings
via `PyUnicode_InternFromString()`. Also add an invalid-offset exception
check to `testPropertiesIterator` in `tests/pylibfdt_tests.py`.
On one stress test (walking all ~573k nodes and ~2.06M properties across
438 arm64 Qualcomm DTBs) this reduced `FdtRo.properties()` time by
~1.8x.
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 FdtRo.properties() with a C helper
pylibfdt/libfdt.i | 64 ++++++++++++++++++++++++++++++++++++++---
tests/pylibfdt_tests.py | 3 ++
2 files changed, 63 insertions(+), 4 deletions(-)
diff --git a/pylibfdt/libfdt.i b/pylibfdt/libfdt.i
index e4a9357d278a..333a25f27946 100644
--- a/pylibfdt/libfdt.i
+++ b/pylibfdt/libfdt.i
@@ -24,6 +24,61 @@ static int fdt_property_stub(void *fdt, const char *name, const void *val,
return fdt_property(fdt, name, val, len);
}
+static PyObject *fdt_node_properties_stub(const void *fdt, int nodeoffset,
+ PyObject *prop_cls)
+{
+ PyObject *list = PyList_New(0);
+ int poff;
+
+ if (!list)
+ return NULL;
+
+ poff = fdt_first_property_offset(fdt, nodeoffset);
+ while (poff >= 0) {
+ const char *pname;
+ int len;
+ const void *val = fdt_getprop_by_offset(fdt, poff, &pname, &len);
+ PyObject *name_obj, *val_obj, *prop_obj;
+ int rc;
+
+ if (!val) {
+ Py_DECREF(list);
+ return PyLong_FromLong(len);
+ }
+ name_obj = PyUnicode_InternFromString(pname);
+ if (!name_obj) {
+ Py_DECREF(list);
+ return NULL;
+ }
+ val_obj = PyBytes_FromStringAndSize((const char *)val, len);
+ if (!val_obj) {
+ Py_DECREF(name_obj);
+ Py_DECREF(list);
+ return NULL;
+ }
+ prop_obj = PyObject_CallFunctionObjArgs(prop_cls, name_obj, val_obj,
+ NULL);
+ Py_DECREF(name_obj);
+ Py_DECREF(val_obj);
+ if (!prop_obj) {
+ Py_DECREF(list);
+ return NULL;
+ }
+ rc = PyList_Append(list, prop_obj);
+ Py_DECREF(prop_obj);
+ if (rc < 0) {
+ Py_DECREF(list);
+ return NULL;
+ }
+ poff = fdt_next_property_offset(fdt, poff);
+ }
+ if (poff != -FDT_ERR_NOTFOUND) {
+ Py_DECREF(list);
+ return PyLong_FromLong(poff);
+ }
+ return list;
+}
+
%}
%pythoncode %{
@@ -477,10 +532,9 @@ class FdtRo(object):
Raises:
FdtException on error (e.g. invalid nodeoffset)
"""
- poff = self.first_property_offset(nodeoffset, quiet=QUIET_NOTFOUND)
- while poff >= 0:
- yield self.get_property_by_offset(poff)
- poff = self.next_property_offset(poff, quiet=QUIET_NOTFOUND)
+ return iter(check_err(
+ fdt_node_properties_stub(self._fdt, nodeoffset, Property)
+ ))
def getprop(self, nodeoffset, prop_name, quiet=()):
"""Get a property from a node
@@ -1420,5 +1474,7 @@ int fdt_property_cell(void *fdt, const char *name, uint32_t val);
* function and a struct, which confuses SWIG.
*/
int fdt_property_stub(void *fdt, const char *name, const void *val, int len);
+PyObject *fdt_node_properties_stub(const void *fdt, int nodeoffset,
+ PyObject *prop_cls);
%include <libfdt.h>
diff --git a/tests/pylibfdt_tests.py b/tests/pylibfdt_tests.py
index e2f9c6d5afe4..9c0104bfbabb 100644
--- a/tests/pylibfdt_tests.py
+++ b/tests/pylibfdt_tests.py
@@ -270,6 +270,9 @@ class PyLibfdtBasicTests(unittest.TestCase):
self.assertEqual(props[0], b'subnode1\0')
leaf = self.fdt.path_offset('/subnode@1/ss1')
self.assertEqual(list(self.fdt.properties(leaf)), [])
+ with self.assertRaises(FdtException) as e:
+ list(self.fdt.properties(-1))
+ self.assertEqual(e.exception.err, -libfdt.BADOFFSET)
def testFirstNextSubnodeOffsetExceptions(self):
"""Check except handling for first/next subnode functions"""
--
2.56.0.679.gc527c65306-goog