Changeset c01a2fd
- Timestamp:
- Nov 10, 2024, 8:57:28 PM (45 hours ago)
- Branches:
- master
- Children:
- b766c9b7
- Parents:
- 464cfc7
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/mike_brooks_MMath/string.tex
r464cfc7 rc01a2fd 5 5 6 6 7 \section{String Comparison}7 \section{String Operations} 8 8 9 9 To prepare for the following discussion, a simple comparison among C, \CC, and \CFA basic string operation is presented. 10 10 \begin{cquote} 11 11 \begin{tabular}{@{}l|l|l@{}} 12 C @char [ ]@ & \CC @string@ & \CFA @string@ \\12 C @char [ ]@ & \CC @string@ & \CFA @string@ \\ 13 13 \hline 14 @strcpy@, @strncpy@ & @=@ & @=@ \\15 @strcat@, @strncat@ & @+@ & @+@ \\14 @strcpy@, @strncpy@ & @=@ & @=@ \\ 15 @strcat@, @strncat@ & @+@ & @+@ \\ 16 16 @strcmp@, @strncmp@ & @==@, @!=@, @<@, @<=@, @>@, @>=@ & @==@, @!=@, @<@, @<=@, @>@, @>=@ \\ 17 @strlen@ & @length@ & @size@ \\18 @[ ]@ & @[ ]@ & @[ ]@ \\19 & @substr@ & @substr@ \\20 & @replace@ 21 @strstr@ & @find@, @rfind@ 17 @strlen@ & @length@ & @size@ \\ 18 @[ ]@ & @[ ]@ & @[ ]@ \\ 19 & @substr@ & @substr@ \\ 20 & @replace@ & @=@ \emph{(on a substring)}\\ 21 @strstr@ & @find@, @rfind@ & @find@, MISSING \\ 22 22 @strcspn@ & @find_first_of@, @find_last_of@ & @include@, MISSING \\ 23 23 @strspn@ & @find_first_not_of@, @find_last_not_of@ & @exclude@, MISSING \\ 24 & @c_str@ 24 & @c_str@ & MISSING \\ 25 25 \end{tabular} 26 26 \end{cquote} 27 Some more discussion ... 28 29 30 \section{Logical overlap} 27 The key commonality is that operations work on groups of characters for assigning. copying, scanning, and updating, 28 Because a C string is null terminated and requires explicit storage management \see{\VRef{s:String}}, most of its group operations are error prone and expensive. 29 Most high-level string libraries use a separate length field and specialized storage management to support group operations. 30 \CC strings retains null termination to allow them to interface with C library functions requiring C strings. 31 \begin{cfa} 32 int open( const char * pathname, int flags ); 33 string fname{ "abc" ); 34 open( fname.@c_str()@ ); 35 \end{cfa} 36 The function @c_str@ does not create a new null-terminated C string of the \CC string, as that requires passing ownership of the C string to the caller for eventual deletion. 37 Instead, each \CC string is null terminator just in case it might be needed for this purpose. 38 Providing this backwards compatibility with C has a ubiquitous performance and storage cost even when this capability is not used. 39 40 41 \section{Storage Management} 42 43 This section discusses issues related to storage management of strings. 44 Specifically, it is common for strings to logically overlap completely or within a substring. 45 \begin{cfa} 46 string s1 = "abcdef"; 47 string s2 = s1; $\C{// complete overlap, s2 == "abcdef"}$ 48 string s3 = s1.substr( 0, 3 ); $\C{// substring overlap, s3 == "abc"}$ 49 \end{cfa} 50 This raises the question of how strings behave when an overlapping component is changed, 51 \begin{cfa} 52 s3[1] = 'w'; $\C{// what happens to s1 and s2?}$ 53 \end{cfa} 54 \ie the notion of mutable and immutable strings. 55 For example, Java has immutable strings so strings are copied when any overlapping string changes. 56 Note, the notion of string mutability is not specified by @const@, \eg: 57 \begin{cfa} 58 const string s1 = "abc"; 59 \end{cfa} 60 Here, @s1@ always points at @"abc"@, and @"abc"@ is an immutable constant, so all aspects of @s1@ are immutable. 61 Because a string is an implicit pointer, there is no mechanism to write: 62 \begin{cfa} 63 const string @* const@ s1; 64 \end{cfa} 65 Hence, the underlying string is mutable. 66 67 68 \subsection{Logical overlap} 69 70 Rather than have strings be either mutable or immutable, it is possible to mark them on assignment. 31 71 32 72 \input{sharing-demo.tex} … … 47 87 The purposes of such invariants go beyond ensuring authentic values for the bits inside the object. These invariants can track occurrences of the managed objects in other data structures. Reference counting is a typical application of the latter invariant type. With a reference-counting smart pointer, the constructor and destructor \emph{of the pointer type} track the life cycles of occurrences of these pointers, by incrementing and decrementing a counter (usually) on the referent object, that is, they maintain a that is state separate from the objects to whose life cycles they are attached. Both the \CC and \CFA RAII systems ares powerful enough to achieve such reference counting. 48 88 49 The \CC RAII system supports a more advanced application. A life cycle function has access to the object under management, by location; constructors and dest uctors receive a @this@ parameter providing its memory address. A lifecycle-function implementation can then add its objects to a collection upon creation, and remove them at destruction. A modulue that provides such objects, by using and encapsulating such a collection, can traverse the collection at relevant times, to keep the objects ``good.'' Then, if you are the user of such an module, declaring an object of its type means not only receiving an authentically ``good'' value at initialization, but receiving a subscription to a service that will keep the value ``good'' until you are done with it.50 51 In many cases, the relationship between memory location and lifecycle is simple. But with stack-allocated objects being used as parameters and returns, there is a sender version in one stack frame and a receiver version in another. \CC is able to treat those versions as distinct objects and guarantee a copy-constructor call for communicating the value from one to the other. This ability has implications on the language's calling convention. Consider an ordinary function @void f( Vehicle x )@, which receives an aggregate by value. If the type @Vehicle@ has custom lifecycle functions, then a call to a user-provided copy constructor occurs, after the caller evaluates its argument expression, after the callee's stack frame exists, with room for its variable @x@ (which is the location that the copy-constructor must target), but before the user-provided body of @f@ begins executing. \CC achieves this ordering by changing the function signature, in the compiled form, to pass-by-reference and having the callee invoke the copy constructor in its preamble. On the other hand, if @Vehicle@ is a simple structure then the C calling convention is applied as the code originally appeared, that is, the call site implementation code performs a bitwise copy from the caller's expression result, into the callee's x.52 53 TODO: learn correction to fix incon csistency: this discussion says the callee invokes the copy constructor, but only the caller knows which copy constructor to use!89 The \CC RAII system supports a more advanced application. A life cycle function has access to the object under management, by location; constructors and destructors receive a @this@ parameter providing its memory address. A lifecycle-function implementation can then add its objects to a collection upon creation, and remove them at destruction. A module that provides such objects, by using and encapsulating such a collection, can traverse the collection at relevant times, to keep the objects ``good.'' Then, if you are the user of such an module, declaring an object of its type means not only receiving an authentically ``good'' value at initialization, but receiving a subscription to a service that will keep the value ``good'' until you are done with it. 90 91 In many cases, the relationship between memory location and lifecycle is simple. But with stack-allocated objects being used as parameters and returns, there is a sender version in one stack frame and a receiver version in another. \CC is able to treat those versions as distinct objects and guarantee a copy-constructor call for communicating the value from one to the other. This ability has implications on the language's calling convention. Consider an ordinary function @void f( Vehicle x )@, which receives an aggregate by value. If the type @Vehicle@ has custom lifecycle functions, then a call to a user-provided copy constructor occurs, after the caller evaluates its argument expression, after the callee's stack frame exists, with room for its variable @x@ (which is the location that the copy-constructor must target), but before the user-provided body of @f@ begins executing. \CC achieves this ordering by changing the function signature, in the compiled form, to pass-by-reference and having the callee invoke the copy constructor in its preamble. On the other hand, if @Vehicle@ is a simple structure then the C calling convention is applied as the code originally appeared, that is, the call-site implementation code performs a bitwise copy from the caller's expression result, into the callee's x. 92 93 TODO: learn correction to fix inconsistency: this discussion says the callee invokes the copy constructor, but only the caller knows which copy constructor to use! 54 94 55 95 TODO: discuss the return-value piece of this pattern 56 96 57 The \CFA RAII system has limited support for using lifecycle functions to provide a ``stay good'' service. It works in restricted settings, including on dynamically allocated objects. It does not work for communicating arguments and returns by value because the system does not produce a constructor call that tracks the implied move from a sender frame to a rec iver frame. This limitation does not prevent a typical reference-counting design from using call-with-value/return-of-value, because the constructor--destructor calls are correctly balanced. But it impedes a ``stay-good'' service from supporting call-with-value/return-of-value, because the lifecycles presented to the constructor/destor calls do not keep stable locations. A ``stay-good'' service is acheivable so long as call-with-value/return-of-value do not occur. The original presentation [to cite Schluntz section] acknoweledges this limitiation; the present discussion makes its consequences more apparent.58 59 The \CFA team sees this limitation as part of a tactical inter em state that should some day be improved. The \CFA compiler is currently a source-to-source translator that targets relativly portable C. Several details of its features are provisionally awkward or under-performant until finer control of its code generation is feasible. In the present state, all calls that appear in \CFA source code as call-with-value/return-of-value are emitted this way to the underlying C calling convention. SO WHAT?60 61 The present string-API contribution has both the ``stay good'' promise and call-with-value/return-of-value being essential. The main string API uses a work-around to ach eive the full feature set, at a runtime performance penalty. An alternative API sacrifices call-with-value/return-of-value functionality to recover full runtime performance. These APIs are layered, with the slower, friendlier High Level API (HL) wrapping the faster, more primitive Low Level API (LL). They present the same features, up to lifecycle management, with call-with-value/return-of-value being disabled in LL and implemented with the workaround in HL. The intention is for most future code to target HL. In a more distant future state, where \CFA has an RAII system that can handle the problematic quadrant, the HL layer can be abolished, the LL can be renamed to match today's HL, and LL can have its call-with-value/return-of-value permission reenabled. Then, programs written originally against HL will simply run faster. In the meantime, two use cases of LL exist. Performance-critical sections of applications have LL as an option. Within [Xref perf experiments], though HL-v-LL penalties are measured, typcial comparisons of the contributed string libary vs similar systems are made using LL. This measurement gives a fair estimate of the goal state for \CFA while it is an evloving work in progress.97 The \CFA RAII system has limited support for using lifecycle functions to provide a ``stay good'' service. It works in restricted settings, including on dynamically allocated objects. It does not work for communicating arguments and returns by value because the system does not produce a constructor call that tracks the implied move from a sender frame to a receiver frame. This limitation does not prevent a typical reference-counting design from using call-with-value/return-of-value, because the constructor--destructor calls are correctly balanced. But it impedes a ``stay-good'' service from supporting call-with-value/return-of-value, because the lifecycles presented to the constructor/destructor calls do not keep stable locations. A ``stay-good'' service is achievable so long as call-with-value/return-of-value do not occur. The original presentation [to cite Schluntz section] acknowledges this limitation; the present discussion makes its consequences more apparent. 98 99 The \CFA team sees this limitation as part of a tactical interim state that should some day be improved. The \CFA compiler is currently a source-to-source translator that targets relatively portable C. Several details of its features are provisionally awkward or under-performant until finer control of its code generation is feasible. In the present state, all calls that appear in \CFA source code as call-with-value/return-of-value are emitted this way to the underlying C calling convention. SO WHAT? 100 101 The present string-API contribution has both the ``stay good'' promise and call-with-value/return-of-value being essential. The main string API uses a work-around to achieve the full feature set, at a runtime performance penalty. An alternative API sacrifices call-with-value/return-of-value functionality to recover full runtime performance. These APIs are layered, with the slower, friendlier High Level API (HL) wrapping the faster, more primitive Low Level API (LL). They present the same features, up to lifecycle management, with call-with-value/return-of-value being disabled in LL and implemented with the workaround in HL. The intention is for most future code to target HL. In a more distant future state, where \CFA has an RAII system that can handle the problematic quadrant, the HL layer can be abolished, the LL can be renamed to match today's HL, and LL can have its call-with-value/return-of-value permission reenabled. Then, programs written originally against HL will simply run faster. In the meantime, two use cases of LL exist. Performance-critical sections of applications have LL as an option. Within [Xref perf experiments], though HL-v-LL penalties are measured, typical comparisons of the contributed string library vs similar systems are made using LL. This measurement gives a fair estimate of the goal state for \CFA while it is an evolving work in progress. 62 102 63 103 … … 65 105 \subsection{Memory management} 66 106 67 A centrep riece of the string module is its memory manager. The managment scheme defines a large shared buffer for strings' text. Allocation in this buffer is always bump-pointer; the buffer is compacted and/or relocated with growth when it fills. A string is a smart pointer into this buffer.107 A centrepiece of the string module is its memory manager. The management scheme defines a large shared buffer for strings' text. Allocation in this buffer is always bump-pointer; the buffer is compacted and/or relocated with growth when it fills. A string is a smart pointer into this buffer. 68 108 69 109 This cycle of frequent cheap allocations, interspersed with infrequent expensive compactions, has obvious similarities to a general-purpose memory manager based on garbage collection (GC). A few differences are noteworthy. First, in a general purpose manager, the objects of allocation contain pointers to other such objects, making the transitive reachability of these objects be a critical property. Here, the allocations are of buffers of text, never pointers, so one allocation never keeps another one alive. Second, in a general purpose manager, where the handle that keeps an allocation alive is the same as the program's general-purpose inter-object reference, an extremely lean representation of this reference is required. Here, a fatter representation is acceptable because [why??]. 70 110 71 111 72 Figure [memmgr-basix.vsdx] shows the representation. A heap header, with its text buffer, defines a sharing context. Often, one global sharing context is appropriate for an entire program; exceptions are discussed in [xref TBD]. Strings are handles into the buffer. They are members of a linked list whose order matches the order of their buffer fragments (exactly, where there is no overlapping, and approximately, where there is). The header maintains a next-allocation pointer (alloc, in the figure) after the last live allocation of the buffer. No external references into the buffer are allowed and the management procedure relocates the text allocations as needed. The string handles contain explicit length fields, null termination characters are not used and all string text is kept in contiguous storage. When strings (the inter-linked han ldes) are allocated on the program's call stack, a sustained period with no use of the program's dynamic memory allocator can ensue, during which the program nonetheless creates strings, destroys them, and runs length-increasing modifications on existing ones.73 74 Compaction happens when the heap fills. It is the first of two uses of the linked list. The list allows discovering all live string handles, and from them, the ranges of the character buffer that are in use. With these ranges known, their total character count gives the amount of space in use. When that amount is small, compared with the current buffer size, an in-place compaction occurs, which en atils copying the in-use ranges, to be adjacent, at the font of the buffer. When the in-use amount is large, a larger buffer is allocated (using the program's general-purpose dynamic allcator), the in-use strings are copied to be adjacent at the front of it, and the original buffer is freed back to the program's general allocator. Either way, navigating the links between the handles provides the pointers into the buffer, first read, to find the source fragment, then written with the location of the resultant fragment. This linkage across the structure is unaffected during a compaction; only the pointers from the handles to the buffer are modified. This modification, along with the grooming/provisioning of the text storage resouce that it represents, is an example, in the language of [xref RAII limitations] of the string module providing a ``stay good'' service.75 76 Object lifecycle events are the subscription-management triggers in such a service. There are two fundamental string-creation routines: importing from external text like a C-string, and initialization from an existing \CFA string. When importing, a fresh allocation at the free end fo the buffer occurs, into which the text is copied. The resultant handle is therefore inserted into the list at the position after the incumbent last handle, a position given by the heap manager's ``last handle'' pointer. When initializing from text already on the \CFA heap, the resultant handle is a second reference onto the original run of characters. In this case, the resultant handle's linked-list position is beside the original handle. Both string initialization styles preserve the string module's internal invriant that the linked-list order match the buffer order. For string destruction, the list being doubly linked provides for easy removal of the disappearing handle.112 Figure [memmgr-basix.vsdx] shows the representation. A heap header, with its text buffer, defines a sharing context. Often, one global sharing context is appropriate for an entire program; exceptions are discussed in [xref TBD]. Strings are handles into the buffer. They are members of a linked list whose order matches the order of their buffer fragments (exactly, where there is no overlapping, and approximately, where there is). The header maintains a next-allocation pointer (alloc, in the figure) after the last live allocation of the buffer. No external references into the buffer are allowed and the management procedure relocates the text allocations as needed. The string handles contain explicit length fields, null termination characters are not used and all string text is kept in contiguous storage. When strings (the inter-linked handles) are allocated on the program's call stack, a sustained period with no use of the program's dynamic memory allocator can ensue, during which the program nonetheless creates strings, destroys them, and runs length-increasing modifications on existing ones. 113 114 Compaction happens when the heap fills. It is the first of two uses of the linked list. The list allows discovering all live string handles, and from them, the ranges of the character buffer that are in use. With these ranges known, their total character count gives the amount of space in use. When that amount is small, compared with the current buffer size, an in-place compaction occurs, which entails copying the in-use ranges, to be adjacent, at the font of the buffer. When the in-use amount is large, a larger buffer is allocated (using the program's general-purpose dynamic allocator), the in-use strings are copied to be adjacent at the front of it, and the original buffer is freed back to the program's general allocator. Either way, navigating the links between the handles provides the pointers into the buffer, first read, to find the source fragment, then written with the location of the resultant fragment. This linkage across the structure is unaffected during a compaction; only the pointers from the handles to the buffer are modified. This modification, along with the grooming/provisioning of the text storage resource that it represents, is an example, in the language of [xref RAII limitations] of the string module providing a ``stay good'' service. 115 116 Object lifecycle events are the subscription-management triggers in such a service. There are two fundamental string-creation routines: importing from external text like a C-string, and initialization from an existing \CFA string. When importing, a fresh allocation at the free end of the buffer occurs, into which the text is copied. The resultant handle is therefore inserted into the list at the position after the incumbent last handle, a position given by the heap manager's ``last handle'' pointer. When initializing from text already on the \CFA heap, the resultant handle is a second reference onto the original run of characters. In this case, the resultant handle's linked-list position is beside the original handle. Both string initialization styles preserve the string module's internal invariant that the linked-list order match the buffer order. For string destruction, the list being doubly linked provides for easy removal of the disappearing handle. 77 117 78 118 While a string handle is live, it accepts modification operations, some of which make it reference a different portion of the underlying buffer, and accordingly, move the handle to a different position in the inter-handle list. While special cases have more optimal handling, the general case requires a fresh buffer run. In this case, the new run is allocated at the bump-pointer end and filled with the required value. Then, handles that originally referenced the old location and need to see the new value are pointed at the new buffer location, unlinked from their original positions in the handles' list, and linked in at the end of the list. An optimal case, when the target is not a substring of something larger, and the source is text from elsewhere in the managed buffer, allows the target to be re-pointed at the source characters, and accordingly, move list position to be beside the source. Cases where in-place editing happens, addressed further in [xref: TBD], leave affected handles in their original list positions. In analogy to the two cases of string initialization, the two cases of realizing assignment by moving either to a fresh buffer run, or to overlap references with the source, maintain the invariant of linked list order matching buffer order. … … 83 123 84 124 85 At the level of the memory manager, these modifications can a ways be explained as assignments; for example, an append is an assignemnt into the empty substring at the end.125 At the level of the memory manager, these modifications can always be explained as assignments; for example, an append is an assignment into the empty substring at the end. 86 126 87 127 While favourable conditions allow for in-place editing, the general case requires a fresh buffer run. For example, if the new value does not fit in the old place, or if other handles are still using the old value, then the new value will use a fresh buffer run. … … 96 136 \subsection{Sharing implementation} 97 137 98 The \CFA string module has two manners in which se rveral string handles can share an unerlying run of characters.99 100 The first type of sharing is user-requested, following the [xref Logical Overlap]. Here, the user requests, explicitly, that both handles be views of the same logical, modifiable string. This state is typically prod ecd by the substring operation. In a typical substring call, the source string-handle is referencing an entire string, and the resluting, newly made, string handle is referencing a portion of the orignal. In this state, a subsequent modification made by either is visible in both.101 102 The second type of sharing happens when the system implicitly delays the physical execution of a logical \emph{copy} operation, as part of its copy-on-write optimization. This state is typically produced by constructing a new string, using an original string as its in tialization source. In this state, a subsequent modification done on one handle triggers the deferred copy action, leaving the handles referencing different runs within the buffer, holding distinct values.138 The \CFA string module has two manners in which several string handles can share an underlying run of characters. 139 140 The first type of sharing is user-requested, following the [xref Logical Overlap]. Here, the user requests, explicitly, that both handles be views of the same logical, modifiable string. This state is typically produced by the substring operation. In a typical substring call, the source string-handle is referencing an entire string, and the resulting, newly made, string handle is referencing a portion of the original. In this state, a subsequent modification made by either is visible in both. 141 142 The second type of sharing happens when the system implicitly delays the physical execution of a logical \emph{copy} operation, as part of its copy-on-write optimization. This state is typically produced by constructing a new string, using an original string as its initialization source. In this state, a subsequent modification done on one handle triggers the deferred copy action, leaving the handles referencing different runs within the buffer, holding distinct values. 103 143 104 144 A further abstraction, in the string module's implementation, helps distinguish the two senses of sharing. A share-edit set (SES) is an equivalence class over string handles, being the reflexive, symmetric and transitive closure of the relationship of one being constructed from the other, with the ``share edits'' opt-in given. It is represented by a second linked list among the handles. A string that shares edits with no other is in a SES by itself. Inside a SES, a logical modification of one substring portion may change the logical value in another, depending on whether the two actually overlap. Conversely, no logical value change can flow outside of a SES. Even if a modification on one string handle does not reveal itself \emph{logically} to anther handle in the same SES (because they don't overlap), if the modification is length-changing, completing the modification requires visiting the second handle to adjust its location in the sliding text. … … 107 147 \subsection{Avoiding implicit sharing} 108 148 109 There are tradeoffs associated with the copy-on-write mechanism. Several qua titative matters are detailed in the [xref: Performance Assessment] section and the qualitiative issue of multi-threaded support is introduced here. The \CFA sting library provides a switch to disable the sharing mechanism for situtations where it is inappropriate.149 There are tradeoffs associated with the copy-on-write mechanism. Several qualitative matters are detailed in the [xref: Performance Assessment] section and the qualitative issue of multi-threaded support is introduced here. The \CFA sting library provides a switch to disable the sharing mechanism for situations where it is inappropriate. 110 150 111 151 Because of the inter-linked string handles, any participant managing one string is also managing, directly, the neighbouring strings, and from there, a data structure of the ``set of all strings.'' This data structure is intended for sequential access. A negative consequence of this decision is that multiple threads using strings need to be set up so that they avoid attempting to modify (concurrently) an instance of this structure. A positive consequence is that a single-threaded program, or a program with several independent threads, can use the sharing context without an overhead from locking. 112 152 113 The \CFA sting library provides the @string_sharectx@ type to control an ambient sharing context for the current thread. It allows two adjustments: to opt out of sharing entirely, or to begin sharing within a private context. Either way, the chosen mode applies to the current thread, for the duration of the lifetime of the created @string_sharectx@ object, up to being suspended by child lif times of different contexts. The indended use is with stack-managed lifetimes, in which the established context lasts until the current function returns, and affects all functions called that don't create their own contexts.153 The \CFA sting library provides the @string_sharectx@ type to control an ambient sharing context for the current thread. It allows two adjustments: to opt out of sharing entirely, or to begin sharing within a private context. Either way, the chosen mode applies to the current thread, for the duration of the lifetime of the created @string_sharectx@ object, up to being suspended by child lifetimes of different contexts. The indented use is with stack-managed lifetimes, in which the established context lasts until the current function returns, and affects all functions called that don't create their own contexts. 114 154 \lstinputlisting[language=CFA, firstline=20, lastline=34]{sharectx-demo.cfa} 115 155 In this example, the single-letter functions are called in alphabetic order. The functions @a@ and @d@ share string character ranges within themselves, but not with each other. The functions @b@, @c@ and @e@ never share anything. … … 117 157 [ TODO: true up with ``is thread local'' (implement that and expand this discussion to give a concurrent example, or adjust this wording) ] 118 158 119 When the string library is running with sharing disabled, it runs without implicit thread-safety challenges (which same as the STL) and with performance goals similar to the STL's. This thread-safety quality means concurrent users of one string object must still bring their own mutual ex lusion, but the string libary will not add any cross thread uses that were not apparent in the user's code.159 When the string library is running with sharing disabled, it runs without implicit thread-safety challenges (which same as the STL) and with performance goals similar to the STL's. This thread-safety quality means concurrent users of one string object must still bring their own mutual exclusion, but the string library will not add any cross thread uses that were not apparent in the user's code. 120 160 121 161 Running with sharing disabled can be thought of as STL-emulation mode. … … 132 172 \subsection{Performance assessment} 133 173 134 I assessed the CFA string library's speed and memory usage. I present these results in evenquivalent cases, due to either micro-optimizations foregone, or fundamental costs of the added functionality. They also show the benefits and tradeoffs, as >100\% effects, of switching to CFA, with the tradeoff points quantified. The final test shows the overall win of the CFA text-sharing mechanism. It exercises several operations together, showing CFA enabling clean user code to achieve performance that STL requires less-clean user code to achieve.135 136 To discuss: general goal of ... while STL makes you think about memory management, all the time, and if you do your performance can be great ... CFA sacrifices this advantage modestly in exchange for big wins when you're not thinking about memory ma mangement. [Does this position cover all of it?]174 I assessed the CFA string library's speed and memory usage. I present these results in even equivalent cases, due to either micro-optimizations foregone, or fundamental costs of the added functionality. They also show the benefits and tradeoffs, as >100\% effects, of switching to CFA, with the tradeoff points quantified. The final test shows the overall win of the CFA text-sharing mechanism. It exercises several operations together, showing CFA enabling clean user code to achieve performance that STL requires less-clean user code to achieve. 175 176 To discuss: general goal of ... while STL makes you think about memory management, all the time, and if you do your performance can be great ... CFA sacrifices this advantage modestly in exchange for big wins when you're not thinking about memory management. [Does this position cover all of it?] 137 177 138 178 To discuss: revisit HL v LL APIs 139 179 140 To discuss: revisit no sharing as STL emulation modes180 To discuss: revisit no-sharing as STL emulation modes 141 181 142 182 These tests use randomly generated text fragments of varying lengths. A collection of such fragments is a \emph{corpus}. The mean length of a fragment from corpus is a typical explanatory variable. Such a length is used in one of three modes: … … 144 184 \item [Fixed-size] means all string fragments are of the stated size 145 185 \item [Varying from 1] means string lengths are drawn from a geometric distribution with the stated mean, and all lengths occur 146 \item [Varying from 16] means string lengths are drawn from a geometric distribution with the stated mean, but only lengths 16 and obove occur; thus, the stated mean will be above 16.186 \item [Varying from 16] means string lengths are drawn from a geometric distribution with the stated mean, but only lengths 16 and above occur; thus, the stated mean will be above 16. 147 187 \end{description} 148 The geometric distribution implies that lengths much longer than the mean occur frequently. The special treatment of length 16 deals with comparison to STL, given that STL has short-string optimization (see [ todo: write and cross-ref future-work SSO]), currently not implmented in \CFA. When success notwithstanding SSO is illustrated, a fixed-size or from-16 distribution ensures that extra-optimized cases are not part of the mix on the STL side. In all experiments that use a corpus, its text is generated and loaded into the SUT before the timed phase begins.188 The geometric distribution implies that lengths much longer than the mean occur frequently. The special treatment of length 16 deals with comparison to STL, given that STL has short-string optimization (see [TODO: write and cross-ref future-work SSO]), currently not implemented in \CFA. When success notwithstanding SSO is illustrated, a fixed-size or from-16 distribution ensures that extra-optimized cases are not part of the mix on the STL side. In all experiments that use a corpus, its text is generated and loaded into the SUT before the timed phase begins. 149 189 150 190 To discuss: vocabulary for reused case variables … … 161 201 This test measures the speed of appending fragments of text onto a growing string. Its subcases include both CFA being similar to STL, and their designs offering a tradeoff. 162 202 163 One experimental variable is the user's operation being @a = a + b@ vs. @a += b@. While experienced programmers expect the latter to be ``what you obviously should do,'' control ing the penatly of the former both helps the API be accessible to beginners and also helps offer confidence that when a user tries to compose operations, the forms that are most natural to the user's composition are viable.164 165 Another experimental variable is whether the user's logical allocation is fresh vs reused. Here, \emph{reusing a logical allocation}, means that the pr gram variable, into which the user is concatenating, previously held a long string:\\203 One experimental variable is the user's operation being @a = a + b@ vs. @a += b@. While experienced programmers expect the latter to be ``what you obviously should do,'' controlling the penalty of the former both helps the API be accessible to beginners and also helps offer confidence that when a user tries to compose operations, the forms that are most natural to the user's composition are viable. 204 205 Another experimental variable is whether the user's logical allocation is fresh vs reused. Here, \emph{reusing a logical allocation}, means that the program variable, into which the user is concatenating, previously held a long string:\\ 166 206 \begin{tabular}{ll} 167 207 Logical allocation fresh & Logical allocation reused \\ … … 173 213 @ } @ & @ } @ 174 214 \end{tabular}\\ 175 These benchmark drivers have an outer loop for ``until a sample-worthy amount of execution has happened'' and an inner loop for ``build up the desired-length string.'' It is sensible to doubt that a user should have to care about this difference, yet the STL performs differently in these cases. Concret ly, both cases incur the cost of copying characters into the target string, but only the allocation-fresh case incurs a further reallocation cost, which is generally paid at points of doubling the length. For the STL, this cost includes obtaining a fresh buffer from the memory allocator and copying older characters into the new buffer, while CFA-sharing hides such a cost entirely. The reuse-vs-fresh distinction is only relevant in the currrent \emph{append} tests.176 177 The \emph{append} tests use the varying-from-1 corpus construction; that is they do not assume away the STL's advantage from small-string op itimization.178 179 To discuss: any other case variables intr uduced in the performance intro215 These benchmark drivers have an outer loop for ``until a sample-worthy amount of execution has happened'' and an inner loop for ``build up the desired-length string.'' It is sensible to doubt that a user should have to care about this difference, yet the STL performs differently in these cases. Concretely, both cases incur the cost of copying characters into the target string, but only the allocation-fresh case incurs a further reallocation cost, which is generally paid at points of doubling the length. For the STL, this cost includes obtaining a fresh buffer from the memory allocator and copying older characters into the new buffer, while CFA-sharing hides such a cost entirely. The reuse-vs-fresh distinction is only relevant in the current \emph{append} tests. 216 217 The \emph{append} tests use the varying-from-1 corpus construction; that is they do not assume away the STL's advantage from small-string optimization. 218 219 To discuss: any other case variables introduced in the performance intro 180 220 181 221 \begin{figure} … … 185 225 \end{figure} 186 226 187 Figure \ref{fig:string-graph-peq-cppemu} shows this behaviour, by the STL and by \CFA in STL emulation mode. \CFA reproduces STL's performance, up to a 15\% penalty averaged over the cases shown, diminishing with larger strings, and 50\% in the worst case. This pena tly characterizes the amount of implementation fine tuning done with STL and not done with \CFA in present state. The larger inherent penalty, for a user mismanaging reuse, is 40\% averaged over the cases shown, is minimally 24\%, shows up consistently between the STL and \CFA implementations, and increases with larger strings.227 Figure \ref{fig:string-graph-peq-cppemu} shows this behaviour, by the STL and by \CFA in STL emulation mode. \CFA reproduces STL's performance, up to a 15\% penalty averaged over the cases shown, diminishing with larger strings, and 50\% in the worst case. This penalty characterizes the amount of implementation fine tuning done with STL and not done with \CFA in present state. The larger inherent penalty, for a user mismanaging reuse, is 40\% averaged over the cases shown, is minimally 24\%, shows up consistently between the STL and \CFA implementations, and increases with larger strings. 188 228 189 229 \begin{figure} … … 207 247 To have introduced: STL string library forces users to think about memory management when communicating values across a function call 208 248 209 STL charges a prohibitive penalty for passing a string by value. With implicit sharing active, \CFA treats this operation as normal and supported. This test illustrates a main ad jantage of the \CFA sharing algorithm. It also has a case in which STL's small-string optimization provides a successful mitigation.249 STL charges a prohibitive penalty for passing a string by value. With implicit sharing active, \CFA treats this operation as normal and supported. This test illustrates a main advantage of the \CFA sharing algorithm. It also has a case in which STL's small-string optimization provides a successful mitigation. 210 250 211 251 \begin{figure} … … 224 264 This test directly compares the allocation schemes of the \CFA string with sharing, compared with the STL string. It treats the \CFA scheme as a form of garbage collection, and the STL scheme as an application of malloc-free. The test shows that \CFA enables faster speed at a cost in memory usage. 225 265 226 A garbage collector, afforded the freedom of managed memory, often runs faster than malloc-free (in an am mortized analysis, even though it must occasionally stop to collect) because it is able to use its collection time to move objects. (In the case of the mini-allocator powering the \CFA string library, objects are runs of text.) Moving objects lets fresh allocations consume from a large contiguous store of available memory; the ``bump pointer'' book-keeping for such a scheme is very light. A malloc-free implementation without the freedom to move objects must, in the general case, allocate in the spaces between existing objects; doing so entails the heavier book-keeping to navigate and maintain a linked structure.266 A garbage collector, afforded the freedom of managed memory, often runs faster than malloc-free (in an amortized analysis, even though it must occasionally stop to collect) because it is able to use its collection time to move objects. (In the case of the mini-allocator powering the \CFA string library, objects are runs of text.) Moving objects lets fresh allocations consume from a large contiguous store of available memory; the ``bump pointer'' book-keeping for such a scheme is very light. A malloc-free implementation without the freedom to move objects must, in the general case, allocate in the spaces between existing objects; doing so entails the heavier book-keeping to navigate and maintain a linked structure. 227 267 228 268 A garbage collector keeps allocations around for longer than the using program can reach them. By contrast, a program using malloc-free (correctly) releases allocations exactly when they are no longer reachable. Therefore, the same harness will use more memory while running under garbage collection. A garbage collector can minimize the memory overhead by searching for these dead allocations aggressively, that is, by collecting more often. Tuned in this way, it spends a lot of time collecting, easily so much as to overwhelm its speed advantage from bump-pointer allocation. If it is tuned to collect rarely, then it leaves a lot of garbage allocated (waiting to be collected) but gains the advantage of little time spent doing collection. … … 238 278 \begin{figure} 239 279 \includegraphics[width=\textwidth]{string-graph-allocn.png} 240 \caption{Space and time performance, under varying fraction-live targets, for the five string lengths shown, at ( emph{Fixed-size} corpus construction. [MISSING] The identified clusters are for the default fraction-live target, which is 30\%. MISSING: STL results, typically just below the 0.5--0.9 CFA segment. All runs keep an average of 836 strings live, and the median string lifetime is ?? allocations.}280 \caption{Space and time performance, under varying fraction-live targets, for the five string lengths shown, at (\emph{Fixed-size} corpus construction. [MISSING] The identified clusters are for the default fraction-live target, which is 30\%. MISSING: STL results, typically just below the 0.5--0.9 CFA segment. All runs keep an average of 836 strings live, and the median string lifetime is ?? allocations.} 241 281 \label{fig:string-graph-allocn} 242 282 \end{figure} 243 283 244 Figure \ref{fig:string-graph-allocn} shows the results of this experi emnt. At all string sizes, varying the liveness threshold gives offers speed-for-space tradeoffs relative to STL. At the default liveness threshold, all measured string sizes see a ??\%--??\% speedup for a ??\%--??\% increase in memory footprint.284 Figure \ref{fig:string-graph-allocn} shows the results of this experiment. At all string sizes, varying the liveness threshold gives offers speed-for-space tradeoffs relative to STL. At the default liveness threshold, all measured string sizes see a ??\%--??\% speedup for a ??\%--??\% increase in memory footprint. 245 285 246 286
Note: See TracChangeset
for help on using the changeset viewer.