]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
performance: make get_iiwidth() to be const function
authorSami Kerola <kerolasa@iki.fi>
Sun, 4 Sep 2016 08:46:52 +0000 (09:46 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 4 Sep 2016 20:13:39 +0000 (21:13 +0100)
Change input argument to be a variable, so that optimizer can kick in and
take short cuts when possible.

Reference; https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes

asn.c
asn.h
curses.c
report.c

diff --git a/asn.c b/asn.c
index 4202f669fad2bbd4024855685ffb659897044af3..13ae0f81cea427b93eaed461856873b60a8472e6 100644 (file)
--- a/asn.c
+++ b/asn.c
@@ -256,18 +256,18 @@ extern ATTRIBUTE_CONST size_t get_iiwidth_len(void) {
     return (sizeof(iiwidth) / sizeof((iiwidth)[0]));
 }
 
-extern int get_iiwidth(struct mtr_ctl *ctl) {
+extern ATTRIBUTE_CONST int get_iiwidth(int ipinfo_no) {
     static const int len = (sizeof(iiwidth) / sizeof((iiwidth)[0]));
 
-    if (ctl->ipinfo_no < len)
-        return iiwidth[ctl->ipinfo_no];
-    return iiwidth[ctl->ipinfo_no % len];
+    if (ipinfo_no < len)
+        return iiwidth[ipinfo_no];
+    return iiwidth[ipinfo_no % len];
 }
 
 extern char *fmt_ipinfo(struct mtr_ctl *ctl, ip_t *addr){
     char *ipinfo = get_ipinfo(ctl, addr);
     char fmt[8];
-    snprintf(fmt, sizeof(fmt), "%s%%-%ds", ctl->ipinfo_no?"":"AS", get_iiwidth(ctl));
+    snprintf(fmt, sizeof(fmt), "%s%%-%ds", ctl->ipinfo_no?"":"AS", get_iiwidth(ctl->ipinfo_no));
     snprintf(fmtinfo, sizeof(fmtinfo), fmt, ipinfo?ipinfo:UNKN);
     return fmtinfo;
 }
diff --git a/asn.h b/asn.h
index adea4896ee2a57417a7ba1984fad4157c02d99ca..9d02e5f78147c94b3215c78ff1cfcdcbb563b145 100644 (file)
--- a/asn.h
+++ b/asn.h
@@ -22,5 +22,5 @@ extern void asn_open(struct mtr_ctl *ctl);
 extern void asn_close(struct mtr_ctl *ctl);
 extern char *fmt_ipinfo(struct mtr_ctl *ctl, ip_t *addr);
 extern ATTRIBUTE_CONST size_t get_iiwidth_len(void);
-extern int get_iiwidth(struct mtr_ctl *ctl);
+extern ATTRIBUTE_CONST int get_iiwidth(int ipinfo_no);
 extern int is_printii(struct mtr_ctl *ctl);
index a071f40c404aadfd5412df45db6535f9417633d1..cec844de2a721bacda58725ac7ca7634ba8a5ef2 100644 (file)
--- a/curses.c
+++ b/curses.c
@@ -687,7 +687,7 @@ extern void mtr_curses_redraw(struct mtr_ctl *ctl)
     int padding = 30;
 #ifdef HAVE_IPINFO
     if (is_printii(ctl))
-      padding += get_iiwidth(ctl);
+      padding += get_iiwidth(ctl->ipinfo_no);
 #endif
     int max_cols = maxx<=SAVED_PINGS+padding ? maxx-padding : SAVED_PINGS;
     startstat = padding - 2;
index 93568fb1ac86359e72b616e6530cdd2ff95027ae..79bd9469ddfd2244fc6f33b810cd1bdec10cf83d 100644 (file)
--- a/report.c
+++ b/report.c
@@ -100,7 +100,7 @@ extern void report_close(struct mtr_ctl *ctl)
     ctl->ipinfo_no %= iiwidth_len;
     if (ctl->reportwide) {
       len_hosts++;    // space
-      len_tmp   += get_iiwidth(ctl);
+      len_tmp   += get_iiwidth(ctl->ipinfo_no);
       if (!ctl->ipinfo_no)
         len_tmp += 2; // align header: AS
     }