| 1 | \chapter{Performance}
 | 
|---|
| 2 | \label{c:performance}
 | 
|---|
| 3 | 
 | 
|---|
| 4 | Performance has been of secondary importance for most of this project.
 | 
|---|
| 5 | Instead, the focus has been to get the features working. The only performance
 | 
|---|
| 6 | requirements is to ensure the tests for correctness run in a reasonable
 | 
|---|
| 7 | amount of time.
 | 
|---|
| 8 | 
 | 
|---|
| 9 | \section{Test Set-Up}
 | 
|---|
| 10 | Tests will be run in \CFA, C++, Java and Python.
 | 
|---|
| 11 | In addition there are two sets of tests for \CFA,
 | 
|---|
| 12 | one for termination exceptions and once with resumption exceptions.
 | 
|---|
| 13 | 
 | 
|---|
| 14 | C++ is the most comparable language because both it and \CFA use the same
 | 
|---|
| 15 | framework, libunwind.
 | 
|---|
| 16 | In fact, the comparison is almost entirely a quality of implementation
 | 
|---|
| 17 | comparison. \CFA's EHM has had significantly less time to be optimized and
 | 
|---|
| 18 | does not generate its own assembly. It does have a slight advantage in that
 | 
|---|
| 19 | there are some features it does not handle, through utility functions,
 | 
|---|
| 20 | but otherwise \Cpp has a significant advantage.
 | 
|---|
| 21 | 
 | 
|---|
| 22 | Java is another very popular language with similar termination semantics.
 | 
|---|
| 23 | It is implemented in a very different environment, a virtual machine with
 | 
|---|
| 24 | garbage collection.
 | 
|---|
| 25 | It also implements the finally clause on try blocks allowing for a direct
 | 
|---|
| 26 | feature-to-feature comparison.
 | 
|---|
| 27 | As with \Cpp, Java's implementation is more mature, has more optimizations
 | 
|---|
| 28 | and more extra features.
 | 
|---|
| 29 | 
 | 
|---|
| 30 | Python was used as a point of comparison because of the \CFA EHM's
 | 
|---|
| 31 | current performance goals, which is not be prohibitively slow while the
 | 
|---|
| 32 | features are designed and examined. Python has similar performance goals for
 | 
|---|
| 33 | creating quick scripts and its wide use suggests it has achieved those goals.
 | 
|---|
| 34 | 
 | 
|---|
| 35 | Unfortunately there are no notable modern programming languages with
 | 
|---|
| 36 | resumption exceptions. Even the older programming languages with resumptions
 | 
|---|
| 37 | seem to be notable only for having resumptions.
 | 
|---|
| 38 | So instead resumptions are compared to a less similar but much more familiar
 | 
|---|
| 39 | feature, termination exceptions.
 | 
|---|
| 40 | 
 | 
|---|
| 41 | All tests are run inside a main loop which will perform the test
 | 
|---|
| 42 | repeatedly. This is to avoids start-up or tear-down time from
 | 
|---|
| 43 | affecting the timing results.
 | 
|---|
| 44 | Most test were run 1 000 000 (a million) times.
 | 
|---|
| 45 | The Java versions of the test also run this loop an extra 1000 times before
 | 
|---|
| 46 | beginning to time the results to ``warm-up" the JVM.
 | 
|---|
| 47 | 
 | 
|---|
| 48 | Timing is done internally, with time measured immediately before and
 | 
|---|
| 49 | immediately after the test loop. The difference is calculated and printed.
 | 
|---|
| 50 | 
 | 
|---|
| 51 | The loop structure and internal timing means it is impossible to test
 | 
|---|
| 52 | unhandled exceptions in \Cpp and Java as that would cause the process to
 | 
|---|
| 53 | terminate.
 | 
|---|
| 54 | Luckily, performance on the ``give-up and kill the process" path is not
 | 
|---|
| 55 | critical.
 | 
|---|
| 56 | 
 | 
|---|
| 57 | The exceptions used in these tests will always be a exception based off of
 | 
|---|
| 58 | the base exception. This requirement minimizes performance differences based
 | 
|---|
| 59 | on the object model used to repersent the exception.
 | 
|---|
| 60 | 
 | 
|---|
| 61 | All tests were designed to be as minimal as possible while still preventing
 | 
|---|
| 62 | exessive optimizations.
 | 
|---|
| 63 | For example, empty inline assembly blocks are used in \CFA and \Cpp to
 | 
|---|
| 64 | prevent excessive optimizations while adding no actual work.
 | 
|---|
| 65 | 
 | 
|---|
| 66 | % We don't use catch-alls but if we did:
 | 
|---|
| 67 | % Catch-alls are done by catching the root exception type (not using \Cpp's
 | 
|---|
| 68 | % \code{C++}{catch(...)}).
 | 
|---|
| 69 | 
 | 
|---|
| 70 | \section{Tests}
 | 
|---|
| 71 | The following tests were selected to test the performance of different
 | 
|---|
| 72 | components of the exception system.
 | 
|---|
| 73 | The should provide a guide as to where the EHM's costs can be found.
 | 
|---|
| 74 | 
 | 
|---|
| 75 | \paragraph{Raise and Handle}
 | 
|---|
| 76 | The first group of tests involve setting up
 | 
|---|
| 77 | So there is three layers to the test. The first is set up and a loop, which
 | 
|---|
| 78 | configures the test and then runs it repeatedly to reduce the impact of
 | 
|---|
| 79 | start-up and shutdown on the results.
 | 
|---|
| 80 | Each iteration of the main loop
 | 
|---|
| 81 | \begin{itemize}[nosep]
 | 
|---|
| 82 | \item Empty Function:
 | 
|---|
| 83 | The repeating function is empty except for the necessary control code.
 | 
|---|
| 84 | \item Destructor:
 | 
|---|
| 85 | The repeating function creates an object with a destructor before calling
 | 
|---|
| 86 | itself.
 | 
|---|
| 87 | \item Finally:
 | 
|---|
| 88 | The repeating function calls itself inside a try block with a finally clause
 | 
|---|
| 89 | attached.
 | 
|---|
| 90 | \item Other Handler:
 | 
|---|
| 91 | The repeating function calls itself inside a try block with a handler that
 | 
|---|
| 92 | will not match the raised exception. (But is of the same kind of handler.)
 | 
|---|
| 93 | \end{itemize}
 | 
|---|
| 94 | 
 | 
|---|
| 95 | \paragraph{Cross Try Statement}
 | 
|---|
| 96 | The next group measures the cost of a try statement when no exceptions are
 | 
|---|
| 97 | raised. The test is set-up, then there is a loop to reduce the impact of
 | 
|---|
| 98 | start-up and shutdown on the results.
 | 
|---|
| 99 | In each iteration, a try statement is executed. Entering and leaving a loop
 | 
|---|
| 100 | is all the test wants to do.
 | 
|---|
| 101 | \begin{itemize}[nosep]
 | 
|---|
| 102 | \item Handler:
 | 
|---|
| 103 | The try statement has a handler (of the matching kind).
 | 
|---|
| 104 | \item Finally:
 | 
|---|
| 105 | The try statement has a finally clause.
 | 
|---|
| 106 | \end{itemize}
 | 
|---|
| 107 | 
 | 
|---|
| 108 | \paragraph{Conditional Matching}
 | 
|---|
| 109 | This group of tests checks the cost of conditional matching.
 | 
|---|
| 110 | Only \CFA implements the language level conditional match,
 | 
|---|
| 111 | the other languages must mimic with an ``unconditional" match (it still
 | 
|---|
| 112 | checks the exception's type) and conditional re-raise if it was not supposed
 | 
|---|
| 113 | to handle that exception.
 | 
|---|
| 114 | \begin{itemize}[nosep]
 | 
|---|
| 115 | \item Match All:
 | 
|---|
| 116 | The condition is always true. (Always matches or never re-raises.)
 | 
|---|
| 117 | \item Match None:
 | 
|---|
| 118 | The condition is always false. (Never matches or always re-raises.)
 | 
|---|
| 119 | \end{itemize}
 | 
|---|
| 120 | 
 | 
|---|
| 121 | %\section{Cost in Size}
 | 
|---|
| 122 | %Using exceptions also has a cost in the size of the executable.
 | 
|---|
| 123 | %Although it is sometimes ignored
 | 
|---|
| 124 | %
 | 
|---|
| 125 | %There is a size cost to defining a personality function but the later problem
 | 
|---|
| 126 | %is the LSDA which will be generated for every function.
 | 
|---|
| 127 | %
 | 
|---|
| 128 | %(I haven't actually figured out how to compare this, probably using something
 | 
|---|
| 129 | %related to -fexceptions.)
 | 
|---|
| 130 | 
 | 
|---|
| 131 | \section{Results}
 | 
|---|
| 132 | Each test is was run five times, the best and worst result were discarded and
 | 
|---|
| 133 | the remaining values were averaged.
 | 
|---|
| 134 | 
 | 
|---|
| 135 | In cases where a feature is not supported by a language the test is skipped
 | 
|---|
| 136 | for that language. Similarly, if a test is does not change between resumption
 | 
|---|
| 137 | and termination in \CFA, then only one test is written and the result
 | 
|---|
| 138 | was put into the termination column.
 | 
|---|
| 139 | 
 | 
|---|
| 140 | \begin{tabular}{|l|c c c c c|}
 | 
|---|
| 141 | \hline
 | 
|---|
| 142 |               & \CFA (Terminate) & \CFA (Resume) & \Cpp & Java & Python \\
 | 
|---|
| 143 | \hline
 | 
|---|
| 144 | Raise Empty   & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 \\
 | 
|---|
| 145 | Raise D'tor   & 0.0 & 0.0 & 0.0 & N/A & N/A \\
 | 
|---|
| 146 | Raise Finally & 0.0 & 0.0 & N/A & 0.0 & 0.0 \\
 | 
|---|
| 147 | Raise Other   & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 \\
 | 
|---|
| 148 | Cross Handler & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 \\
 | 
|---|
| 149 | Cross Finally & 0.0 & N/A & N/A & 0.0 & 0.0 \\
 | 
|---|
| 150 | Match All     & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 \\
 | 
|---|
| 151 | Match None    & 0.0 & 0.0 & 0.0 & 0.0 & 0.0 \\
 | 
|---|
| 152 | \hline
 | 
|---|
| 153 | \end{tabular}
 | 
|---|