home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / pop / 89 < prev    next >
Encoding:
Internet Message Format  |  1992-11-22  |  4.6 KB

  1. Path: sparky!uunet!pipex!warwick!uknet!news.cs.bham.ac.uk!axs
  2. From: axs@cs.bham.ac.uk (Aaron Sloman)
  3. Newsgroups: comp.lang.pop
  4. Subject: Re: Clarity VS Efficiency in POP11
  5. Keywords: efficiency vs elegance
  6. Message-ID: <By556G.CoB@cs.bham.ac.uk>
  7. Date: 22 Nov 92 23:27:52 GMT
  8. References: <TMR.92Nov21122734@emotsun.bham.ac.uk> <1992Nov22.131443.11011@syma.sussex.ac.uk>
  9. Sender: news@cs.bham.ac.uk
  10. Organization: School of Computer Science, University of Birmingham, UK
  11. Lines: 112
  12. Nntp-Posting-Host: emotsun
  13.  
  14. ianr@syma.sussex.ac.uk (Ian Rogers) writes:
  15.  
  16. > Date: 22 Nov 92 13:14:43 GMT
  17. > Organization: University of Sussex at Brighton
  18. >
  19. > tmr@cs.bham.ac.uk (Tim Read) writes:
  20. > > It is my feeling as a competent beginner in POP11, that putting too many
  21. > > efficiency hacks into your code tends to lose the inherent clarity that
  22. > > POP11 provides.
  23. >
  24. > Efficiency *hacks* are bad, sure (Pop11 buys you clarity, a Good
  25. > Thing IMHO)
  26. (Tim)
  27. > > An example of what I mean is using a temporary closure within a
  28. > > loop, which looks nice and neat, but will cause extra garbage collections,
  29. > > as opposed to using an lblock (As Aaron pointed out).
  30. (Ian)
  31. > So make a new data type (even notionally) and optimise the code,
  32. > *after* you've finished development, by free-listing instances.
  33. > ....
  34. > In your case you can free-list the closures, either in a simple list
  35. > if you're only making closures of a single procedure, or in a
  36. > property that matches against the pdpart.
  37. > ... example of how to make a freelist of closures....
  38.  
  39. Two points
  40.  
  41. a. Pop should support freelists for various kinds of datastructures,
  42. for knowledgeable users without it being necessary for individuals
  43. to create these mechanisms.  Poplog Pop-11 already does this for
  44. lists (actually that was originally put in by John Gibson for system
  45. use and I arranged for it to be exported for general use (in 1986,
  46. for Poplog V13.6)). Steve Knight has been arguing for ages that the
  47. mechanism should be generalised, and he is right. On the other hand
  48. you are right that users *can* do it themselves to reduce garbage
  49. collections, if they know enough. (Sometimes I think that if they
  50. need to be told how to do it then they don't know enough to do it
  51. safely!!!)
  52.  
  53. b. However, the sort of case brought up by Tim was one where I don't
  54. think a free list is the answer. It involved replacing applist with
  55. a for loop, inside an already nested pair of loops. Something like
  56. this:
  57.  
  58.   for x_iter from X1 to X2 do
  59.       for y_iter from Y1 to Y2 do
  60.           applist
  61.             (
  62.               world(x_iter, y_iter),
  63.                 extract_detail(% x_iter - grid_x, y_iter - grid_y %)
  64.             );
  65.       endfor;
  66.     endfor;
  67.  
  68. (I've slightly simplified the original example.) Here the second
  69. argument to applist is a closure, and a new one is created on every
  70. call of applist, then discarded. I don't believe it's worth creating
  71. a free list of closures to prevent garbage collection in *this* sort
  72. of context because I think replacing applist with an explicit "for"
  73. look is actually not a *hack* in this case.
  74.  
  75. It is not quite as elegant as applist, but I think it is perfectly
  76. acceptable, and for some people will be even clearer than the call
  77. of applist. I.e.
  78.  
  79.   lblock; lvars item;
  80.  
  81.   for x_iter from X1 to X2 do
  82.       for y_iter from Y1 to Y2 do
  83.           for item in world(x_iter, y_iter) do
  84.               extract_detail( item, x_iter - grid_x, y_iter - grid_y )
  85.           endfor
  86.       endfor;
  87.   endfor;
  88.  
  89.   endlblock;
  90.  
  91. Anyone who doesn't like three nested for loops could, instead define
  92. an additional procedure:
  93.  
  94.     define extract_location_details(list, xloc, yloc, grid_x, grid_y);
  95.         lvars item, list, xloc, yloc, grid_x, grid_y;
  96.         for item in list do
  97.            extract_detail(item, (xloc - grid_x), (yloc - grid_y))
  98.         endfor
  99.     enddefine;
  100.  
  101. then the original would be replaced by
  102.  
  103.   for x_iter from X1 to X2 do
  104.       for y_iter from Y1 to Y2 do
  105.         extract_location_details
  106.             (world(x_iter, y_iter), x_iter, y_iter, grid_x, grid_y)
  107.       endfor
  108.   endfor;
  109.  
  110. which I think is at least as clear and elegant as the call of
  111. applist, and considerably more efficient, especially if "define
  112. procedure" is used instead of just "define" to make sure that
  113. there's no procedure type check on every iteration.
  114.  
  115. It's also more modular, as the introduction of the extra procedure
  116. allows it to be reused, and to be redefined, e.g. during testing,
  117. without tampering with the middle of a couple of nested loops.
  118.  
  119. Aaron
  120. ---
  121. -- 
  122. Aaron Sloman, School of Computer Science,
  123. The University of Birmingham, B15 2TT, England
  124. EMAIL   A.Sloman@cs.bham.ac.uk  OR A.Sloman@bham.ac.uk
  125. Phone: +44-(0)21-414-3711       Fax:   +44-(0)21-414-4281
  126.