is_return_internal_reference_eff.html 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Is return_internal_reference efficient?</title>
  5. <link rel="stylesheet" href="../boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../index.html" title="Boost.Python">
  8. <link rel="up" href="../faq.html" title="Chapter&#160;5.&#160;Frequently Asked Questions (FAQs)">
  9. <link rel="prev" href="i_m_getting_the_attempt_to_retur.html" title="I'm getting the &quot;attempt to return dangling reference&quot; error. What am I doing wrong?">
  10. <link rel="next" href="how_can_i_wrap_functions_which_t.html" title="How can I wrap functions which take C++ containers as arguments?">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../images/boost.png"></td></tr></table>
  14. <hr>
  15. <div class="spirit-nav">
  16. <a accesskey="p" href="i_m_getting_the_attempt_to_retur.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="how_can_i_wrap_functions_which_t.html"><img src="../images/next.png" alt="Next"></a>
  17. </div>
  18. <div class="section">
  19. <div class="titlepage"><div><div><h3 class="title">
  20. <a name="faq.is_return_internal_reference_eff"></a><a class="link" href="is_return_internal_reference_eff.html" title="Is return_internal_reference efficient?">Is <code class="computeroutput"><span class="identifier">return_internal_reference</span></code> efficient?</a>
  21. </h3></div></div></div>
  22. <p>
  23. <span class="bold"><strong>Q:</strong></span> /I have an object composed of 12 doubles.
  24. A <code class="computeroutput"><span class="keyword">const</span><span class="special">&amp;</span></code>
  25. to this object is returned by a member function of another class. From the
  26. viewpoint of using the returned object in Python I do not care if I get a
  27. copy or a reference to the returned object. In Boost.Python I have the choice
  28. of using <code class="computeroutput"><span class="identifier">copy_const_reference</span></code>
  29. or <code class="computeroutput"><span class="identifier">return_internal_reference</span></code>.
  30. Are there considerations that would lead me to prefer one over the other,
  31. such as size of generated code or memory overhead?/
  32. </p>
  33. <p>
  34. <span class="bold"><strong>A:</strong></span> <code class="computeroutput"><span class="identifier">copy_const_reference</span></code>
  35. will make an instance with storage for one of your objects, <code class="computeroutput"><span class="identifier">size</span> <span class="special">=</span> <span class="identifier">base_size</span> <span class="special">+</span> <span class="number">12</span> <span class="special">*</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="keyword">double</span><span class="special">)</span></code>.
  36. <code class="computeroutput"><span class="identifier">return_internal_reference</span></code>
  37. will make an instance with storage for a pointer to one of your objects,
  38. <code class="computeroutput"><span class="identifier">size</span> <span class="special">=</span>
  39. <span class="identifier">base_size</span> <span class="special">+</span>
  40. <span class="keyword">sizeof</span><span class="special">(</span><span class="keyword">void</span><span class="special">*)</span></code>. However,
  41. it will also create a weak reference object which goes in the source object's
  42. weakreflist and a special callback object to manage the lifetime of the internally-referenced
  43. object. My guess? <code class="computeroutput"><span class="identifier">copy_const_reference</span></code>
  44. is your friend here, resulting in less overall memory use and less fragmentation,
  45. also probably fewer total cycles.
  46. </p>
  47. </div>
  48. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  49. <td align="left"></td>
  50. <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2015 David
  51. Abrahams, Stefan Seefeld<p>
  52. Distributed under the Boost Software License, Version 1.0. (See accompanying
  53. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  54. </p>
  55. </div></td>
  56. </tr></table>
  57. <hr>
  58. <div class="spirit-nav">
  59. <a accesskey="p" href="i_m_getting_the_attempt_to_retur.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="how_can_i_wrap_functions_which_t.html"><img src="../images/next.png" alt="Next"></a>
  60. </div>
  61. </body>
  62. </html>