Skip to contentSkip to conversation

scripts/dtc/flattree.c: stringtable_insert optimization

PATCH In review Alex Tran 2 participants 3 messages Aug 22, 2025 – Aug 23, 2025 Device tree

lore
Share & tools
lore

Alex Tran

author
#0 lore raw

PATCH 1/1 scripts/dtc/flattree.c: stringtable_insert optimization

Increment string by string instead of character by character.

---
 scripts/dtc/flattree.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
scripts/dtc/flattree.c +1 -3
index 1bcd8089c5b9..156ca5da89b2 100644
@@ -222,9 +222,7 @@ static int stringtable_insert(struct data *d, const char *str)
 {
 	unsigned int i;
 
-	/* FIXME: do this more efficiently? */
-
-	for (i = 0; i < d->len; i++) {
+	for (i = 0; i < d->len; i += strlen(d->val + i) + 1) {
 		if (streq(str, d->val + i))
 			return i;
 	}
Signature · 1 line
2.50.1
maintainer
#1 lore raw
Quoted 2 lines · Alex Tran

Increment string by string instead of character by character.

Why? Are you doing something where this provides measurable improvement?

Quoted 5 lines
---
 scripts/dtc/flattree.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

This is a copy of upstream dtc. If you want to make changes to dtc, you have to submit them upstream.

scripts/dtc/flattree.c static int stringtable_insert(struct data *d, const char *str) · line 225

Quoted 13 lines
scripts/dtc/flattree.c +1 -3
index 1bcd8089c5b9..156ca5da89b2 100644
@@ -222,9 +222,7 @@ static int stringtable_insert(struct data *d, const char *str)
 {
        unsigned int i;
 
-       /* FIXME: do this more efficiently? */
-
-       for (i = 0; i < d->len; i++) {
+       for (i = 0; i < d->len; i += strlen(d->val + i) + 1) {

This isn't equivalent. If 'd' was 'foobar' and 'str' was 'bar', then before we'd match. Now you don't.

Quoted 6 lines
scripts/dtc/flattree.c +0 -1
                if (streq(str, d->val + i))
                        return i;
        }
--

2.50.1

author
#2 lore raw

I didn't take into account the substring cases. Originally I thought that incrementing by each string in the table would be more efficient than by each character to reduce iterations but now with checking for substrings it is more difficult. I could use strstr(haystack, needle) to detect substrings and get the position on top of the changes I made but the performance boost would be negligible. I'll drop this patch.

Thanks for the review.

Quoted 39 lines · Rob Herring
Quoted 2 lines · Alex Tran

Increment string by string instead of character by character.

Why? Are you doing something where this provides measurable improvement?

Quoted 5 lines
---
 scripts/dtc/flattree.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

This is a copy of upstream dtc. If you want to make changes to dtc, you have to submit them upstream.

scripts/dtc/flattree.c static int stringtable_insert(struct data *d, const char *str) · line 225

Quoted 13 lines
scripts/dtc/flattree.c +1 -3
index 1bcd8089c5b9..156ca5da89b2 100644
@@ -222,9 +222,7 @@ static int stringtable_insert(struct data *d, const char *str)
 {
        unsigned int i;
 
-       /* FIXME: do this more efficiently? */
-
-       for (i = 0; i < d->len; i++) {
+       for (i = 0; i < d->len; i += strlen(d->val + i) + 1) {

This isn't equivalent. If 'd' was 'foobar' and 'str' was 'bar', then before we'd match. Now you don't.

Quoted 6 lines
scripts/dtc/flattree.c +0 -1
                if (streq(str, d->val + i))
                        return i;
        }
--

2.50.1

Signature · 2 lines
Alex Tran
[email protected] | 408-406-2417