Vanilla Netrek Server Development Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[VANILLA-L:793] Re: [VANILLA-L:790] Use of 'register' declaration



On Fri, Feb 06, 1998 at 12:51:03AM -0500, koconnor@acsu.buffalo.edu wrote:
> 
> Of course, if someone is willing to enlighten me to the uses of register,
> I would be grateful.

Modern compilers typically do some pretty good optimizations, so the use
of 'register' isn't as important as it used to be.  In fact, frequent
and poor use of 'register' can even degrade performance, depending on
how smart the compiler optimizer is.  And different compilers handle
'register' in different ways.

Anyway, 'register is' typically reserved to force a typed variable to be
kept in the register (or cached) even after the variable is no longer
needed.  The duration is the scope of that variable in that function
call (typically).  If you access more variables than the number of
available registers, the variables get swapped out to a cache or RAM.
This can be a big performance hit if you're accessing some of those
variables very often.  Consider a hypothetical case of a CPU with
2 registers, the following piece of code, and a dumb compiler:

  int i, j, k, l;
  i = j = k = l = 0;
  while (1) {
    i++; j++; k++; i++; j++; k++; i++; j++; l++; i++

    /* i is accessed 4 times
     * j is accessed 3 times
     * k is accessed 2 times
     * l is accessed 1 time
     */
  }

Without declaring 'register', every variable will be swapped in and out
of RAM FIFO.  However, if you declare 'register i', then only j,k,l will
be swapped, so the code loop gets optimized for maximum performance by
reducing the CPU wait for the main memory read/write.

I suspect that all the 'register' declarations in the server code is
left over from early days when the compiler optimizers weren't that
great (or readily available) and the memory fetch cycle and CPU speed
were extremely slow.  I wouldn't be surprised if it increased performance
by as much as 20% in those days...

Hope that helps.
Dave.

-- 
Dave Ahn,  ahn@vec.bgsm.edu             "When you were born you cried, and the
                                         world rejoiced.  Try to live your life
Virtual Endoscopy Center                 so that when you die you will rejoice,
Bowman Gray School of Medicine           and the world will cry."  -1/2 jj^2
+
++ Vanilla-l Mailing List ++
To unsubscribe: send "unsubscribe vanilla-l" to majordomo@real-time.com
For more information: http://archives.real-time.com


References: