[PATCH 2/3] nslookup: query class: move query_class to a global

Henrique de Moraes Holschuh henrique at nic.br
Wed Dec 8 14:19:59 UTC 2021


Size optimization: move the query class to a global static variable,
this decreases overall code size, but increases BSS by sizeof int.

This commit results in:

function                                             old     new   delta
add_query                                            117     114      -3
nslookup_main                                        959     945     -14
add_query_with_search                                160     141     -19
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-36)             Total: -36 bytes

This brings down the size cost of the "query class" feature to:
209 - 36 = 173 bytes.

Signed-off-by: Henrique de Moraes Holschuh <henrique at nic.br>
---
 networking/nslookup.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/networking/nslookup.c b/networking/nslookup.c
index 35e29eaae..1cbf8becc 100644
--- a/networking/nslookup.c
+++ b/networking/nslookup.c
@@ -327,6 +327,7 @@ struct globals {
 	unsigned default_timeout;
 	unsigned query_count;
 	unsigned serv_count;
+	int query_class;
 	struct ns *server;
 	struct query *query;
 	char *search;
@@ -339,6 +340,7 @@ struct globals {
 	G.default_port = 53; \
 	G.default_retry = 2; \
 	G.default_timeout = 5; \
+	G.query_class = C_IN; \
 } while (0)
 
 enum {
@@ -782,7 +784,7 @@ static void parse_resolvconf(void)
 		G.search = NULL;
 }
 
-static void add_query(int class, int type, const char *dname)
+static void add_query(int type, const char *dname)
 {
 	struct query *new_q;
 	unsigned count;
@@ -793,10 +795,10 @@ static void add_query(int class, int type, const char *dname)
 	G.query = xrealloc_vector(G.query, /*4=2^2:*/ 2, count);
 	new_q = &G.query[count];
 
-	dbg("new query#%u class %u type %u for '%s'\n", count, class, type, dname);
+	dbg("new query#%u class %u type %u for '%s'\n", count, G.query_class, type, dname);
 	new_q->name = dname;
 
-	qlen = res_mkquery(QUERY, dname, class, type,
+	qlen = res_mkquery(QUERY, dname, G.query_class, type,
 			/*data:*/ NULL, /*datalen:*/ 0,
 			/*newrr:*/ NULL,
 			new_q->query, sizeof(new_q->query)
@@ -804,12 +806,12 @@ static void add_query(int class, int type, const char *dname)
 	new_q->qlen = qlen;
 }
 
-static void add_query_with_search(int class, int type, const char *dname)
+static void add_query_with_search(int type, const char *dname)
 {
 	char *s;
 
 	if (type == T_PTR || !G.search || strchr(dname, '.')) {
-		add_query(class, type, dname);
+		add_query(type, dname);
 		return;
 	}
 
@@ -819,7 +821,7 @@ static void add_query_with_search(int class, int type, const char *dname)
 
 		e = skip_non_whitespace(s);
 		fullname = xasprintf("%s.%.*s", dname, (int)(e - s), s);
-		add_query(class, type, fullname);
+		add_query(type, fullname);
 		s = skip_whitespace(e);
 		if (!*s)
 			break;
@@ -862,7 +864,6 @@ int nslookup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int nslookup_main(int argc UNUSED_PARAM, char **argv)
 {
 	unsigned types;
-	int qclass;
 	int rc;
 	int err;
 
@@ -872,7 +873,6 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
 	 * if they precede the arguments and are prefixed with a hyphen."
 	 */
 	types = 0;
-	qclass = C_IN;
 	argv++;
 	for (;;) {
 		const char *options =
@@ -953,7 +953,7 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
 				if (i >= ARRAY_SIZE(qclasses))
 					bb_error_msg_and_die("invalid query class \"%s\"", val);
 				if (strcasecmp(qclasses[i].name, val) == 0) {
-					qclass = qclasses[i].class;
+					G.query_class = qclasses[i].class;
 					break;
 				}
 			}
@@ -986,18 +986,18 @@ int nslookup_main(int argc UNUSED_PARAM, char **argv)
 
 		ptr = make_ptr(argv[0]);
 		if (ptr) {
-			add_query(qclass, T_PTR, ptr);
+			add_query(T_PTR, ptr);
 		} else {
-			add_query_with_search(qclass, T_A, argv[0]);
+			add_query_with_search(T_A, argv[0]);
 #if ENABLE_FEATURE_IPV6
-			add_query_with_search(qclass, T_AAAA, argv[0]);
+			add_query_with_search(T_AAAA, argv[0]);
 #endif
 		}
 	} else {
 		int c;
 		for (c = 0; c < ARRAY_SIZE(qtypes); c++) {
 			if (types & (1 << c))
-				add_query_with_search(qclass, qtypes[c].type, argv[0]);
+				add_query_with_search(qtypes[c].type, argv[0]);
 		}
 	}
 
-- 
2.20.1



More information about the busybox mailing list