devicetree-compiler.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yao Zi <ziyao@disroot.org>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: devicetree-compiler@vger.kernel.org
Subject: Re: [RESEND PATCH] flattree: Optimize stringtable_insert()
Date: Mon, 14 Apr 2025 12:10:09 +0000	[thread overview]
Message-ID: <Z_z7IUsK1zbikIp2@pie> (raw)
In-Reply-To: <Z_yqFzoTQ4fpdd8g@zatzit>

On Mon, Apr 14, 2025 at 04:24:23PM +1000, David Gibson wrote:
> On Sat, Apr 12, 2025 at 10:03:52AM +0000, Yao Zi wrote:
> > According to perf result, stringtable_insert() is one of the five hotest
> > functions, which is obvious since it depends on a brute-force,
> > quadratic-complexity method to deduplicate the string block and is
> > called at creation of every property.
> 
> Right.  The optimization strategy of dtc and libfdt is pretty much "if
> it's slow enough that it bothers someone, then we'll think about it".
> Little effort has gone into optimization, in general, because device
> trees are generally so small that runtime hasn't really been a
> problem, even with very inefficient coding.
> 
> > This patch optimizes the function in two ways,
> > 
> > - Replace brute-force deduplication with libc-provided memmem(), which
> >   is guaranteed to be in linear complexity on both glibc on musl libc.
> >   This brings roughly 24.6% reduction in execution time.
> 
> That said, this is a substantial improvement for a very simple change.
> I'd be happy to apply this change, separated into its own patch.
> 
> > - An open-addressing hashtable is maintained to track strings already
> >   inserted. As property names are likely to be duplicated, this could
> >   filter out many existing strings, avoiding traversing the string
> >   block. This reduces another 1.2% of execution time.
> 
> Honestly the substantial complexity this adds doesn't seem worth it
> for a mere 1.2% improvement.

Thanks for the feedback. At first I'm only interested in improving the
deduplication with a hashtable -- but later it's found that reusing
subsequences is also necessary and memmem() itself surprisingly brings a
huge improvement.

I'm preparing for v2 where the hashtable is split out and also used for
optimizing get_node_by_label(). Although the changes are not ready for
reviewing now, the new optimization reduces executation by another
6.4%. Do you think it's a reason strong enough to keep the the code?
Here's a summary of the changes,

	Makefile.dtc |   1 +
	checks.c     |   4 --
	dtc-parser.y |   4 +-
	dtc.c        |   4 ++
	dtc.h        |  19 +++++++
	flattree.c   |  68 +++++++++++++++++++------
	hashtable.c  | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++
	livetree.c   |  35 +++++++++++--
	8 files changed, 251 insertions(+), 25 deletions(-)

...

> -- 
> David Gibson (he or they)	| I'll have my music baroque, and my code
> david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
> 				| around.
> http://www.ozlabs.org/~dgibson

Best regards,
Yao Zi

  reply	other threads:[~2025-04-14 12:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-12 10:03 [RESEND PATCH] flattree: Optimize stringtable_insert() Yao Zi
2025-04-12 12:45 ` Simon Glass
2025-04-14  3:27   ` Yao Zi
2025-04-14  6:24 ` David Gibson
2025-04-14 12:10   ` Yao Zi [this message]
2025-04-15  1:39     ` David Gibson
2025-04-15  4:03       ` Yao Zi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z_z7IUsK1zbikIp2@pie \
    --to=ziyao@disroot.org \
    --cc=david@gibson.dropbear.id.au \
    --cc=devicetree-compiler@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).