| 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <title>Using dbstl efficiently</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="stl.html" title="Chapter 7. Standard Template Library API" /> |
| 11 | <link rel="prev" href="stl_container_specific.html" title="Dbstl container specific notes" /> |
| 12 | <link rel="next" href="stl_memory_mgmt.html" title="Dbstl memory management" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Using dbstl efficiently</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="stl_container_specific.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 7. Standard Template Library API</th> |
| 26 | <td width="20%" align="right"> <a accesskey="n" href="stl_memory_mgmt.html">Next</a></td> |
| 27 | </tr> |
| 28 | </table> |
| 29 | <hr /> |
| 30 | </div> |
| 31 | <div class="sect1" lang="en" xml:lang="en"> |
| 32 | <div class="titlepage"> |
| 33 | <div> |
| 34 | <div> |
| 35 | <h2 class="title" style="clear: both"><a id="stl_efficienct_use"></a>Using dbstl efficiently</h2> |
| 36 | </div> |
| 37 | </div> |
| 38 | </div> |
| 39 | <div class="toc"> |
| 40 | <dl> |
| 41 | <dt> |
| 42 | <span class="sect2"> |
| 43 | <a href="stl_efficienct_use.html#idp51530568">Using iterators efficiently</a> |
| 44 | </span> |
| 45 | </dt> |
| 46 | <dt> |
| 47 | <span class="sect2"> |
| 48 | <a href="stl_efficienct_use.html#idp51530352">Using containers efficiently</a> |
| 49 | </span> |
| 50 | </dt> |
| 51 | </dl> |
| 52 | </div> |
| 53 | <div class="sect2" lang="en" xml:lang="en"> |
| 54 | <div class="titlepage"> |
| 55 | <div> |
| 56 | <div> |
| 57 | <h3 class="title"><a id="idp51530568"></a>Using iterators efficiently</h3> |
| 58 | </div> |
| 59 | </div> |
| 60 | </div> |
| 61 | <p> |
| 62 | To make the most efficient possible use of iterators: |
| 63 | </p> |
| 64 | <div class="itemizedlist"> |
| 65 | <ul type="disc"> |
| 66 | <li> |
| 67 | <p> |
| 68 | Close an iterator's cursor as soon as possible. |
| 69 | </p> |
| 70 | <p> |
| 71 | Each iterator has an open cursor associated with it, so when you are finished using the |
| 72 | iterator it is a good habit to explicitly close its cursor. This can potentially improve |
| 73 | performance by avoiding locking issues, which will enhanced concurrency. Dbstl will close |
| 74 | the cursor when the iterator is destroyed, but you can close the cursor before that time. |
| 75 | If the cursor is closed, the associated iterator cannot any longer be used. |
| 76 | </p> |
| 77 | <p> |
| 78 | In some functions of container classes, an iterator is used to access the database, and its |
| 79 | cursor is internally created by dbstl. So if you want to specify a non-zero flag for the |
| 80 | <code class="methodname">Db::cursor()</code> call, you need to call the container's |
| 81 | <code class="function">set_cursor_open_flag()</code> function to do so. |
| 82 | </p> |
| 83 | </li> |
| 84 | <li> |
| 85 | <p> |
| 86 | Use const iterators where applicable. |
| 87 | </p> |
| 88 | <p> |
| 89 | If your data access is read only, you are strongly recommended to use a const iterator. In |
| 90 | order to create a const iterator, you must use a const reference to the container object. |
| 91 | For example, supposed we have: |
| 92 | </p> |
| 93 | <pre class="programlisting">db_vector<int> intv(10);</pre> |
| 94 | <p> |
| 95 | then we must use a: |
| 96 | </p> |
| 97 | <pre class="programlisting">const db_vector<int>& intv_ref = intv;</pre> |
| 98 | <p> |
| 99 | reference to invoke the const begin/end functions. <code class="methodname">intv_ref.begin()</code> |
| 100 | will give you a const iterator. You can use a const iterator only to read its referenced |
| 101 | data elements, not update them. However, you should have better performance with this |
| 102 | iterator using, for example, either <code class="literal">iterator::operator*</code> or |
| 103 | <code class="literal">iterator::operator->member</code>. Also, using array indices like |
| 104 | <code class="literal">intv_ref[i]</code> will also perform better. |
| 105 | </p> |
| 106 | <p> |
| 107 | All functions in dbstl's containers which return an iterator or data element reference have |
| 108 | two versions — one returns a const iterator/reference, the other returns an |
| 109 | iterator/reference. If your access is read only, choose the version returning const |
| 110 | iterators/references. |
| 111 | </p> |
| 112 | <p> |
| 113 | Remember that you can only use a const reference to a container object to call the const |
| 114 | versions of <code class="literal">operator*</code> and <code class="literal">operator[]</code>. |
| 115 | </p> |
| 116 | <p> |
| 117 | You can also use the non-const container object or its non-const reference to create a read |
| 118 | only iterator by passing <code class="literal">true</code> to the |
| 119 | <span class="bold"><strong>readonly</strong></span> parameter in the container's |
| 120 | <code class="methodname">begin()</code> method. |
| 121 | </p> |
| 122 | </li> |
| 123 | <li> |
| 124 | <p> |
| 125 | Use pre-increment/pre-decrement rather than post-increment/post-decrement where possible |
| 126 | </p> |
| 127 | <p> |
| 128 | Pre-increment operations are more efficient because the <code class="literal">++iterator</code> avoids |
| 129 | two iterator copy constructions. This is true when you are using C++ standard STL iterators |
| 130 | as well. |
| 131 | </p> |
| 132 | </li> |
| 133 | <li> |
| 134 | <p> |
| 135 | Use bulk retrieval in iterators |
| 136 | </p> |
| 137 | <p> |
| 138 | If your access pattern is to go through the entire database read only, or if you are reading |
| 139 | a continuous range of the database, bulk retrieval can be very useful because it returns |
| 140 | multiple key/data pairs in one database call. But be aware that you can only read the |
| 141 | returned data, you can not update it. Also, if you do a bulk retrieval and read the data, |
| 142 | and simultaneously some other thread of control updates that same data, then unless you are |
| 143 | using a serializable transaction, you will now be working with old data. |
| 144 | </p> |
| 145 | </li> |
| 146 | </ul> |
| 147 | </div> |
| 148 | </div> |
| 149 | <div class="sect2" lang="en" xml:lang="en"> |
| 150 | <div class="titlepage"> |
| 151 | <div> |
| 152 | <div> |
| 153 | <h3 class="title"><a id="idp51530352"></a>Using containers efficiently</h3> |
| 154 | </div> |
| 155 | </div> |
| 156 | </div> |
| 157 | <p> |
| 158 | To make the most efficient possible use of containers: |
| 159 | </p> |
| 160 | <div class="itemizedlist"> |
| 161 | <ul type="disc"> |
| 162 | <li> |
| 163 | <p> |
| 164 | Avoid using container methods that return references. These because they are a little more |
| 165 | expensive. |
| 166 | </p> |
| 167 | <p> |
| 168 | To implement reference semantics, dbstl has to wrap the data element with the current |
| 169 | key/data pair, and must invoke two iterator copy constructions and two Berkeley DB cursor |
| 170 | duplications for each such a call. This is true of non-const versions of these functions: |
| 171 | </p> |
| 172 | <table class="simplelist" border="0" summary="Simple list"> |
| 173 | <tr> |
| 174 | <td> |
| 175 | <code class="methodname">db_vector<T>::operator[]()</code> |
| 176 | </td> |
| 177 | </tr> |
| 178 | <tr> |
| 179 | <td> |
| 180 | <code class="methodname">db_vector<T>::front()</code> |
| 181 | </td> |
| 182 | </tr> |
| 183 | <tr> |
| 184 | <td> |
| 185 | <code class="methodname">db_vector<T>::back()</code> |
| 186 | </td> |
| 187 | </tr> |
| 188 | <tr> |
| 189 | <td> |
| 190 | <code class="methodname">db_vector<T>::at()</code> |
| 191 | </td> |
| 192 | </tr> |
| 193 | <tr> |
| 194 | <td> |
| 195 | <code class="methodname">db_map<>::operator[]()</code> |
| 196 | </td> |
| 197 | </tr> |
| 198 | </table> |
| 199 | <p> |
| 200 | There are alternatives to these functions, mainly through explicit use of iterators. |
| 201 | </p> |
| 202 | </li> |
| 203 | <li> |
| 204 | <p> |
| 205 | Use const containers where possible. |
| 206 | </p> |
| 207 | <p> |
| 208 | The const versions of the functions listed above have less overhead than their non-const |
| 209 | counterparts. Using const containers and iterators can bring more performance when you call |
| 210 | the const version of the overloaded container/iterator methods. To do so, you define a const |
| 211 | container reference to an existing container, and then use this reference to call the |
| 212 | methods. For example, if you have: |
| 213 | </p> |
| 214 | <pre class="programlisting">db_vector<int> container int_vec</pre> |
| 215 | <p> |
| 216 | then you can define a const reference to <code class="literal">int_vec</code>: |
| 217 | </p> |
| 218 | <pre class="programlisting">const db_vector<int>& int_vec_ref; </pre> |
| 219 | <p> |
| 220 | Then you use <code class="methodname">int_vec_ref.begin()</code> to create a const iterator, |
| 221 | <code class="literal">citr</code>. You can now can use <code class="literal">int_vec_ref</code> to call the |
| 222 | const versions of the container's member functions, and then use <code class="literal">citr</code> to |
| 223 | access the data read only. By using <code class="literal">int_vec_ref</code> and |
| 224 | <code class="literal">citr</code>, we can gain better performance. |
| 225 | </p> |
| 226 | <p> |
| 227 | |
| 228 | It is acceptable to call the non-const versions of container functions that return non-const |
| 229 | iterators, and then assign these return values to const iterator objects. But if you are |
| 230 | using Berkeley DB concurrent data store (CDS), be sure to set the |
| 231 | <span class="bold"><strong>readonly</strong></span> parameter for each container method that returns an |
| 232 | iterator to <code class="literal">true</code>. This is because each iterator corresponds to a Berkeley |
| 233 | DB cursor, and so for best performance you should specify that the returned iterator be |
| 234 | read-only so that the underlying cursor is also read-only. Otherwise, the cursor will be a |
| 235 | writable cursor, and performance might be somewhat degraded. If you are not using CDS, but |
| 236 | instead TDS or DS or HA, there is no distinction between read-only cursors and read-write |
| 237 | cursors. Consequently, you do not need to specify the |
| 238 | <span class="bold"><strong>readonly</strong></span> parameter at all. |
| 239 | </p> |
| 240 | </li> |
| 241 | </ul> |
| 242 | </div> |
| 243 | </div> |
| 244 | </div> |
| 245 | <div class="navfooter"> |
| 246 | <hr /> |
| 247 | <table width="100%" summary="Navigation footer"> |
| 248 | <tr> |
| 249 | <td width="40%" align="left"><a accesskey="p" href="stl_container_specific.html">Prev</a> </td> |
| 250 | <td width="20%" align="center"> |
| 251 | <a accesskey="u" href="stl.html">Up</a> |
| 252 | </td> |
| 253 | <td width="40%" align="right"> <a accesskey="n" href="stl_memory_mgmt.html">Next</a></td> |
| 254 | </tr> |
| 255 | <tr> |
| 256 | <td width="40%" align="left" valign="top">Dbstl container specific notes </td> |
| 257 | <td width="20%" align="center"> |
| 258 | <a accesskey="h" href="index.html">Home</a> |
| 259 | </td> |
| 260 | <td width="40%" align="right" valign="top"> Dbstl memory management</td> |
| 261 | </tr> |
| 262 | </table> |
| 263 | </div> |
| 264 | </body> |
| 265 | </html> |