POV-Ray : Newsgroups : povray.off-topic : Games programmers : Re: Games programmers Server Time
10 Oct 2024 13:11:30 EDT (-0400)
  Re: Games programmers  
From: Warp
Date: 11 Sep 2008 17:50:31
Message: <48c992a7@news.povray.org>
Orchid XP v8 <voi### [at] devnull> wrote:
> Warp wrote:

> >   The basic design principle in C++ is that you don't have to pay for
> > what you don't use.
> > 
> >   C++ supports dynamic binding if you need it, but it's not shoved down
> > your throat by force.

> I prefer the Eiffel way, where the compiler looks at your program and 
> automatically decides which things can be bound statically (with all the 
> optimisations that follow), and which things actually require dynamic 
> binding.

  That's incompatible with precompiled and dynamically loadable libraries.

  I have always wondered why so many "fancy" languages seem to completely
disregard the idea of dynamically loadable libraries. The idea with them
is that the OS loads them only *once* into memory for all programs to use.
For example, linux (afaik) loads libc.so only once into memory, and all
the myriad of programs which require libc.so use that single copy in memory
rather than each one loading it separately. (Given that libc is something
like 2 megabytes in size and the amount of programs depending on it and
running at any single time in a typical system is counted on the hundreds,
that's a significant memory usage saving.)

  Of course it makes the size of the executable files smaller (because
libc and other standard libraries don't need to be linked into them),
saving disk space (and making loading times faster).

> Does it actually build a seperate copy of the function for each data 
> type, or just build one generic version that works with all of them?

  How could it use one generic function for all possible data types,
given that the data types can have different sizes and even have completely
different implementations for their operators, etc?

  One common misconception people have about templates is that they
increase the size of the executable because the functions are created
for each used type. However, this is only true in some cases.

  In many cases, however, template functions actually *reduce* the size
of the executable. If you have 20 regular functions somewhere, and you
call only one of them, all the 20 will nevertheless be compiled in the
object file. Then it's up to the linker to remove the ones which are not
called at all. Many linkers don't do this (eg. the gcc linker doesn't),
and those unused 19 functions will be linked into the executable,
incrementing its size, even though they are not used.

  If they were template functions, however, the ones which are never
called are never instantiated. No trace of them whatsoever will be
compiled into the object file nor the executable file. They just don't
exist. Thus they don't increase the size of the executable, unlike
regular unused functions do.

  Inlining can also help reducing the size of the executable, besides
making the program faster.

  The great thing about templates is that the compiler is able to optimize
the code on a per-type basis. For example, if you sort an array of integers,
the compiler will most probably be able to compile each comparison into
a CPU register comparison opcode. If you sort an array of strings, then
it will have to perform a string comparison function call. However, the
original sorting function code was the same. Templates just allow the
compiler to perform better optimizations based on the type.

> >   Yes, debug checks are nice. They are nicer if you can turn them off
> > for a final release.

> There is certainly something to be said for that.

> The problem is that, certainly with C anyway, there's no switch to turn 
> it ON in the first place. :-(

  It depends on the compiler. Some compilers do insert checks in debug
mode.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.