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