Skip to contentSkip to conversation

const / static (against current CVS)

PATCH In review Andreas Mohr 5 participants 8 messages Aug 23, 2005 – Aug 24, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 const / static (against current CVS)

Hello all,

Introduction part:
----------
I'm doing a performance tuning effort all over the place in frequently-used Linux programs:
I'm trying to mark as many data areas as possible const (and static) in various programs (at those places where it's actually feasible, that is).

This should have the following benefits:
a) more .rodata (read-only) pages in programs, which means that pages (since they're read-only and can thus be restored from the binary any time) can simply be discarded instead of having to be paged out when free memory is getting low
b) more page sharing between program instances due to less modified / read-write (and thus unique instance *per-program*!) memory pages
c) various const compiler optimizations kicking in
d) less linker work to be done (static)
e) we detect previously silent memory corruption (random data area trashing) better

(use "objdump -x" to analyze what can be improved)

In smaller programs marking variables as const won't be enough to get several whole pages (4K size) back to read-only, but it goes into the right direction after all, since we don't intend to actually increase the number of read-write pages...
----------

This patch against current CVS adds const and/or static to numerous structs that obviously could be made so. It compiles without any warnings, and qemu even still runs! ;-)

Patch had been posted to "Patches Repository" on User Forum about a month ago, but of course that wasn't enough... (silly me)

Thank you,

Andreas Mohr
(patch attached)

Signature · 1 line
GNU/Linux. It's not the software that's free, it's you.

Index: cpu-all.h
===================================================================
RCS file: /cvsroot/qemu/qemu/cpu-all.h,v
retrieving revision 1.45

cpu-all.h +1 -1
diff -u -r1.45 cpu-all.h
@@ -674,7 +674,7 @@
     const char *help;
 } CPULogItem;
 
-extern CPULogItem cpu_log_items[];
+extern const CPULogItem cpu_log_items[];
 
 void cpu_set_log(int log_flags);
 void cpu_set_log_filename(const char *filename);

Index: exec.c
===================================================================
RCS file: /cvsroot/qemu/qemu/exec.c,v
retrieving revision 1.64

exec.c +3 -3
diff -u -r1.64 exec.c
@@ -141,7 +141,7 @@
 static int io_mem_nb;
 
 /* log support */
-char *logfilename = "/tmp/qemu.log";
+static const char *logfilename = "/tmp/qemu.log";
 FILE *logfile;
 int loglevel;
 
@@ -1289,7 +1289,7 @@
     env->interrupt_request &= ~mask;
 }
 
