[DTrace-devel] [PATCH 10/20] Optimize dt_strtab_insert() for empty string

Kris Van Hees kris.van.hees at oracle.com
Tue Jun 1 22:48:00 PDT 2021


This patch also clarifies that the string table will always have the
empty string at offset 0.

Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
 libdtrace/dt_strtab.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/libdtrace/dt_strtab.c b/libdtrace/dt_strtab.c
index 9f5ae23f..624b0934 100644
--- a/libdtrace/dt_strtab.c
+++ b/libdtrace/dt_strtab.c
@@ -65,6 +65,11 @@ dt_strtab_create(size_t bufsz)
 	if (dt_strtab_grow(sp) == -1)
 		goto err;
 
+	/*
+	 * Pre-populate the string table with the empty string as frist string,
+	 * at offset 0.  We use this guarantee in dt_strtab_insert() and
+	 * dt_strtab_index().
+	 */
 	*sp->str_ptr++ = '\0';
 	return sp;
 
@@ -192,7 +197,7 @@ dt_strtab_index(dt_strtab_t *sp, const char *str)
 	ulong_t h;
 
 	if (str == NULL || str[0] == '\0')
-		return 0; /* we keep a \0 at offset 0 to simplify things */
+		return 0;	/* The empty string is always at offset 0. */
 
 	h = dt_strtab_hash(str, &len) % sp->str_hashsz;
 
@@ -212,6 +217,9 @@ dt_strtab_insert(dt_strtab_t *sp, const char *str)
 	ssize_t off;
 	ulong_t h;
 
+	if (str == NULL || str[0] == '\0')
+		return 0;	/* The empty string is always at offset 0. */
+
 	if ((off = dt_strtab_index(sp, str)) != -1)
 		return off;
 
-- 
2.31.1




More information about the DTrace-devel mailing list