Ver Fonte

Documented how to choose the right collection and when to use Buffer and SafePointer instead.

David Piuva há 8 horas atrás
pai
commit
03fa03ad43

+ 107 - 0
Doc/ChoosingStorage.html

@@ -0,0 +1,107 @@
+<!DOCTYPE html> <HTML lang=en> <HEAD> <STYLE>
+body { background-color: #EEFFEE;  font-size: 1.0rem; font-family: Arial; max-width: 60rem;
+      color: #000000; margin: 0px;
+      padding-left:  0px; padding-right:  0px; padding-top:  0px; padding-bottom:  0px; }
+H1 {  padding-left: 10px; padding-right:  0px; padding-top: 10px; padding-bottom: 10px; font-size: 1.4rem; }
+H2 {  padding-left: 10px; padding-right:  0px; padding-top: 10px; padding-bottom:  0px; font-size: 1.2rem; }
+blockquote {
+  tab-size: 3rem;
+  color: #88FF88; background: #000000;
+  font-size: 0.95rem; font-family: monospace;
+  padding-left: 5px; padding-right: 5px;
+  padding-top: 5px; padding-bottom: 5px;
+}
+P {   padding-left: 20px; padding-right:  0px; padding-top:  0px; padding-bottom:  0px; }
+IMG { padding-left:  0px; padding-right:  0px; padding-top:  2px; padding-bottom:  0px;
+      max-width: 100%; }
+A { display: inline; border-radius: 4px;
+    font-size: 1.0rem; font-family: Arial; color: #000044; text-decoration: none;
+    padding-left: 4px; padding-right: 4px; padding-top: 4px; padding-bottom: 4px; }
+A:hover { color: #FFFF00; background: #000044; }
+A:active { color: #FFFFFF; background: #444444; }
+</STYLE> </HEAD> <BODY>
+<IMG SRC="Images/Title.png" ALT="Images/Title.png">
+<P>
+<A href="Manual.html">Back to main page</A>
+</P><P>
+</P><H1> Choosing storage</H1><P>
+</P><P>
+The terms "safe" and "unsafe" are relative to the context.
+A low level pointer is considered safe from having bound checks in debug mode, because it is supposed to be bruteforce tested in debug mode to catch all bugs.
+A high level collection is considered unsafe if it does not have bound checks in release mode, because collections are used for more complex control flow where bruteforce testing might not be enough.
+
+</P><P>
+</P><IMG SRC="Images/Border.png"><P>
+</P><H2> Buffer and SafePointer</H2><P>
+</P><P>
+<IMG SRC="Images/SmallDot.png">
+Source/DFPSR/api/bufferAPI.h
+
+</P><P>
+<IMG SRC="Images/SmallDot.png">
+Source/DFPSR/base/SafePointer.h
+
+</P><P>
+The fastest way to access data is to use Buffer and SafePointer.
+Buffer replaces raw memory allocations, is responsible for ownership using automatic reference counting, and is padded and aligned for SIMD vectorization by default.
+SafePointer replaces raw pointers with zero overhead in release mode and safety checks in debug mode.
+
+</P><P>
+<IMG SRC="Images/Warning.png" ALT="Warning">
+While Buffer aligns the start for you, it does not know which data types you are storing inside of it.
+Make sure that the innermost types (such as integers and floats) are aligned by their own size to ensure good performance and avoid crashes on certain processors that do not support unaligned access.
+
+</P><P>
+</P><IMG SRC="Images/Border.png"><P>
+</P><H2> FixedArray</H2><P>
+</P><P>
+<IMG SRC="Images/SmallDot.png">
+Source/DFPSR/collection/FixedArray
+
+</P><P>
+For small fixed size allocations, FixedArray is the simplest of all collections by allocating all data directly in the head without pointing anywhere else in the memory.
+The [] operator is bound checked by default in release mode, but if you only want bound checks in debug mode, you can call unsafe_readAccess or unsafe_writeAccess instead.
+
+</P><P>
+</P><IMG SRC="Images/Border.png"><P>
+</P><H2> Array</H2><P>
+</P><P>
+<IMG SRC="Images/SmallDot.png">
+Source/DFPSR/collection/Array
+
+</P><P>
+When you know the size to allocate ahead of time but it is large or you do not know it in compile time, Array allows dynamically allocating the data.
+The [] operator is bound checked by default in release mode, but if you only want bound checks in debug mode, you can call unsafe_readAccess or unsafe_writeAccess instead.
+To change the size, assign a new array in its place constructed with different dimensions.
+
+</P><P>
+</P><IMG SRC="Images/Border.png"><P>
+</P><H2> Field</H2><P>
+</P><P>
+<IMG SRC="Images/SmallDot.png">
+Source/DFPSR/collection/Field
+
+</P><P>
+Field is a slow but convenient wrapper around Array that simplifies two-dimensional storage.
+Rows are not padded, which preserves memory but makes access slower.
+Ideal for reference implementations, quick prototypes and things that will not become a performance bottleneck.
+If you only need to store unsigned integers or floats, consider using multiple images instead, for easy visualization of results.
+
+</P><P>
+</P><IMG SRC="Images/Border.png"><P>
+</P><H2> List</H2><P>
+</P><P>
+<IMG SRC="Images/SmallDot.png">
+Source/DFPSR/collection/List
+
+</P><P>
+List is an array based list with continuous memory.
+The [] operator is always bound checked.
+
+</P><P>
+<IMG SRC="Images/Warning.png" ALT="Warning">
+Reserving memory or adding elements to List may cause reallocation of the buffer, which may invalidate any pointers and references to the data.
+Never change the size of a list while iterating over the elements using pointers.
+Deleting elements from List should be done by looping backwards by index, so that the loop is always entering regions not affected by element removal.
+</P>
+</BODY> </HTML>

+ 5 - 1
Doc/Generator/Input/Manual.txt

@@ -10,6 +10,7 @@ Anything with "impl" in the name is expected to change at any time, so don't do
 Anything that requires defining DFPSR_INTERNAL_ACCESS before a header is also considered internal.
 
 ---
+Title2: Read first
 
 *
 <- Starting.html | Getting started
@@ -35,6 +36,9 @@ Title2: APIs
 ---
 Title2: Techniques
 
+*
+<- ChoosingStorage.html | Choosing storage
+
 *
 <- ImageProcessing.html | Image processing
 
@@ -52,4 +56,4 @@ Title2: Technical details
 
 *
 <- Security.html | Security
----
+---


+ 4 - 1
Doc/Manual.html

@@ -37,7 +37,7 @@ Anything that requires defining DFPSR_INTERNAL_ACCESS before a header is also co
 
 </P><P>
 </P><IMG SRC="Images/Border.png"><P>
-
+</P><H2> Read first</H2><P>
 </P><P>
 <IMG SRC="Images/SmallDot.png">
 <A href="Starting.html">Getting started</A>
@@ -64,6 +64,9 @@ Anything that requires defining DFPSR_INTERNAL_ACCESS before a header is also co
 </P><H2> Techniques</H2><P>
 </P><P>
 <IMG SRC="Images/SmallDot.png">
+<A href="ChoosingStorage.html">Choosing storage</A>
+</P><P>
+<IMG SRC="Images/SmallDot.png">
 <A href="ImageProcessing.html">Image processing</A>
 </P><P>
 </P><IMG SRC="Images/Border.png"><P>

+ 1 - 0
Source/DFPSR/collection/List.h

@@ -36,6 +36,7 @@ namespace dsr {
 // TODO:
 // * Allow storing names of lists in debug mode for better error messages, using the same setName method as used in Handle.
 // * Allow getting a SafePointer to all elements for faster loops without bound checks in release mode.
+//   This needs to be marked as unsafe with a warning about potential invalidation of pointers from reallocation.
 
 // Because elements are returned by reference, the list can not know when an element is modified.
 //   Therefore it must clone the entire content when assigned by value for consistent behavior during reallocation.