performance: make get_iiwidth() to be const function

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
This commit is contained in:
Sami Kerola 2016-09-04 09:46:52 +01:00
parent c1806e9c75
commit 48d859cd7e
No known key found for this signature in database
GPG Key ID: A9553245FDE9B739
4 changed files with 8 additions and 8 deletions

10
asn.c
View File

@ -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;
}

2
asn.h
View File

@ -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);

View File

@ -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;

View File

@ -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
}