Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I suspect many values aren't strings. So the first thing you can do is to get rid of the <code>string</code> object if you don't need it. Put it into an union. Another thing is that probably many of your strings are only small, thus you can get rid of heap allocation if you save small strings in the object itself. LLVM has the <a href="http://llvm.org/doxygen/classllvm_1_1SmallString.html" rel="nofollow noreferrer"><code>SmallString</code></a> template for that. And then you can use string interning, as another answer says too. LLVM has the <a href="http://llvm.org/doxygen/classllvm_1_1StringPool.html" rel="nofollow noreferrer"><code>StringPool</code></a> class for that: Call <code>intern("foo")</code> and get a smart pointer refering to a shared string potentially used by other <code>myInterpreterValue</code> objects too. </p> <p>The union can be written like this</p> <pre><code>class myInterpreterValue { boost::variant&lt;int, string&gt; value; }; </code></pre> <p><code>boost::variant</code> does the type tagging for you. You can implement it like this, if you don't have boost. The alignment can't be gotten portably in C++ yet, so we push some types that possibly require some large alignment into the storage union. </p> <pre><code>class myInterpreterValue { union Storage { // for getting alignment long double ld_; long long ll_; // for getting size int i1; char s1[sizeof(string)]; // for access char c; }; enum type { IntValue, StringValue } m_type; Storage m_store; int *getIntP() { return reinterpret_cast&lt;int*&gt;(&amp;m_store.c); } string *getStringP() { return reinterpret_cast&lt;string*&gt;(&amp;m_store.c); } public: myInterpreterValue(string const&amp; str) { m_type = StringValue; new (static_cast&lt;void*&gt;(&amp;m_store.c)) string(str); } myInterpreterValue(int i) { m_type = IntValue; new (static_cast&lt;void*&gt;(&amp;m_store.c)) int(i); } ~myInterpreterValue() { if(m_type == StringValue) { getStringP()-&gt;~string(); // call destructor } } string &amp;asString() { return *getStringP(); } int &amp;asInt() { return *getIntP(); } }; </code></pre> <p>You get the idea. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload