- Timestamp:
- Apr 15, 2017, 12:54:03 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- b23d969
- Parents:
- 1c38f5b (diff), 160af1e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- doc
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/bibliography/cfa.bib
r1c38f5b rf891424 433 433 keywords = {Parametric polymorphism, alphard, iterators, nested types}, 434 434 contributer = {gjditchfield@plg}, 435 key = {Alphard}, 435 436 editor = {Mary Shaw}, 436 437 title = {{ALPHARD}: Form and Content}, … … 861 862 862 863 @techreport{C11, 863 type 864 type = {International Standard}, 864 865 keywords = {ISO/IEC C 11}, 865 866 contributer = {pabuhr@plg}, … … 872 873 873 874 @techreport{C++Concepts, 874 type 875 type = {International Standard}, 875 876 keywords = {ISO/IEC TS 19217:2015}, 876 877 contributer = {a3moss@uwaterloo.ca}, … … 5317 5318 title = {Programming with Sets: An Introduction to {SETL}}, 5318 5319 publisher = {Springer}, 5320 address = {New York, NY, USA}, 5319 5321 year = 1986, 5320 5322 } … … 6312 6314 } 6313 6315 6314 @ article{Sutter15,6316 @online{Sutter15, 6315 6317 contributer = {pabuhr@plg}, 6316 6318 author = {Herb Sutter and Bjarne Stroustrup and Gabriel Dos Reis}, … … 6764 6766 number = 6, 6765 6767 month = jun, 6768 publisher = {ACM}, 6769 address = {New York, NY, USA}, 6766 6770 year = 1990, 6767 6771 pages = {127-136}, -
doc/generic_types/generic_types.tex
r1c38f5b rf891424 194 194 The new constructs are empirically compared with both standard C and \CC; the results show the new design is comparable in performance. 195 195 196 196 197 \subsection{Polymorphic Functions} 197 198 \label{sec:poly-fns} 198 199 199 200 \CFA's polymorphism was originally formalized by \citet{Ditchfield92}, and first implemented by \citet{Bilson03}. 200 The signature feature of \CFA is parametric-polymorphic functions where functions are generalized using a @forall@ clause (giving the language its name):201 The signature feature of \CFA is parametric-polymorphic functions~\citep{forceone:impl,Cormack90} where functions are generalized using a @forall@ clause (giving the language its name): 201 202 \begin{lstlisting} 202 203 `forall( otype T )` T identity( T val ) { return val; } … … 211 212 An advantage of this design is that, unlike \CC template-functions, \CFA polymorphic-functions are compatible with C \emph{separate compilation}, preventing compilation and code bloat. 212 213 213 Since bare polymorphic-types provide only a narrow set of available operations, \CFA provides a \emph{type assertion} mechanism to provide further type information, where type assertions may be variable or function declarations that depend on a polymorphic type-variable.214 Since bare polymorphic-types provide only a narrow set of available operations, \CFA provides a \emph{type assertion}~\cite{alphard} mechanism to provide further type information, where type assertions may be variable or function declarations that depend on a polymorphic type-variable. 214 215 For example, the function @twice@ can be defined using the \CFA syntax for operator overloading: 215 216 \begin{lstlisting} … … 291 292 \smallskip\par\noindent 292 293 \lstMakeShortInline@% 293 He nce, the single name @MAX@ replaces all the C type-specific names: @SHRT_MAX@, @INT_MAX@, @DBL_MAX@.294 Here, the single name @MAX@ replaces all the C type-specific names: @SHRT_MAX@, @INT_MAX@, @DBL_MAX@. 294 295 As well, restricted constant overloading is allowed for the values @0@ and @1@, which have special status in C, \eg the value @0@ is both an integer and a pointer literal, so its meaning depends on context. 295 296 In addition, several operations are defined in terms values @0@ and @1@, \eg: … … 298 299 if (x) x++ $\C{// if (x != 0) x += 1;}$ 299 300 \end{lstlisting} 300 Every if statement in C compares the condition with @0@, and every increment and decrement operator is semantically equivalent to adding or subtracting the value @1@ and storing the result.301 Every if and iteration statement in C compares the condition with @0@, and every increment and decrement operator is semantically equivalent to adding or subtracting the value @1@ and storing the result. 301 302 Due to these rewrite rules, the values @0@ and @1@ have the types @zero_t@ and @one_t@ in \CFA, which allows overloading various operations for new types that seamlessly connect to all special @0@ and @1@ contexts. 302 303 The types @zero_t@ and @one_t@ have special built in implicit conversions to the various integral types, and a conversion to pointer types for @0@, which allows standard C code involving @0@ and @1@ to work as normal. … … 319 320 \end{lstlisting} 320 321 321 In fact, the set of trait operators is incomplete, as there is no assignment requirement for type @T@, but @otype@ is syntactic sugar for the following implicit trait:322 In fact, the set of @summable@ trait operators is incomplete, as it is missing assignment for type @T@, but @otype@ is syntactic sugar for the following implicit trait: 322 323 \begin{lstlisting} 323 324 trait otype( dtype T | sized(T) ) { // sized is a pseudo-trait for types with known size and alignment … … 515 516 [ int, int ] div( int num, int den ); $\C{// return two integers}$ 516 517 [ double, double ] div( double num, double den ); $\C{// return two doubles}$ 517 int q, r; $\C{// overload variable names}$518 int q, r; $\C{// overloaded variable names}$ 518 519 double q, r; 519 520 [ q, r ] = div( 13, 5 ); $\C{// select appropriate div and q, r}$ 520 [ q, r ] = div( 13.5, 5.2 ); 521 [ q, r ] = div( 13.5, 5.2 ); $\C{// assign into tuple}$ 521 522 \end{lstlisting} 522 523 Clearly, this approach is straightforward to understand and use; 523 524 therefore, why do few programming languages support this obvious feature or provide it awkwardly? 524 525 The answer is that there are complex consequences that cascade through multiple aspects of the language, especially the type-system. 525 This section show these consequences and how \CFA deals withthem.526 This section show these consequences and how \CFA handles them. 526 527 527 528 … … 536 537 printf( "%d %d\n", div( 13, 5 ) ); $\C{// return values seperated into arguments}$ 537 538 \end{lstlisting} 538 Here, the values returned by @div@ are composed with the call to @printf@ .539 Here, the values returned by @div@ are composed with the call to @printf@ by flattening the tuple into separate arguments. 539 540 However, the \CFA type-system must support significantly more complex composition: 540 541 \begin{lstlisting} … … 552 553 553 554 An important observation from function composition is that new variable names are not required to initialize parameters from an MRVF. 554 \CFA also allows declaration of tuple variables that can be initialized from an MRVF, since it can be awkward to declare multiple variables of different types. 555 As a consequence, \CFA allows declaration of \emph{tuple variables} that can be initialized from an MRVF, \eg: 555 \CFA also allows declaration of tuple variables that can be initialized from an MRVF, since it can be awkward to declare multiple variables of different types, \eg: 556 556 \begin{lstlisting} 557 557 [ int, int ] qr = div( 13, 5 ); $\C{// tuple-variable declaration and initialization}$ … … 665 665 x.[0, 1] = x.[1, 0]; $\C[1in]{// rearrange: [x.0, x.1] = [x.1, x.0]}$ 666 666 f( x.[0, 3] ); $\C{// drop: f(x.0, x.3)}\CRT{}$ 667 [int, int, int] y = x.[2, 0, 2]; // duplicate: [y.0, y.1, y.2] = [x.2, x.0. 668 x.2] 667 [int, int, int] y = x.[2, 0, 2]; // duplicate: [y.0, y.1, y.2] = [x.2, x.0.x.2] 669 668 \end{lstlisting} 670 669 \end{tabular} … … 954 953 Instead, the presented benchmarks show the costs of idiomatic use of each language's features to examine common usage. 955 954 Figure~\ref{fig:MicroBenchmark} shows the \CFA benchmark tests for a generic stack based on a singly linked-list, a generic pair-data-structure, and a variadic @print@ routine similar to that in Section~\ref{sec:variadic-tuples}. 956 The tests are similar for C and \CC. 957 The first two experiments use element types @int@ and @pair( _Bool, char)@, and push $N=40M$ elements on a generic stack, copy the stack, clear one of the stacks, and find the maximum value in the other stack. 958 The last experiment creates a file and prints $N=40M$ elements of type @int@ and @pair( _Bool, char)@ using a variadic print. 959 960 The structure of each implemented is: C with @void *@-based polymorphism, \CFA with the different presented features, \CC with templates, and \CC using only class inheritance for polymorphism, called \CCV. 955 The benchmark tests are similar for C and \CC. 956 The experiment uses element types @int@ and @pair( _Bool, char)@, and push $N=40M$ elements on a generic stack, copy the stack, clear one of the stacks, find the maximum value in the other stack, and print $N$ constant values. 957 958 The structure of each benchmark implemented is: C with @void *@-based polymorphism, \CFA with the different presented features, \CC with templates, and \CC using only class inheritance for polymorphism, called \CCV. 961 959 The \CCV variant illustrates an alternative object-oriented idiom where all objects inherit from a base @object@ class, mimicking a Java-like interface; 962 960 hence runtime checks are necessary to safely down-cast objects. … … 965 963 For the print benchmark, idiomatic printing is used: the C and \CFA variants used @cstdio.h@, while the \CC and \CCV variants used @iostream@. 966 964 Preliminary tests show the difference has little runtime effect. 967 Finally, the C @rand@ function is used generate random numbers.965 %Finally, the C @rand@ function is used generate random numbers. 968 966 969 967 \begin{figure} 970 968 \begin{lstlisting}[xleftmargin=3\parindentlnth,aboveskip=0pt,belowskip=0pt,numbers=left,numberstyle=\tt\small,numberblanklines=false] 971 969 int main( int argc, char *argv[] ) { 972 int max = 0;973 stack(int) s, t;974 REPEAT_TIMED( "push_int", push( &s, 42 ); )975 TIMED( "copy_int", t = s; )976 TIMED( "clear_int", clear( &s ); )977 REPEAT_TIMED( "pop_int", max = max( max, pop( &t ) ); )978 979 stack(pair(_Bool, char)) s1, t1;980 pair(_Bool, char) max = { (_Bool)0, '\0' };981 REPEAT_TIMED( "push_pair", push( &s1, (pair(_Bool, char)){ (_Bool)0, 'a' } ); )982 TIMED( "copy_pair", t1 = s1; )983 TIMED( "clear_pair", clear( &s1 ); )984 REPEAT_TIMED( "pop_pair", max = max( max, pop( &t1 ) ); )985 986 970 FILE * out = fopen( "cfa-out.txt", "w" ); 987 REPEAT_TIMED( "print_int", print( out, 42, ":", 42, "\n" ); ) 988 REPEAT_TIMED( "print_pair", print( out, (pair(_Bool, char)){ (_Bool)0, 'a' }, ":", 989 (pair(_Bool, char)){ (_Bool)0, 'a' }, "\n" ); ) 971 int max = 0, vali = 42; 972 stack(int) si, ti; 973 974 REPEAT_TIMED( "push_int", push( &si, vali ); ) 975 TIMED( "copy_int", ti = si; ) 976 TIMED( "clear_int", clear( &si ); ) 977 REPEAT_TIMED( "pop_int", max = max( max, pop( &ti ) ); ) 978 REPEAT_TIMED( "print_int", print( out, vali, ":", vali, "\n" ); ) 979 980 pair(_Bool, char) maxp = { (_Bool)0, '\0' }, valp = { (_Bool)0, 'a' }; 981 stack(pair(_Bool, char)) sp, tp; 982 983 REPEAT_TIMED( "push_pair", push( &sp, valp ); ) 984 TIMED( "copy_pair", tp = sp; ) 985 TIMED( "clear_pair", clear( &sp ); ) 986 REPEAT_TIMED( "pop_pair", maxp = max( maxp, pop( &tp ) ); ) 987 REPEAT_TIMED( "print_pair", print( out, valp, ":", valp, "\n" ); ) 990 988 fclose(out); 991 989 } 992 990 \end{lstlisting} 993 \caption{ Micro-Benchmark}991 \caption{\CFA Micro-Benchmark} 994 992 \label{fig:MicroBenchmark} 995 993 \end{figure} … … 1007 1005 \newcommand{\CT}[1]{\multicolumn{1}{c}{#1}} 1008 1006 \begin{tabular}{r|rrrr} 1009 & \CT{C} & \CT{\CFA} & \CT{\CC} & \CT{\CCV}\\ \hline1010 maximum memory usage (MB) & 10001 & 2501 & 2503 & 11253\\1011 source code size (lines) & 301 & 224 & 188 &437 \\1012 redundant type annotations (lines) & 46 & 3 & 2 & 1013 binary size (KB) & 18 & 234 & 18 &42 \\1007 & \CT{C} & \CT{\CFA} & \CT{\CC} & \CT{\CCV} \\ \hline 1008 maximum memory usage (MB) & 10001 & 2501 & 2503 & 11253 \\ 1009 source code size (lines) & 301 & 224 & 188 & 437 \\ 1010 redundant type annotations (lines) & 46 & 3 & 2 & 15 \\ 1011 binary size (KB) & 18 & 234 & 18 & 42 \\ 1014 1012 \end{tabular} 1015 1013 \end{table} … … 1100 1098 \section{Conclusion \& Future Work} 1101 1099 1100 The \CFA goal is to provide an evolutionary pathway for large C development-environments to be more productive and safer, while respecting the talent and skill of C programmers. 1101 While other programming languages purport to be a better C, they are in fact new and interesting languages in their own right, but not C extensions. 1102 The purpose of this paper is to introduce \CFA, and showcase two language features that illustrate the \CFA type-system and approaches taken to achieve the evolutionary goal. 1103 The contributions are a powerful type-system using parametric polymorphism and overloading, generic types, and tuples, which all have complex interactions. 1104 The work is a challenging design, engineering, and implementation exercise. 1105 On the surface, the project may appear as a rehash of similar mechanisms in \CC. 1106 However, every \CFA feature is different than its \CC counterpart, often with extended functionality, better integration with C and its programmers, and always supporting separate compilation. 1107 All of these new features are being used by the \CFA development-team to build the \CFA runtime system. 1108 Finally, we demonstrate that \CFA performance for some idiomtic cases is better than C and close to \CC, showing the design is competitive. 1109 1102 1110 There is ongoing work on a wide range of \CFA feature extensions, including reference types, exceptions, and concurrent programming primitives. 1103 1111 In addition to this work, there are some interesting future directions the polymorphism design could take. … … 1108 1116 These approaches are not mutually exclusive, and would allow these performance optimizations to be applied only where most useful to increase performance, without suffering the code bloat or loss of generality of a template expansion approach where it is unnecessary. 1109 1117 1110 In conclusion, the authors' design for generic types and tuples, unlike those available in existing work, is both reusable and type-checked, while still supporting a full range of C features, including separately-compiled modules.1111 We have experimentally validated the performance of our design against both \CC and standard C, showing it is comparable to both, and in some cases better.1112 1113 1118 1114 1119 \begin{acks}
Note: See TracChangeset
for help on using the changeset viewer.