Re: how to make two references to one string that stay refered to the same string reguardless of the changing value in the string?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 11/03/04


Date: Wed, 3 Nov 2004 15:26:15 -0000

Zach <not@this.address> wrote:
> "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
> news:MPG.1bf2f97a8da9b08b98b870@msnews.microsoft.com...
> > Zach <not@this.address> wrote:
> > > John - hopefully not a terribly dumb question - surely, if you want
> > > to save room and a value is likely to be repeated very often, you can
> > > use a code list, provided of course that the code value is less
> > > memory consuming than the value returned by the code list?
> >
> > I'm not entirely sure what you mean - could you clarify your question?
>
> I understood that the problem was that a certain value might be repeated
> many times in different relationships and that you were providing a medicine
> to keep memory usage down to a minimum.
>
> Thag is why I thouht, rather than filing Monty Ville, you could file 12,
> and have a conversion table to say that 12 translates into Monty Ville.
> This is the point I was attempting to make, and wondered whether
> I was missing the essence of what you were explaining. Obviously
> my example implies that there are lot of, albeit limited number of,
> names of towns.

Given that you've got to store "Monty Ville" somewhere, you might as
well just have several references to the same string. If you know
you'll only have 256 town names, you could store each value as a byte
index, of course, and likewise if you have only 65536 names you could
store each value as a ushort. It's typically not going to be worth
doing that though.

The code I gave will make sure that only one copy of the town name is
used (with other copies just being temporary). Every reference to
"Monty Ville" will be a reference to the same string object. References
are pretty cheap in memory - four bytes in the current CLR
implementation.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: Writing huge Sets() to disk
    ... pushes it's internal dictionaries ... > Almost anything you do copies references. ... > store the values on disc in a reasonably efficient manner. ... bsddb creaps on me that I can store as a key or value only a string. ...
    (comp.lang.python)
  • Re: Which database design is better
    ... you can update primary keys because you can ... of employee. ... Remember that part in the specs in this thread about each store ... then I can use a REFERENCES clause. ...
    (microsoft.public.sqlserver.programming)
  • Re: object references
    ... > performance reasons) so I don't have to recreated the objects when I need ... > stored in the hashtable has no more references to it? ... You'd need to use WeakReferences - store the WeakReference in the table ...
    (microsoft.public.dotnet.languages.csharp)
  • Inheritance, vectors and copy
    ... and two derived classes: ... the same references in v2 and v3, ... object in the vector and store the reference to the copy in v3. ... call a virtual self-copy method; ...
    (comp.lang.cpp)