Re: [PATCH v4 06/14] pylibfdt: Speed up FdtRo.properties() with a C helper
From: Doug Anderson
Date: Mon Nov 23 2026 - 13:54:57 EST
Hi,
On Mon, Nov 23, 2026 at 12:39 AM David Gibson
<david@xxxxxxxxxxxxxxxxxxxxx> wrote:
>
> On Tue, Nov 17, 2026 at 08:17:33AM -0800, Doug Anderson wrote:
> > Hi David,
> >
> > On Mon, Nov 16, 2026 at 7:47 PM David Gibson
> > <david@xxxxxxxxxxxxxxxxxxxxx> wrote:
> > >
> > > > 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.
> > >
> > > Hm, optimizing pylibfdt has never been a focus, on the general
> > > assumption that if speed is really important to you, you shouldn't be
> > > using Python. I'm happy enough with patch 4, because it's a speed win
> > > with a very small and simple change. This optimization is a much
> > > larger chunk of code, so I'm not so convinced this is worth the source
> > > complexity we pay for it.
> > >
> > > That's not an outright rejection, but I'd need convincing this is
> > > worth it.
> >
> > Right, if we're trying to get the absolute maximum speed then of course
> > we wouldn't choose Python. For a tool like `fdtfactor`, speed is
> > certainly not the top priority and Python is the superior choice.
> > Writing it in C would have been a much bigger pain.
> >
> > That all being said, just because speed isn't the top priority doesn't
> > mean it doesn't matter at all. When I started looking at optimizations I
> > was seeing my 438-DTB run take ~25 seconds, which is just a bit too
> > much. With all the optimizations I ended up with ~12 seconds. That's
> > still a lot and it would be nice to make it even better, but it also
> > starts getting into the realm of acceptable.
> >
> > `FdtRo.properties()` stood out in profiling because
> > `DeviceTree.unflatten()` calls it on every node (~573k nodes / ~2.06M
> > properties across the 438 Qualcomm DTBs). In the pure-Python generator,
> > each node makes `1 + 2 * num_props` SWIG calls (~4.7M boundary
> > crossings, each going through Python `check_err` / `check_err_null`
> > wrappers and generator suspension/resumption) and allocates a fresh
> > non-interned `str` for repeated property names like `compatible`, `reg`,
> > `status`, and `phandle`.
> >
> > Below is a small standalone script (`bench_properties.py`) that builds a
> > 1,300-node (~140 KB) DTB in memory via `FdtSw` and walks it 400 times
> > (~520k nodes, ~2.08M properties) with patch 06/14 applied. On my
> > machine:
> > * Default `meson setup build` (`-O0`):
> > - `FdtRo.properties()`: `2.52s -> 1.50s` (1.68x faster)
> > - Full tree walk: `3.35s -> 2.25s` (1.49x faster)
> > * `meson setup --buildtype=release` (`-O3`):
> > - `FdtRo.properties()`: `1.76s -> 0.98s` (1.81x faster)
> > - Full tree walk: `2.24s -> 1.43s` (1.57x faster)
> >
> > On the source-complexity side, roughly half of
> > `fdt_node_properties_stub()` in v4 is repetitive `Py_DECREF`
> > error-cleanup boilerplate -- using `PyObject_CallFunction(prop_cls,
> > "Oy#", ...)` and a single `err:` label trims it from 55 lines down to
> > ~30 lines. That said, as noted in the patch notes, ~1 second on a
> > 400-DTB run isn't make-or-break, so if you'd still rather not carry the
> > C helper in `pylibfdt/libfdt.i` I'm totally fine dropping this patch in
> > v5 (or trimming it to the ~30-line version if that tips the
> > balance).
>
> Let's leave it out for this series. I'm ok with revisiting this
> later. Fwiw, gotos for error handling are generally acceptable (along
> the lines commonly used in the kernel, if that helps).
Sure. Dropped from my draft v5.
-Doug