-CPULogItem cpu_log_items[] = {
+const CPULogItem cpu_log_items[] = {
     { CPU_LOG_TB_OUT_ASM, "out_asm", 
       "show generated host assembly code for each compiled TB" },
     { CPU_LOG_TB_IN_ASM, "in_asm",
@@ -1327,7 +1327,7 @@
 /* takes a comma separated list of log masks. Return 0 if error. */
 int cpu_str_to_log_mask(const char *str)
 {
-    CPULogItem *item;
+    const CPULogItem *item;
     int mask;
     const char *p, *p1;
 

Index: i386-dis.c
===================================================================
RCS file: /cvsroot/qemu/qemu/i386-dis.c,v
retrieving revision 1.5

i386-dis.c +24 -24

108 lines not on this page · raw message

Index: keymaps.c
===================================================================
RCS file: /cvsroot/qemu/qemu/keymaps.c,v
retrieving revision 1.1

keymaps.c +1 -1
diff -u -r1.1 keymaps.c
@@ -24,7 +24,7 @@
 
 static int get_keysym(const char *name)
 {
-    name2keysym_t *p;
+    const name2keysym_t *p;
     for(p = name2keysym; p->name != NULL; p++) {
         if (!strcmp(p->name, name))
             return p->keysym;

Index: monitor.c
===================================================================
RCS file: /cvsroot/qemu/qemu/monitor.c,v
retrieving revision 1.37

monitor.c +13 -13
diff -u -r1.37 monitor.c
@@ -148,7 +148,7 @@
     } else {
         help_cmd1(term_cmds, "", name);
         if (name && !strcmp(name, "log")) {
-            CPULogItem *item;
+            const CPULogItem *item;
             term_printf("Log items (comma separated):\n");
             term_printf("%-10s %s\n", "none", "remove all logs");
             for(item = cpu_log_items; item->mask != 0; item++) {
@@ -992,19 +992,19 @@
 typedef struct MonitorDef {
     const char *name;
     int offset;
-    target_long (*get_value)(struct MonitorDef *md, int val);
+    target_long (*get_value)(const struct MonitorDef *md, int val);
     int type;
 } MonitorDef;
 
 #if defined(TARGET_I386)
-static target_long monitor_get_pc (struct MonitorDef *md, int val)
+static target_long monitor_get_pc (const struct MonitorDef *md, int val)
 {
     return cpu_single_env->eip + cpu_single_env->segs[R_CS].base;
 }
 #endif
 
 #if defined(TARGET_PPC)
-static target_long monitor_get_ccr (struct MonitorDef *md, int val)
+static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
 {
     unsigned int u;
     int i;
@@ -1016,7 +1016,7 @@
     return u;
 }
 
-static target_long monitor_get_msr (struct MonitorDef *md, int val)
+static target_long monitor_get_msr (const struct MonitorDef *md, int val)
 {
     return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
         (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
@@ -1035,7 +1035,7 @@
         (cpu_single_env->msr[MSR_LE] << MSR_LE);
 }
 
-static target_long monitor_get_xer (struct MonitorDef *md, int val)
+static target_long monitor_get_xer (const struct MonitorDef *md, int val)
 {
     return (cpu_single_env->xer[XER_SO] << XER_SO) |
         (cpu_single_env->xer[XER_OV] << XER_OV) |
@@ -1043,35 +1043,35 @@
         (cpu_single_env->xer[XER_BC] << XER_BC);
 }
 
-static target_long monitor_get_decr (struct MonitorDef *md, int val)
+static target_long monitor_get_decr (const struct MonitorDef *md, int val)
 {
     return cpu_ppc_load_decr(cpu_single_env);
 }
 
-static target_long monitor_get_tbu (struct MonitorDef *md, int val)
+static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
 {
     return cpu_ppc_load_tbu(cpu_single_env);
 }
 
-static target_long monitor_get_tbl (struct MonitorDef *md, int val)
+static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
 {
     return cpu_ppc_load_tbl(cpu_single_env);
 }
 #endif
 
 #if defined(TARGET_SPARC)
-static target_long monitor_get_psr (struct MonitorDef *md, int val)
+static target_long monitor_get_psr (const struct MonitorDef *md, int val)
 {
     return GET_PSR(cpu_single_env);
 }
 
-static target_long monitor_get_reg(struct MonitorDef *md, int val)
+static target_long monitor_get_reg(const struct MonitorDef *md, int val)
 {
     return cpu_single_env->regwptr[val];
 }
 #endif
 
-static MonitorDef monitor_defs[] = {
+static const MonitorDef monitor_defs[] = {
 #ifdef TARGET_I386
 
 #define SEG(name, seg) \
@@ -1251,7 +1251,7 @@
 
 static int get_monitor_def(target_long *pval, const char *name)
 {
-    MonitorDef *md;
+    const MonitorDef *md;
     void *ptr;
 
     for(md = monitor_defs; md->name != NULL; md++) {

Index: sdl_keysym.h
===================================================================
RCS file: /cvsroot/qemu/qemu/sdl_keysym.h,v
retrieving revision 1.3

sdl_keysym.h +1 -1
diff -u -r1.3 sdl_keysym.h
@@ -2,7 +2,7 @@
 	const char* name;
 	int keysym;
 } name2keysym_t;
-static name2keysym_t name2keysym[]={
+static const name2keysym_t name2keysym[]={
 /* ascii */
     { "space",                0x020},
     { "exclam",               0x021},

Index: translate-all.c
===================================================================
RCS file: /cvsroot/qemu/qemu/translate-all.c,v
retrieving revision 1.13

translate-all.c +2 -2
diff -u -r1.13 translate-all.c
@@ -58,13 +58,13 @@
 int code_copy_enabled = 1;
 
 #ifdef DEBUG_DISAS
-static const char *op_str[] = {
+static const char * const op_str[] = {
 #define DEF(s, n, copy_size) #s,
 #include "opc.h"
 #undef DEF
 };
 
-static uint8_t op_nb_args[] = {
+static const uint8_t op_nb_args[] = {
 #define DEF(s, n, copy_size) n,
 #include "opc.h"
 #undef DEF

Index: vgafont.h
===================================================================
RCS file: /cvsroot/qemu/qemu/vgafont.h,v
retrieving revision 1.1

vgafont.h +1 -1
diff -u -r1.1 vgafont.h
@@ -1,4 +1,4 @@
-static uint8_t vgafont16[256 * 16] = {
+static const uint8_t vgafont16[256 * 16] = {
 
 	/* 0 0x00 '^@' */
 	0x00, /* 00000000 */

Index: vl.c
===================================================================
RCS file: /cvsroot/qemu/qemu/vl.c,v
retrieving revision 1.133

vl.c +1 -1
diff -u -r1.133 vl.c
@@ -3393,7 +3393,7 @@
             case QEMU_OPTION_d:
                 {
                     int mask;
-                    CPULogItem *item;
+                    const CPULogItem *item;
                     
                     mask = cpu_str_to_log_mask(optarg);
                     if (!mask) {

Index: audio/audio.c
===================================================================
RCS file: /cvsroot/qemu/qemu/audio/audio.c,v
retrieving revision 1.5

audio.c +1 -1
diff -u -r1.5 audio.c
@@ -775,7 +775,7 @@
     }
 }
 
-static struct audio_output_driver *drvtab[] = {
+static struct audio_output_driver * const drvtab[] = {
 #ifdef CONFIG_OSS
     &oss_output_driver,
 #endif

Index: audio/fmodaudio.c
===================================================================
RCS file: /cvsroot/qemu/qemu/audio/fmodaudio.c,v
retrieving revision 1.3

fmodaudio.c +1 -1
diff -u -r1.3 fmodaudio.c
@@ -354,7 +354,7 @@
     return 0;
 }
 
-static struct {
+static const struct {
     const char *name;
     int type;
 } drvtab[] = {

Index: audio/mixeng.c
===================================================================
RCS file: /cvsroot/qemu/qemu/audio/mixeng.c,v
retrieving revision 1.1

mixeng.c +2 -2
diff -u -r1.1 mixeng.c
@@ -62,7 +62,7 @@
 #undef IN_MIN
 #undef IN_T
 
-t_sample *mixeng_conv[2][2][2] = {
+t_sample * const mixeng_conv[2][2][2] = {
     {
         {
             conv_uint8_t_to_mono,
@@ -85,7 +85,7 @@
     }
 };
 
-f_sample *mixeng_clip[2][2][2] = {
+f_sample * const mixeng_clip[2][2][2] = {
     {
         {
             clip_uint8_t_from_mono,

Index: audio/mixeng.h
===================================================================
RCS file: /cvsroot/qemu/qemu/audio/mixeng.h,v
retrieving revision 1.1

mixeng.h +2 -2
diff -u -r1.1 mixeng.h
@@ -28,8 +28,8 @@
 typedef void (f_sample) (void *dst, const void *src, int samples);
 typedef struct { int64_t l; int64_t r; } st_sample_t;
 
-extern t_sample *mixeng_conv[2][2][2];
-extern f_sample *mixeng_clip[2][2][2];
+extern t_sample * const mixeng_conv[2][2][2];
+extern f_sample * const mixeng_clip[2][2][2];
 
 void *st_rate_start (int inrate, int outrate);
 void st_rate_flow (void *opaque, st_sample_t *ibuf, st_sample_t *obuf,

Index: hw/adlib.c
===================================================================
RCS file: /cvsroot/qemu/qemu/hw/adlib.c,v
retrieving revision 1.2

adlib.c +1 -1
diff -u -r1.2 adlib.c
@@ -52,7 +52,7 @@
 #define IO_WRITE_PROTO(name) \
     void name (void *opaque, uint32_t nport, uint32_t val)
 
-static struct {
+static const struct {
     int port;
     int freq;
 } conf = {0x220, 44100};

Index: hw/dma.c
===================================================================
RCS file: /cvsroot/qemu/qemu/hw/dma.c,v
retrieving revision 1.13

dma.c +1 -1
diff -u -r1.13 dma.c
@@ -77,7 +77,7 @@
 
 };
 
-static int channels[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
+static const int channels[8] = {-1, 2, 3, 1, -1, -1, -1, 0};
 
 static void write_page (void *opaque, uint32_t nport, uint32_t data)
 {

Index: hw/fdc.c
===================================================================
RCS file: /cvsroot/qemu/qemu/hw/fdc.c,v
retrieving revision 1.18

fdc.c +2 -2
diff -u -r1.18 fdc.c
@@ -180,7 +180,7 @@
     const unsigned char *str;
 } fd_format_t;
 
-static fd_format_t fd_formats[] = {
+static const fd_format_t fd_formats[] = {
     /* First entry is default format */
     /* 1.44 MB 3"1/2 floppy disks */
     { FDRIVE_DRV_144, FDRIVE_DISK_144, 18, 80, 1, "1.44 MB 3\"1/2", },
@@ -230,7 +230,7 @@
 /* Revalidate a disk drive after a disk change */
 static void fd_revalidate (fdrive_t *drv)
 {
-    fd_format_t *parse;
+    const fd_format_t *parse;
     int64_t nb_sectors, size;
     int i, first_match, match;
     int nb_heads, max_track, last_sect, ro;

Index: hw/pc.c
===================================================================
RCS file: /cvsroot/qemu/qemu/hw/pc.c,v
retrieving revision 1.39

pc.c +6 -6
diff -u -r1.39 pc.c
@@ -396,14 +396,14 @@
 
 #define NE2000_NB_MAX 6
 
-static int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
-static int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
+static const int ne2000_io[NE2000_NB_MAX] = { 0x300, 0x320, 0x340, 0x360, 0x280, 0x380 };
+static const int ne2000_irq[NE2000_NB_MAX] = { 9, 10, 11, 3, 4, 5 };
 
-static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
-static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
+static const int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
+static const int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
 
-static int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
-static int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
+static const int parallel_io[MAX_PARALLEL_PORTS] = { 0x378, 0x278, 0x3bc };
+static const int parallel_irq[MAX_PARALLEL_PORTS] = { 7, 7, 7 };
 
 /* PC hardware initialisation */
 static void pc_init1(int ram_size, int vga_ram_size, int boot_device,

Index: hw/sb16.c
===================================================================
RCS file: /cvsroot/qemu/qemu/hw/sb16.c,v
retrieving revision 1.15

sb16.c +1 -1
diff -u -r1.15 sb16.c
@@ -43,7 +43,7 @@
 
 static const char e3[] = "COPYRIGHT (C) CREATIVE TECHNOLOGY LTD, 1992.";
 
-static struct {
+static const struct {
     int ver_lo;
     int ver_hi;
     int irq;

Index: hw/vga.c
===================================================================
RCS file: /cvsroot/qemu/qemu/hw/vga.c,v
retrieving revision 1.41

vga.c +4 -4
diff -u -r1.41 vga.c
@@ -961,21 +961,21 @@
     }
 }
 
-static vga_draw_glyph8_func *vga_draw_glyph8_table[4] = {
+static vga_draw_glyph8_func * const vga_draw_glyph8_table[4] = {
     vga_draw_glyph8_8,
     vga_draw_glyph8_16,
     vga_draw_glyph8_16,
     vga_draw_glyph8_32,
 };
 
-static vga_draw_glyph8_func *vga_draw_glyph16_table[4] = {
+static vga_draw_glyph8_func * const vga_draw_glyph16_table[4] = {
     vga_draw_glyph16_8,
     vga_draw_glyph16_16,
     vga_draw_glyph16_16,
     vga_draw_glyph16_32,
 };
 
-static vga_draw_glyph9_func *vga_draw_glyph9_table[4] = {
+static vga_draw_glyph9_func * const vga_draw_glyph9_table[4] = {
     vga_draw_glyph9_8,
     vga_draw_glyph9_16,
     vga_draw_glyph9_16,
@@ -1194,7 +1194,7 @@
     VGA_DRAW_LINE_NB,
 };
 
-static vga_draw_line_func *vga_draw_line_table[4 * VGA_DRAW_LINE_NB] = {
+static vga_draw_line_func * const vga_draw_line_table[4 * VGA_DRAW_LINE_NB] = {
     vga_draw_line2_8,
     vga_draw_line2_16,
     vga_draw_line2_16,

Index: linux-user/main.c
===================================================================
RCS file: /cvsroot/qemu/qemu/linux-user/main.c,v
retrieving revision 1.70

main.c +1 -1
diff -u -r1.70 main.c
@@ -1034,7 +1034,7 @@
             break;
         } else if (!strcmp(r, "d")) {
             int mask;
-            CPULogItem *item;
+            const CPULogItem *item;
 
 	    if (optind >= argc)
 		break;

Index: slirp/ip_icmp.c
===================================================================
RCS file: /cvsroot/qemu/qemu/slirp/ip_icmp.c,v
retrieving revision 1.1

ip_icmp.c +3 -3
diff -u -r1.1 ip_icmp.c
@@ -40,11 +40,11 @@
 struct icmpstat icmpstat;
 
 /* The message sent when emulating PING */
-/* Be nice and tell them it's just a psuedo-ping packet */
-char icmp_ping_msg[] = "This is a psuedo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
+/* Be nice and tell them it's just a pseudo-ping packet */
+static const char icmp_ping_msg[] = "This is a pseudo-PING packet used by Slirp to emulate ICMP ECHO-REQUEST packets.\n";
 
 /* list of actions for icmp_error() on RX of an icmp message */
-static int icmp_flush[19] = {
+static const int icmp_flush[19] = {
 /*  ECHO REPLY (0)  */   0,
 		         1,
 		         1,

Index: slirp/mbuf.c
===================================================================
RCS file: /cvsroot/qemu/qemu/slirp/mbuf.c,v
retrieving revision 1.1

mbuf.c +2 -2
diff -u -r1.1 mbuf.c
@@ -21,9 +21,9 @@
 char	*mclrefcnt;
 int mbuf_alloced = 0;
 struct mbuf m_freelist, m_usedlist;
-int mbuf_thresh = 30;
+static const int mbuf_thresh = 30;
 int mbuf_max = 0;
-int msize;
+static int msize;
 
 void
 m_init()

Index: slirp/tcp.h
===================================================================
RCS file: /cvsroot/qemu/qemu/slirp/tcp.h,v
retrieving revision 1.1

tcp.h +1 -1
diff -u -r1.1 tcp.h
@@ -164,6 +164,6 @@
 
 extern tcp_seq tcp_iss;                /* tcp initial send seq # */
 
-extern char *tcpstates[];
+extern const char * const tcpstates[];
 
 #endif

Index: slirp/tcp_input.c
===================================================================
RCS file: /cvsroot/qemu/qemu/slirp/tcp_input.c,v
retrieving revision 1.4

tcp_input.c +1 -1
diff -u -r1.4 tcp_input.c
@@ -47,7 +47,7 @@
 
 struct socket tcb;
 
-int	tcprexmtthresh = 3;
+static int	tcprexmtthresh = 3;
 struct	socket *tcp_last_so = &tcb;
 
 tcp_seq tcp_iss;                /* tcp initial send seq # */

Index: slirp/tcp_output.c
===================================================================
RCS file: /cvsroot/qemu/qemu/slirp/tcp_output.c,v
retrieving revision 1.2

tcp_output.c +3 -3
diff -u -r1.2 tcp_output.c
@@ -45,17 +45,17 @@
 #include <slirp.h>
 
 /*
- * Since this is only used in "stats socket", we give meaning
+ * Since this is only used in "stats socket", we give meaningful
  * names instead of the REAL names
  */
-char *tcpstates[] = {
+const char * const tcpstates[] = {
 /*	"CLOSED",       "LISTEN",       "SYN_SENT",     "SYN_RCVD", */
 	"REDIRECT",	"LISTEN",	"SYN_SENT",     "SYN_RCVD",
 	"ESTABLISHED",  "CLOSE_WAIT",   "FIN_WAIT_1",   "CLOSING",
 	"LAST_ACK",     "FIN_WAIT_2",   "TIME_WAIT",
 };
 
-u_char  tcp_outflags[TCP_NSTATES] = {
+static const u_char  tcp_outflags[TCP_NSTATES] = {
 	TH_RST|TH_ACK, 0,      TH_SYN,        TH_SYN|TH_ACK,
 	TH_ACK,        TH_ACK, TH_FIN|TH_ACK, TH_FIN|TH_ACK, 
 	TH_FIN|TH_ACK, TH_ACK, TH_ACK,

Index: slirp/tcp_subr.c
===================================================================
RCS file: /cvsroot/qemu/qemu/slirp/tcp_subr.c,v
retrieving revision 1.5

tcp_subr.c +4 -4
diff -u -r1.5 tcp_subr.c
@@ -46,9 +46,9 @@
 #include <slirp.h>
 
 /* patchable/settable parameters for tcp */
-int 	tcp_mssdflt = TCP_MSS;
-int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
-int	tcp_do_rfc1323 = 0;	/* Don't do rfc1323 performance enhancements */
+static const int 	tcp_mssdflt = TCP_MSS;
+static const int 	tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ;
+static const int	tcp_do_rfc1323 = 0;	/* Don't do rfc1323 performance enhancements */
 int	tcp_rcvspace;	/* You may want to change this */
 int	tcp_sndspace;	/* Keep small if you have an error prone link */
 
@@ -554,7 +554,7 @@
 /*
  * Set the socket's type of service field
  */
-struct tos_t tcptos[] = {
+static const struct tos_t tcptos[] = {
 	  {0, 20, IPTOS_THROUGHPUT, 0},	/* ftp data */
 	  {21, 21, IPTOS_LOWDELAY,  EMU_FTP},	/* ftp control */
 	  {0, 23, IPTOS_LOWDELAY, 0},	/* telnet */

Index: slirp/tcp_timer.c
===================================================================
RCS file: /cvsroot/qemu/qemu/slirp/tcp_timer.c,v
retrieving revision 1.2

tcp_timer.c +1 -1
diff -u -r1.2 tcp_timer.c
@@ -127,7 +127,7 @@
 		tp->t_timer[i] = 0;
 }
 
-int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
+const int	tcp_backoff[TCP_MAXRXTSHIFT + 1] =
    { 1, 2, 4, 8, 16, 32, 64, 64, 64, 64, 64, 64, 64 };
 
 /*

Index: slirp/tcp_timer.h
===================================================================
RCS file: /cvsroot/qemu/qemu/slirp/tcp_timer.h,v
retrieving revision 1.1

tcp_timer.h +1 -1
diff -u -r1.1 tcp_timer.h
@@ -130,7 +130,7 @@
 extern int tcp_keepintvl;		/* time between keepalive probes */
 extern int tcp_maxidle;			/* time to drop after starting probes */
 extern int tcp_ttl;			/* time to live for TCP segs */
-extern int tcp_backoff[];
+extern const int tcp_backoff[];
 
 struct tcpcb;
 

Index: slirp/udp.c
===================================================================
RCS file: /cvsroot/qemu/qemu/slirp/udp.c,v
retrieving revision 1.6

udp.c +3 -3
diff -u -r1.6 udp.c
@@ -54,9 +54,9 @@
  * Per RFC 768, August, 1980.
  */
 #ifndef	COMPAT_42
-int	udpcksum = 1;
+static const int	udpcksum = 1;
 #else
-int	udpcksum = 0;		/* XXX */
+static const int	udpcksum = 0;		/* XXX */
 #endif
 
 struct	socket *udp_last_so = &udb;
@@ -363,7 +363,7 @@
 	sofree(so);
 }
 
-struct tos_t udptos[] = {
+static const struct tos_t udptos[] = {
 	{0, 53, IPTOS_LOWDELAY, 0},			/* DNS */
 	{517, 517, IPTOS_LOWDELAY, EMU_TALK},	/* talk */
 	{518, 518, IPTOS_LOWDELAY, EMU_NTALK},	/* ntalk */

Index: target-i386/helper2.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-i386/helper2.c,v retrieving revision 1.35

helper2.c +1 -1
diff -u -r1.35 helper2.c
@@ -257,7 +257,7 @@
 {
     int eflags, i, nb;
     char cc_op_name[32];
-    static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
+    static const char * const seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
 
     eflags = env->eflags;
 #ifdef TARGET_X86_64

Index: target-i386/translate.c
===================================================================
RCS file: /cvsroot/qemu/qemu/target-i386/translate.c,v retrieving revision 1.51

translate.c +57 -57

426 lines not on this page · raw message

#1 lore raw

Hello Andreas!

Quoted 7 lines

Introduction part:
----------
I'm doing a performance tuning effort all over the place in frequently-used Linux programs:
I'm trying to mark as many data areas as possible const (and static) in various programs (at those places where it's actually feasible, that is).

Great! Damn, never think to do something like this before. Anyway, do you get any speed improvement when using this patch? or smaller memory consumption?

regards

Mulyadi

#2 lore raw
In reply to Andreas Mohr

My own experience is the effect of these types of optimizations is usually negligible, although it is still the first thing I do when optimizing a program. The main improvement I find is reducing the time required to initialize variables and improved code readability. If you know values are constant, you do not need to reference how there values are changing in a debugger...

Probably more important is to make sure none constant data structures are done on the stack. There is no good reason why any code page should be read-write.

Bill

Quoted 54 lines · Andreas Mohr

Hello all,

Introduction part:
----------
I'm doing a performance tuning effort all over the place in frequently-used Linux programs:
I'm trying to mark as many data areas as possible const (and static) in various programs (at those places where it's actually feasible, that is).

This should have the following benefits:
a) more .rodata (read-only) pages in programs, which means that pages (since they're read-only and can thus be restored from the binary any time) can simply be discarded instead of having to be paged out when free memory is getting low
b) more page sharing between program instances due to less modified / read-write (and thus unique instance *per-program*!) memory pages
c) various const compiler optimizations kicking in
d) less linker work to be done (static)
e) we detect previously silent memory corruption (random data area trashing) better

(use "objdump -x" to analyze what can be improved)

In smaller programs marking variables as const won't be enough to get several whole pages (4K size) back to read-only, but it goes into the right direction after all, since we don't intend to actually increase the number of read-write pages...
----------

This patch against current CVS adds const and/or static to numerous structs that obviously could be made so. It compiles without any warnings, and qemu even still runs! ;-)

Patch had been posted to "Patches Repository" on User Forum about a month ago, but of course that wasn't enough... (silly me)

Thank you,

Andreas Mohr
(patch attached)

--
GNU/Linux. It's not the software that's free, it's you.

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

#3 lore raw
Quoted 3 lines

Probably more important is to make sure none constant data structures are done on the stack. There is no good reason why any code page should be read-write.

Huh? this is nonsense.

You have three segements in an application (ignoring dynamic heap allocated memory):

The RO segment that contains code and readonly data. This is typically implemented as a readonly file mapping shared by multiple applications.

The RW segment contains read/write data, some of which may be initialized by data stored in the executable file, the rest is zero-initialized at startup.

The Stack is readwrite, unititialized, and typically allocated dynamically at runtime.

The compiler never puts readwrite objects in th RO segment. If it does you've got a buggy toolchain or build system.

Making global data readonly is a small win because it means it can be shared by multiple instances of the same application.

Moving global RW data onto the stack isn't neccly a win. Most systems have a relatively small limit on stack size, so putting large objects on the stack is a bad idea.

Contrary to popular belief the "const" qualifier on pointers has absolutely no effect on optimization. It's simply a debugging aid so the compiler will generate an error if you accidentally assign to it. It's perfectly legal to cast a (const char *) to a (char *) then dereference and write to it, provided the object the object it points to is modifiable.

Paul

#4 lore raw

Hi,

Quoted 5 lines · Paul Brook
Quoted 3 lines

Probably more important is to make sure none constant data structures are done on the stack. There is no good reason why any code page should be read-write.

Huh? this is nonsense.

Uhoh, I seem to have managed to stir up a veritable hornet's net with my patch...

Quoted 2 lines

Making global data readonly is a small win because it means it can be shared by multiple instances of the same application.

Yup.

Quoted 3 lines

Moving global RW data onto the stack isn't neccly a win. Most systems have a relatively small limit on stack size, so putting large objects on the stack is a bad idea.

Indeed. Witness e.g. Linux's optional kernel stack reduction to 4k and the problems that ensued.

Quoted 5 lines

Contrary to popular belief the "const" qualifier on pointers has absolutely no effect on optimization. It's simply a debugging aid so the compiler will generate an error if you accidentally assign to it. It's perfectly legal to cast a (const char *) to a (char *) then dereference and write to it, provided the object the object it points to is modifiable.

I was thinking just that: that constifying stuff at most gives a very small compiler gain, but most probably just nothing (some people reported some rather weak 1.4% performance gain somewhere!? Statistical noise...). Since you say it's nothing I'm strongly inclined to believe it...

Well, it's not only a compiler debugging aid, but also a runtime debugging aid, since random (attempted) .rodata page corruption should SEGV in that case.

Andreas Mohr

#5 lore raw
Quoted 11 lines · Andreas Mohr

Hi,

Quoted 5 lines · Paul Brook
Quoted 3 lines

Probably more important is to make sure none constant data structures are done on the stack. There is no good reason why any code page should be read-write.

Huh? this is nonsense.

Uhoh, I seem to have managed to stir up a veritable hornet's net with my patch...

For the record this wasn't intended as a criticism of your patch. I think adding const/static is a good idea for debugging/maintainability, even if it doesn't give any actual performance benefits.

Paul

#6 lore raw
Quoted 5 lines · Paul Brook
Quoted 3 lines

Probably more important is to make sure none constant data structures are done on the stack. There is no good reason why any code page should be read-write.

Huh? this is nonsense.

I stand corrected, I ment to say on the stack or in the heap. ie. I was only refering within the scope of the original post. The point is you don't want self modifing code. In rare circumstances you need self modifing code, but it is very rare. While it possible to mark allocated pages as read only, this usually only done for runtime error detection and sometimes thread saftey. Pretty much all modern operating systems do copy on write. However, this is completely beyond the scope of our discussion.

Bill

#7 lore raw
In reply to Paul Brook
Quoted 4 lines · Paul Brook

Contrary to popular belief the "const" qualifier on pointers has absolutely no effect on optimization. It's simply a debugging aid so the compiler will generate an error if you accidentally assign to it.

That's only true when the "const" applies to pointer targets, as in:

   const char * ptr = "foo";

It does not apply when the "const" applies to the pointer itself. This puts the pointer "ptr" into RO storage:

const char * const ptr = "foo";

Hence the large number of times that pattern occurs in the patch.

Quoted 2 lines

It's perfectly legal to cast a (const char *) to a (char *) then dereference and write to it, provided the object the object it

                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 1 line

points to is modifiable.

^^^^^^^^^^^^^^^^^^^^^^^

Precisely. Global and static variables that are declared "const" are not "modifiable", and are put in RO storage, so they cannot be written to. Taking their address creates a "const" pointer which must not be derefenced and written to.

-- Jamie