[990] in Coldmud discussion meeting

root meeting help first previous next next in chain last in chain last

[COLD] Possible optimization idea..

daemon@ATHENA.MIT.EDU (Thu May 2 19:20:57 1996 )

Date: Thu, 2 May 1996 16:55:37 -0600 (MDT)
From: Brandon Gillespie <brandon@tombstone.sunrem.com>
To: coldstuff@cold.org

I have noticed that there is a very common construct in ColdC, and I am 
considering adding an optimization for this construct.  Basically, you 
will often see a variable initialized to a base data type, then x 
elements are added to the variable and the result is returned.  The 
problem here is you can end up having quite a few reallocs leading to 
fragmentation and other problems, if the increments are small enough and 
the end result is very large.  The idea would be to provide an 
'initializer' function of sorts, for this case called 'new()' where you 
specify the data type and the anticipated size.  The driver can ignore 
this directive, or it can take it into account--calling 'new()' will 
never throw an error, and may simply be equivalent to doing 'bar = "";'.  

An example of where this could be very useful is taken from the catalog 
server I am working on.  This is pseudo code to clarify things:

    images = ["008-123", "008-141", ..];
    serv = "<img src=\""+ $sys.primary_server() +"/images/catalog/";    
    line = "";
    for i in (images)   
        line += t + i + ".gif\">\n";

Assuming 'images' were 20 elements long, the end result would be a string 
that is around 1100 characters in length.  With the addition of a 'new()'
function the above code changes to:

    images = ["008-123", "008-141", ..];
    serv = "<img src=\""+ $sys.primary_server() +"/images/catalog/";    
    line = new('string, listlen(images) * (strlen(serv) + 14));
    for i in (images)   
        line += t + i + ".gif\">\n";

The variable 'line' would still begin with no elements (""), but it would 
possibly have much more starting memory allocated (other than the minimal 
starting-size we have now).  Keep in mind, calling 'new()' would not 
guarantee memory allocated for the given elements--I would most 
definitely have a disabling option to this (leaving it equivalent to a 
simple 'line = "";').  However, in certain situations I can see it giving 
a definite bonus to things.

Pros: mentioned above
Cons:

  people could easilly max out a server by 'new()'ing a large number
  of elements.  However they can do this already.  You could disable
  new() to keep them from the immediate situation, or you can create
  a wrapper around .compile() which is:

      if (!$sys.is_admin(sender())) {
          for line in (code) {
              if (match_regexp("[^\.a-z_]new\(", line))
                  throw(~invfunc, "Only administrators may use new()");
          }
      }

  new() can also be a problem if the person coding with it is not
  aware of how references are handled.  If they are using it in a
  situation where the string would simply be copied anyway, it could
  end up creating slower code..

Comments?  Is this too low a level of functionality for ColdC?  It is 
simply a hinting mechanism intended for optimization, not necessarily a 
direct memory allocation system like in other languages...

-Brandon Gillespie