Weight.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <html lang="en">
  2. <head>
  3. <title>Weight - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="Metrics-and-Maintenance.html#Metrics-and-Maintenance" title="Metrics and Maintenance">
  9. <link rel="prev" href="Profile.html#Profile" title="Profile">
  10. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  11. <meta http-equiv="Content-Style-Type" content="text/css">
  12. <style type="text/css"><!--
  13. pre.display { font-family:inherit }
  14. pre.format { font-family:inherit }
  15. pre.smalldisplay { font-family:inherit; font-size:smaller }
  16. pre.smallformat { font-family:inherit; font-size:smaller }
  17. pre.smallexample { font-size:smaller }
  18. pre.smalllisp { font-size:smaller }
  19. span.sc { font-variant:small-caps }
  20. span.roman { font-family:serif; font-weight:normal; }
  21. span.sansserif { font-family:sans-serif; font-weight:normal; }
  22. --></style>
  23. </head>
  24. <body>
  25. <div class="node">
  26. <a name="Weight"></a>
  27. <p>
  28. Previous:&nbsp;<a rel="previous" accesskey="p" href="Profile.html#Profile">Profile</a>,
  29. Up:&nbsp;<a rel="up" accesskey="u" href="Metrics-and-Maintenance.html#Metrics-and-Maintenance">Metrics and Maintenance</a>
  30. <hr>
  31. </div>
  32. <h5 class="subsubsection">2.7.7.4 Weight</h5>
  33. <p>The following virtual code implements a function that returns the weight
  34. of its argument in the standard representation for natural numbers.
  35. <a name="index-g_t_0040code_007bweight_007d-286"></a>
  36. <pre class="example"> weight = ((nil,nil),((nil,nil),(nil,(nil,nil))))
  37. </pre>
  38. <p class="noindent">The weight of a tree is zero if the tree is <code>nil</code>, and otherwise
  39. the sum of the weights of the two subtrees plus one.
  40. <p>An algorithm to compute the weight of a tree would be trivial to
  41. implement without being built in to the virtual machine. The built in
  42. capability could also be used for purposes unrelated to code
  43. maintenance. Nevertheless, it is built in for the following reasons.
  44. <ul>
  45. <li>Computing weights happened to be a bottleneck for a particular
  46. aspect of code generation that was of interest to the author,
  47. <a name="index-compression-287"></a>namely the compression of generated code.
  48. <li>A built in implementation in C runs at least an order of magnitude
  49. faster than the equivalent implementation in virtual code.
  50. <li>It runs even faster when repeated on the same data, by retrieving
  51. previously calculated weights rather than recalculating them.
  52. </ul>
  53. <p>The only disadvantage of using this feature instead of implementing a
  54. function in virtual code to compute weights is that it relies on native
  55. <a name="index-native-integer-arithmetic-288"></a><a name="index-overflow-289"></a>integer arithmetic and could overflow, causing a fatal error. It has
  56. never occurred in practice, but is possible due to sharing, whereby the
  57. nominal weight of a tree could be exponentially larger than the actual
  58. amount of memory occupied by it.
  59. </body></html>