lowering memory usage

From: Chris Gilbert (chris@buzzbee.freeserve.co.uk)
Date: 11/20/99


A while back I asked about ways save memory, I know a few ppl were
interested in this and were interested in what I did.

What I've done is use reference counting to strings in objects and
mobs.  The basis for it was taken from the src for death gates mud,
(which is actually full of lots of nifty things).  The idea is that you
have a struct like;

/* shared string struct */
typedef struct {
  char *str;
  int count;
} sstring;

then use functions to access it.

/* allocates and sets up a sstring struct */
sstring *ss_create(char *str);

/* share an sstring, this just increments the count in the struct */
sstring *ss_share(sstring *ss);

/* free a sstring, this decrements the count and when it reaches 0 frees
it */
void ss_free(string *ss);

/* to avoid a performance hit you then use a macro to access the string
#define ss_data(ss)   ((ss) ? (ss)->str : (char *) NULL)

/* to copy an existing struct you'd use (I should probably macro or
functionise it for readability) */
ss_create(ss_data(existing string));

This does mean you have to be more careful when copying data structures
around that you share or copy the data.

What this means is you lower the number of copies of a string in the
game, IE you've got 10 objects in the game they have 10 copies of the
strings (I think they do, I can't remember now)  using this you've got 1
struct as above in the proto, and then 10 pointers to it.

Also using this interface allows for expansion to a more general string
sharing method (across the whole mud), or to add compression of some
sort in.  IE you get the interface funcs in and working, then you can
meddle with the backend of it and work out what suits your mud best.

I'm not sure of the exact savings I've not actually checked/got all the
bits I want to share in yet, but I think it's already saved some space.

If ppl want the src for this I'd suggest looking at the death gates src
code from:

I don't plan to do a patch/snippet as this is something that would have
to be done on a per mud basis.


     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |

This archive was generated by hypermail 2b30 : 12/15/00 PST