libfdt: Don't search for the terminator in fdt_get_string()
PATCH v2 1/1 libfdt: Don't search for the terminator in fdt_get_string()
Unless FDT_ASSUME_MASK includes ASSUME_VALID_INPUT, fdt_get_string() finds the end of the string with memchr(), bounded by the end of the string block, which also checks that the string is terminated. A string block normally ends with a NUL, and then every string in it is terminated. When the caller does not ask for the string's length, check that byte instead, so that fdt_string() doesn't scan the string at all.
This helps callers that look up many property names. With a 612 KiB tree on x86-64, built with gcc -O2 and the default FDT_ASSUME_MASK, calling fdt_string() for the name of each of its 17,899 properties takes 108 us instead of 160 us, not counting the walk that finds them. libufdt, which Android bootloaders use to apply overlays, calls fdt_string() at least once for every property it reads: tens of thousands of times to apply one overlay to a 445 KiB tree.
---
Changes in v2:
- Simplify the fast path to only trigger when !lenp, since strlen() has roughly the same cost as memchr() when lenp is non-NULL.
- Add David Gibson's Reviewed-by tag.
- Strip function context from @@ hunk headers to avoid MTA line wraps.
libfdt/fdt_ro.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
libfdt/fdt_ro.c +10 -0
@@ -80,6 +80,16 @@ } s = (const char *)fdt + absoffset; + +/* +* If the range ends with a NUL (always the case for a string block made +* of NUL-terminated strings), the string is terminated within it, so +* there is no need to search for the terminator unless the caller +* wants the length. +*/ +if (!lenp && len && s[len -1 ] =='\0' ) +return s; + n =memchr (s,'\0' , len);if (!n) {/* missing terminating NULL */
Signature · 1 line
2.56.0.385.gac671c819a-goog
David Gibson
AppliedQuoted 16 lines · Kelvin Zhang
Unless
FDT_ASSUME_MASKincludesASSUME_VALID_INPUT,fdt_get_string()finds the end of the string withmemchr(), bounded by the end of the string block, which also checks that the string is terminated. A string block normally ends with a NUL, and then every string in it is terminated. When the caller does not ask for the string's length, check that byte instead, so thatfdt_string()doesn't scan the string at all.This helps callers that look up many property names. With a 612 KiB tree on x86-64, built with gcc -O2 and the default
FDT_ASSUME_MASK, callingfdt_string()for the name of each of its 17,899 properties takes 108 us instead of 160 us, not counting the walk that finds them. libufdt, which Android bootloaders use to apply overlays, callsfdt_string()at least once for every property it reads: tens of thousands of times to apply one overlay to a 445 KiB tree.
Quoted 2 lines
Reviewed-by David Gibson <[email protected]>Signed-off-by Kelvin Zhang <[email protected]>
Merged, thanks.
Signature · 4 lines
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