home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12471 < prev    next >
Encoding:
Text File  |  1992-08-17  |  3.6 KB  |  68 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: Garbage Collection for C++
  5. Message-ID: <1992Aug18.010246.17678@microsoft.com>
  6. Date: 18 Aug 92 01:02:46 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Aug6.014619.2111@ucc.su.OZ.AU| <DAVEG.92Aug13025629@synaptx.synaptics.com> <1992Aug14.021547.15215@news.mentorg.com>
  9. Lines: 57
  10.  
  11. In article <1992Aug14.021547.15215@news.mentorg.com| bcannard@hppcb36.mentorg.com (Bob Cannard @ PCB x5565) writes:
  12. |In particular, in my own application - which will eventually be quite a big
  13. |program - I only want GC for certain classes, where the problem of determining
  14. |when an object must be deleted has no sensible solution other than GC. Yet
  15. |performance (both run time and memory use) carries a high premium, so I can't
  16. |afford to have GC everywhere.
  17.  
  18. Other way around.  If you need the smallest code, smallest working set, and
  19. the fastest code, you cannot afford not to use GC almost everywhere.  The
  20. standard "manual" memory management techniques of C++ come at a very high cost.
  21.  
  22. |Exactly what I mean. If the GC has to scan the whole stack (and all the
  23. |statics and globals while we're at it), it's doing too much work. The
  24. |reference-count solution is more efficient than that, because the need
  25. |for GC is restricted to a small but important part of the program. The
  26. |idea of languages like C++ is to give the compiler the information it
  27. |needs to get the best performance out of the resulting code.
  28.  
  29. This of course depends on what one is doing, but in my experience stack
  30. scanning GC techniques are many times faster than reference counting.
  31.  
  32. |Quite fast compared to what? In previous projects I've been involved with
  33. |(using C rather than C++) "malloc" and "free" were found to be too slow,
  34. |and were replaced by custom ones. I want this GC to clip along as fast as
  35. |possible.
  36.  
  37. Compare custom allocators/deallocators costs to generational GC techniques, 
  38. where the typical "allocation" cost is to increment one pointer by n bytes,
  39. and the typical "deallocation" cost is zero.  Further, a good compiler can 
  40. lump together many allocations into one pointer bump, making the allocation
  41. cost analogous to "C" stack allocation cost.  IE typical generational allocation
  42. deallocation is as cheap as stack allocation.
  43.  
  44. |This is why special GC pointers have been proposed in the first place.
  45. |If GC is in use, the programmer must accept the "no clever tricks"
  46. |constraint, which includes not passing pointers around disguised as
  47. |integers. And once that constraint is accepted, there may be no good
  48. |reason for not having compaction. My earlier post on this subject
  49. |contained some suggestions that would make the combination of GC and
  50. |existing libraries practical.
  51.  
  52. Moveable objects, as occur for example in compaction, are necessary anyway
  53. to support persistence -- once freezing and thawing an object your object is
  54. in a new location -- therefore there's no excuse not to do what's necessary
  55. for compaction.  Further, moveable objects are necessary in general to maintain
  56. good working sets.
  57.  
  58. |This would accord with existing C++ philosophy, but I don't see how
  59. |you could make a GC that's immune to dangling pointers. Anyone out
  60. |there know otherwise?
  61.  
  62. There's lots of answers to this question.  I'll quote the answer that most
  63. OOPLs implement: "Don't use pointers -- use references instead."  In the 
  64. C++ case we're already stuck with pointers, so the answer then becomes
  65. "make pointers [as we now know them] an anachronism which is only used within
  66.  'unsafe' modules."  See Modula-3 for an example of this approach.
  67.  
  68.