Ignore:
Timestamp:
Apr 27, 2026, 7:17:34 PM (33 hours ago)
Author:
Michael Brooks <mlbrooks@…>
Branches:
master
Children:
f1ffc47
Parents:
602cec4
Message:

Apply document-level formatting and rearrange the array-in-struct discussion.

Formatting:

  • add visual separation of figures from text
  • prevent "formula"-style code fragments from page breaking
  • (thereby prevent code formula broken at end of page from mistakenly restarting the reader in a code figure at the top of the next page)
  • give short+long figure captions
  • assure descriptive captions
  • remove Title-Like Captialization in Captions
  • turn a couple figures into tables where the figure visual separator looks bad

Array-in-struct

  • remove lingering use of "accordion"
  • call it Dynamic Array Member
  • oranize discussion as, first explain example, then argue feature is good
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/mike_brooks_MMath/array.tex

    r602cec4 r9c12dd0  
    3030
    3131A function can be polymorphic over @array@ arguments using the \CFA @forall@ declaration prefix.
     32\begin{minipage}{\linewidth} % forbid page splitting, force
    3233\begin{cfa}
    3334forall( T, @[N]@ )
     
    4041Cforall Runtime error: subscript 1000 exceeds dimension range [0,99) $for$ array 0x555555558020.
    4142\end{cfa}
     43\end{minipage}
    4244Function @g@ takes an arbitrary type parameter @T@ and an unsigned integer \emph{dimension} @N@.
    4345The dimension represents a to-be-determined number of elements, managed by the type system, where 0 represents an empty array.
     
    125127\lstinput{30-43}{hello-array.cfa}
    126128\lstinput{45-48}{hello-array.cfa}
    127 \caption{Example of calling a dimension-generic function.}
     129\caption{Example of calling a dimension-generic function}
    128130\label{f:fExample}
    129131\end{figure}
     
    243245This static check's rules are presented in \VRef[Section]{s:ArrayTypingC}.
    244246
    245 Orthogonally, the \CFA array type works within generic \emph{types}, \ie @forall@-on-@struct@.
    246 The same argument safety and the associated implicit communication of array length occurs.
     247Orthogonally, the \CFA array type also works inside generic \emph{types}.
     248That is, an @array@ as a generic @struct@'s member and an @[N]@ can parameterize a @struct@.
     249The same argument safety, and the associated implicit length communication, occur.
    247250Preexisting \CFA allowed aggregate types to be generalized with type parameters, enabling parameterizing of element types.
    248 This feature is extended to allow parameterizing by dimension.
    249 Doing so gives a refinement of C's ``flexible array member''~\cite[\S~6.7.2.1.18]{C11}:
     251As polymorphism on functions was extended to allow parameterizing over dimensions, so is polymorphism on types.
     252The result has profound consequences for safe memory management.
     253
     254C's Flexible Array Member (FAM) pattern~\cite[\S~6.7.2.1.18]{C11} gives an unsafe way to put an array of dynamic length inside a structure.
    250255\begin{cfa}
    251256struct S {
    252257        ...
    253         double d []; // incomplete array type => flexible array member
    254 } * s = malloc( sizeof( struct S ) + sizeof( double [10] ) );
    255 \end{cfa}
    256 which creates a VLA of size 10 @double@s at the end of the structure.
    257 A C flexible array member can only occur at the end of a structure;
    258 \CFA allows length-parameterized array members to be nested at arbitrary locations, with intervening member declarations.
    259 \lstinput{10-15}{hello-accordion.cfa}
    260 The structure has course- and student-level metadata (their respective field names) and a position-based preferences' matrix.
    261 The structure's layout is dynamic; the starting offset of @student_ids@ varies according to the generic parameter @C@; the offset of @preferences@ varies by both.
    262 
    263 \VRef[Figure]{f:checkExample} shows a program main using @School@ and results with different array sizes.
     258        double d[]; // incomplete array type => flexible array member
     259};
     260struct S * s = malloc( sizeof( struct S ) + sizeof( double [10] ) );
     261\end{cfa}
     262This type-@S@ declaration defines a quasi-VLA named @d@ as the last structure member.
     263The variable-@s@ initialization sizes this particular array at 10 elements.
     264The choice of @malloc@ here is the common-case FAM idiom, but @alloca@ can be substituted, resulting in stack allocation.
     265A \CFA @[N]@-parameterized structure improves on this pattern.
     266
     267\begin{figure}
     268        \lstinput{10-15}{hello-accordion.cfa}
     269        \caption{\CFA Dynamic Array Member (DAM) declaration}
     270        \label{DAM-declaration}
     271\end{figure}
     272
     273\CFA allows length-parameterized array members to be nested at arbitrary locations, with further member declarations interleaved.
     274The result is a \newterm{Dynamic Array Member (DAM)}
     275% \footnote{
     276%       To keep the names straight, it may help to think of C's flexible array member as ``too flexible,'' where the discussion that follows makes the case that a FAM is weakly typed.
     277% },
     278as illustrated in \VRef[Figure]{DAM-declaration}.  The example's @School@ structure has course- and student-level metadata (in the respectively named fields) and a position-based preferences' matrix.
     279The structure's layout is dynamic; the starting offset of @student_ids@ varies according to the generic parameter @C@, which affects the size of the @course_codes@ field.
     280Similarly, the offset of @preferences@ varies by both parameters.
     281
     282\VRef[Figure]{f:DAM-example} shows a program main using the @School@ type, along with results at different array sizes.
    264283The @school@ variable holds many students' course-preference forms.
    265284It is on the stack and its initialization does not use any casting or size arithmetic.
    266 Both of these points are impossible with a C flexible array member.
    267 When heap allocation is preferred, the original pattern still applies.
     285When heap allocation is preferred, the original pattern still applies, with the same qualities.
    268286\begin{cfa}
    269287School( classes, students ) * sp = alloc();
    270288\end{cfa}
    271 This ability to avoid casting and size arithmetic improves safety and usability over C flexible array members.
    272289The example program prints the courses in each student's preferred order, all using the looked-up display names.
    273 When a function operates on a @School@ structure, the type system handles its memory layout transparently.
    274 In the example, function @getPref@ returns, for the student at position @is@, what is the position of their @pref@\textsuperscript{th}-favoured class.
    275 Finally, inputs and outputs are given on the right for different sized schools.
     290In the example, function @getPref@ returns, for the student at position @is@, the position of their @pref@\textsuperscript{th}-favoured class.
     291This function helps the program to output each student's ordered preferences.
     292The program's inputs and outputs are given on the right, for two different school sizes.
    276293
    277294\begin{figure}
     
    304321\end{tabular}
    305322
    306 \caption{\lstinline{School} Example, Input and Output}
    307 \label{f:checkExample}
     323
     324\caption[\CFA Dynamic Array Member (DAM) usage]{
     325        \CFA Dynamic Array Member (DAM) usage.
     326        Example program (left) with input and output (right).}
     327\label{f:DAM-example}
    308328\end{figure}
     329
     330Much of these \CFA DAM features are impossible with a C FAM.
     331In C, they can only occur at the end of a structure;
     332\CFA allows them anywhere.
     333As a consequence, \CFA allows several of them, while C is limited to one.
     334The \CFA ability to avoid casting and size arithmetic improves safety and usability over C.
     335While C offers the \emph{VLA} language feature, to eliminate casting and size arithmetic for stack-allocated \emph{simple arrays}, this feature does not extend to stack-allocating a FAM-structure, which makes the user fall back on explicit @alloca@.
     336The \CFA DAM, by contrast, treats every stack allocation as a simple variable declaration.
     337When a function operates on a \CFA DAM structure, the type system handles its memory layout transparently.
     338
    309339
    310340
     
    343373        The ``resolver'' compiler pass, which provides argument values for a declaration's type-system parameters, gathered from type information in scope at the usage site.
    344374\item
    345         The ``box'' compiler pass, which encodes information about type parameters into ``extra'' regular parameters on declarations and and arguments on calls.
     375        The ``box'' compiler pass, which encodes information about type parameters into ``extra'' regular parameters on declarations, and corresponding arguments on calls.
    346376        Notably, it conveys the size of a type @foo@ as a @__sizeof_foo@ parameter, and rewrites the @sizeof(foo)@ expression as @__sizeof_foo@, \ie a use of the parameter.
    347377\end{itemize}
     
    690720        \end{itemize}
    691721
    692         \caption{Case comparison for array type compatibility, given pairs of dimension expressions.}
     722        \caption[Case comparison for array type compatibility]{Case comparison for array type compatibility, given pairs of dimension expressions.}
    693723        \label{f:DimexprRuleCompare}
    694724\end{figure}
     
    946976\begin{figure}
    947977\includegraphics{measuring-like-layout}
    948 \caption{Visualization of subscripting by value and by \lstinline[language=CFA]{all}, for \lstinline{x} of type \lstinline{array( float, 5, 7 )} understood as 5 rows by 7 columns.
    949 The horizontal layout represents contiguous memory addresses while the vertical layout is conceptual.
    950 The vertical shaded band highlights the location of the targeted element, 2.3.
    951 Any such vertical slice contains various interpretations of a single address.}
     978\caption[Visualization of subscripting by value and by `all']{
     979        Visualization of subscripting by value and by `\lstinline[language=CFA]{all}.'
     980        Uses example variable \lstinline{x} of type \lstinline{array( float, 5, 7 )}, understood as 5 rows by 7 columns.
     981        The horizontal layout represents contiguous memory addresses while the vertical layout is conceptual.
     982        The vertical shaded band highlights the location of the targeted element, 2.3.
     983        Any such vertical slice contains various interpretations of a single address.
     984}
    952985\label{fig:subscr-all}
    953986\end{figure}
     
    10071040% \noindent END: Paste looking for a home
    10081041
    1009 The new-array library defines types and operations that ensure proper elements are accessed soundly in spite of the overlapping.
    1010 The @arpk@ structure and its @-[i]@ operator are defined as:
     1042\begin{figure}
    10111043\begin{cfa}
    10121044forall(
     
    10261058}
    10271059\end{cfa}
     1060\caption{Definition of structure `\lstinline{arpk},' which underlies the \CFA array}
     1061\label{f:arpk}
     1062\end{figure}
     1063
     1064The new-array library defines types and operations that ensure proper elements are accessed soundly in spite of the overlapping.
     1065The @arpk@ structure and its @-[i]@ operator are defined in \VRef[Figure]{f:arpk}.
    10281066The private @arpk@ structure (array with explicit packing) is generic over four types: dimension length, masquerading-as, ...
    10291067This structure's public interface is hidden behind the @array(...)@ macro and the subscript operator.
     
    11011139\multicolumn{1}{c}{(i)}
    11021140\end{tabular}
    1103 \caption{Overhead comparison, control case.
    1104 All three programs/compilers generate equally performant code when the programmer ``self-checks'' bounds via a loop's condition.
    1105 Yet both \CFA and \CC versions generate code that is slower and safer than C's when the programmer might overrun the bounds.}
     1141\caption[Bound-check overhead comparison, control case]{
     1142        Bound-check overhead comparison, control case.
     1143        All three programs/compilers generate equally performant code when the programmer ``self-checks'' bounds via a loop's condition.
     1144        Yet both \CFA and \CC versions generate code that is slower and safer than C's when the programmer might overrun the bounds.
     1145}
    11061146\label{f:ovhd-ctl}
    11071147\end{figure}
     
    13441384\end{cfa}
    13451385\end{tabular}
    1346 \caption{Exponential thunk generation under the otype-recursion pattern.
     1386\caption[Exponential thunk generation under the otype-recursion pattern]{
     1387        Exponential thunk generation under the otype-recursion pattern.
    13471388        Each time one type's function (\eg ctor) uses another type's function, the \CFA compiler generates a thunk, to capture the used function's dependencies, presented according to the using function's need.
    13481389        So, each non-leaf line represents a generated thunk and every line represents a search request for the resolver to find a satisfying function.}
     
    16401681\hspace{6pt}
    16411682
    1642 \caption{Triangular Matrix}
     1683\caption{Triangular matrix in Java and C}
    16431684\label{f:JavaVsCTriangularMatrix}
    16441685\end{figure}
     
    16841725
    16851726Thus, these options do not offer an allocation with a dynamically given fixed size.
    1686 And furthermore, they do not provide any improvement to the C flexible array member pattern, for making a dynamic amount of storage contiguous with its header, as do \CFA's accordions.
     1727And furthermore, they do not provide any improvement to the C flexible array member pattern, for making a dynamic amount of storage contiguous with its header, as do \CFA's new dynamic array members.
    16871728
    16881729
Note: See TracChangeset for help on using the changeset viewer.