home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / std / cplus / 2029 < prev    next >
Encoding:
Text File  |  1993-01-11  |  7.2 KB  |  154 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!walter!qualcom.qualcomm.com!harvey.qualcomm.com!greg
  3. From: greg@harvey.qualcomm.com (Greg Noel)
  4. Subject: Re: Use of nested functions (Was: Proposal for default scope)
  5. Message-ID: <1993Jan11.215111.11291@qualcomm.com>
  6. Sender: news@qualcomm.com
  7. Nntp-Posting-Host: harvey.qualcomm.com
  8. Organization: Qualcomm, Inc., San Diego, CA
  9. References: <1993Jan9.182938.16874@ucc.su.OZ.AU>
  10. Date: Mon, 11 Jan 1993 21:51:11 GMT
  11. Lines: 141
  12.  
  13. I'm beginning to wish I'd never mentioned nested functions in my original
  14. article.  Now people are trying to cast me in the role of opposing nested
  15. functions, when I really don't care at all.
  16.  
  17. At the risk of being accused of going for the pun, let me put nested
  18. functions in context.
  19.  
  20. What I said in my original article was that C++ needed some sort of type-
  21. safe way of providing helper functions within an implementation.  The syntax
  22. in Marko Kohtala's suggestion seemed to provide that and I thought it was
  23. interesting in that it might provide a basis for eventually providing
  24. modules/packages in C++.
  25.  
  26. Out of about 75 lines, most of the reaction has been due to one sentence that
  27. pointed out that this approach might take away the need for nested functions.
  28. Now, that was overstated; nested functions can also be used for callbacks.
  29. That's a very different capability; if that needs to be mooted, you should
  30. be discussing if the costs of providing it are worth the benefits.
  31.  
  32. John MAX Skaller <maxtal@extro.ucc.su.OZ.AU> writes:
  33. >>>If a local function never has its address taken, then it does not use
  34. >>>the full power of nested functions.
  35. >>
  36. >>This is a valid point, but this is exactly the part that is expensive to
  37. >>implement.  
  38. >
  39. >    Not necessarily.
  40.  
  41. As long as you never take the address of a nested function, they can be
  42. implemented very efficiently since the compiler has full control---no need
  43. for a display or for trampolines.  Once you take the address of a nested
  44. function, you are talking about callbacks, which are more expensive and
  45. have some, ah, interesting interactions with other language features.  It
  46. is my impression that these costs cannot be eliminated.
  47.  
  48. Ignoring displays and trampolines for the moment, which I think can be
  49. pretty much confined to the functions which have nested functions, both
  50. recursion and non-local gotos have to be handled.  These may have costs
  51. even when nested functions are never used.
  52.  
  53. Recursion implies that the context of a pointer must be kept as part of
  54. the pointer, which leads to either expanding the size of a function pointer
  55. or generating trampoline code dynamically.  Expanding function pointers
  56. would be a cost even when nested functions are not used and there are
  57. some architectures where code cannot be generated on the fly.
  58.  
  59. Non-local gotos mean that the compiler has to be able to unwind the stack
  60. to get to the label being referenced.  C++ already has two mechanisms to
  61. unwind the stack, and one of them is a time-encrusted grotty hack; I don't
  62. know if we need a third.
  63.  
  64. I'm sure there are other interactions; those are just the first two that
  65. popped to mind.
  66.  
  67. Returning to displays and trampolines, the usual implementation of displays
  68. uses implicit static variables, which means the code is not thread-safe.
  69. If static variables are not used, an extra machine register is usually
  70. needed to keep track of where the display lives.  And to pass the context
  71. implicitly to the inner function, trampoline code may need to be generated
  72. on the fly.  Again, there are some architectures where this is not possible.
  73.  
  74. It's been more than ten years since we discussed these issues on the DOD-1
  75. committee, so there may be some newer implementation techinques that don't
  76. imply these costs.  If so, I'd be pleased to hear about them.  But as far
  77. as I know, there's no magic wand that will make them disappear.
  78.  
  79. >>The ``Spirit of C'' is that expensive things are exposed to
  80. >>the programmer; C++ inherits much of this spirit.
  81. >
  82. >    Not entirely. Building objects can be VERY expensive.
  83. >Have a look at some non-optimised code for a constructor of
  84. >a complex object sometime.
  85.  
  86. Like a function call, the cost of which cannot always be determined from
  87. the outside, it is under the control of the programmer.  If necessary,
  88. it can be replaced.  Cost due to the language is not always possible to
  89. avoid, and is not (directly) under the control of the programmer.  Perhaps
  90. ``exposed'' was a poor word choice, but ``accessible'' doesn't have quite
  91. the right flavor, either.  Maybe ``controlled?''  Or ``can be managed by?''
  92.  
  93. >    And the flip side: C traditionally provides facilities
  94. >corresponding to the underlying hardware. Many CISC machines,
  95. >including the 386 and 68000 have instructions and architecture
  96. >*specifically* designed to support nested functions.
  97. >
  98. >    Thus NOT providing nested functions is against the spirit of C.
  99.  
  100. Hmmmm....  I don't think I buy into this point.  There are some machines
  101. that have hardware garbage collection, and I don't see that being used as
  102. an argument for garbage collection in C/C++.  (That is NOT a statement
  103. either for _or_ against garbage collection!)
  104.  
  105. >>In
  106. >>a heavily optimizing compiler that keeps many values in registers, it can
  107. >>be costly to have to re-sync memory every time a nested function _might_
  108. >>be invoked.
  109. >    Dont understand '_might_'. Either one is invoked or not.
  110.  
  111. As an example, once you've taken the address of a nested function, it _might_
  112. be executed when _any_ external function is called.  In general, the compiler
  113. can't tell where the pointer was stashed, so it has to assume that memory
  114. has to be re-synced.  You and I may know that strcmp() or qsort() don't do
  115. a callback, but the compiler can't be sure.
  116.  
  117. >>(BTW, I'd like to have nested functions, but I would not make pointers to
  118. >>them be compatible with a ``normal'' function pointer.  
  119. >
  120. >    Do we require nested functions to have the same form
  121. >as ordinary ones? If not, how are they to be distinguished?
  122.  
  123. Useful as it might be, I don't think that a special form for inexpensive
  124. function pointers (similar to the member function pointer) would be what
  125. the proponents of nested functions want.  I understand that position---I
  126. could see the use of a facility that created a callback by wrapping a member
  127. function and an object together and the same mechanism could then be used to
  128. create a nested callback as well.  That way, the cost would be more visible;
  129. it wouldn't be needed unless the programmer explicitly created a callback
  130. pointer.
  131.  
  132. But I'm not really willing to argue either side of that one.
  133.  
  134. >>But, as others have pointed out, it's possible for you, the programmer, to
  135. >>do the expensive thing 
  136. >
  137. >    Yes but VERY clumbsily :-)
  138. >
  139. >>that the compiler would have to do, 
  140. >
  141. >    For me to explicitly keep track of the display is sure
  142. >to be slower --- even on a machine without hardware support
  143. >for nested functions --- than proper compiler generated code.
  144.  
  145. Probably, but not necessarily.  If the compiler can create better code
  146. for 99 and 44/100ths of the programs, it might be a net gain.  Yes,
  147. your program might be slower, but the vast majority of programs might
  148. be just a little bit faster and more than make up the difference.
  149.  
  150. That's another one I'm not willing to argue---I want _my_ programs to
  151. run fast, too....
  152. -- 
  153. -- Greg Noel, Unix Guru         greg@qualcomm.com  or  greg@noel.cts.com
  154.