Ignore:
Timestamp:
Dec 16, 2023, 1:01:32 AM (7 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
5546eee4
Parents:
dc80280
Message:

Update enumeration data structure

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/enum.tex

    rdc80280 r0fa0201d  
    232232
    233233\subsection{Enumeration Data}
    234 It is a work in progress, outlined the idea but will update with more information later...
    235 A \CFA enumeration is backed by data structures to store necessary data. Specifically, an enumeration has arrays to store its labels and values.
    236 \begin{lstlisting}[label=lst:enumeration_back_data]
    237 enum(T) E;
     234\begin{lstlisting}[label=lst:enumeration_backing_data]
     235enum(T) E { ... };
    238236// backing data
    239237T* E_values;
    240238char** E_labels;
    241239\end{lstlisting}
    242 Depending on the use cases, the value and label array might not be necessary to the program.
     240Storing values and labels as arrays can sometimes help support enumeration features. However, the data structures are the overhead for the programs. We want to reduce the memory usage for enumeration support by:
     241\begin{itemize}
     242    \item Only generates the data array if necessary
     243    \item The compilation units share the data structures. No extra overhead if the data structures are requested multiple times.
     244\end{itemize}
     245
     246
     247\subsection{Aggressive Inline}
     248To avoid allocating memory for enumeration data structures, \CFA inline the result of enumeration attribute pseudo-function whenever it is possible.
     249\begin{lstlisting}[label=lst:enumeration_inline]
     250enum(int) OddNumber { A=1, B=3 };
     251sout | "A: " | OddNumber.A | "B: " | OddNumber.B | "A+B: " | OddNumber.A + OddNumber.B
     252\end{lstlisting}
     253Instead of calling pseudo-function @value@ on expression $OddNumber.A$ and $OddNumber.B$, because the result is known statistically, \CFA will inline the constant expression 1 and 3, respectively. Because no runtime lookup for enumeration value is necessary, \CFA will not generate data structure for enumeration OddNumber.
    243254
    244255\subsection{Weak Reference}
    245 If the value and label need to be determined at the runtime, the data arrays are necessary. However, allocating the attribute arrays in every compilation unit can take up a lot of memory and it is not ideal. \CFA Optimized it by using GCC weak reference ( @__attribute(weak)__@ ).
    246 
    247 \subsection{Aggressive Inline}
    248 If the label and value can be determined during the compilation, \CFA can inline the attributes and no allocation is needed.
     256\begin{lstlisting}[label=lst:week_ref]
     257enum(int) OddNumber { A=1, B=3 };
     258enum OddNumber i = ...;
     259...
     260sout | OddNumber;
     261\end{lstlisting}
     262In this example, \CFA cannot determine the static value of the enum variable i, and Runtime lookup is necessary. The OddNumber can be referenced in multiple compilations, and allocating the arrays in all compilation units is not desirable. \CFA addresses this by declaring the value array as a weak reference. All compilation units reference OddNumber have weak references to the same enumeration data structure. No extra memory is allocated if more compilation units reference OddNumber, and the OddNumber is initialized once.
    249263
    250264\section{Unification}
Note: See TracChangeset for help on using the changeset viewer.