473,811 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Seeking optimization advice

Hello all,

I have written an expression interpreter, and a profiler has told me that a
specific part of it is in need of optimization. My purpose in posting to
this newsgroup is to solicit suggestions on how I might accomplish my
intended aim more efficiently.

My interpreter supports the "if-then-else" construct. In addition, it
supports variables, which means it has a symbol table. As you probably
suspect, the symbol table is implemented using std::map<std::s tring,
var_value_t>.

Currently, the processing of an if-then-else proceeds as follows:

1. Save the original symbol table state; say the returned handle to the
saved state is 1

2. Parse the boolean condition and store the result in rval (note that it is
not possible for the symbol table to be altered here; I do not allow
assignment in the condition of an if-then-else)

3. Parse the "then" subexpression

4. Save the "then" symbol table state; say the returned handle to the saved
state is 2

5. Restore symbol table state 1

6. Parse the "else" subexpression

7. Save the "else" symbol table state; say the returned handle to the saved
state is 3

8. If rval is true, restore symbol table state 2, else restore symbol table
state 3
The following serves as an example expression to which this processing could
be applied (assume 'a' is an integer initialized to 0):

if true then a := 6 else a := 7

Note that the "then" and "else" subexpressions can be arbitrarily complex;
they might even be another if-then-else. And so on recursively... This
fact eliminates some potential shortcuts.

What needs to be optimized is the saving and restoring of the symbol table
states. Currently, states are saved in a std::vector<>. When state is
requested to be saved, I just push_back() the whole symbol table and return
the index as the "handle" to the saved state. When the restoration of a
state is requested, I just index the std::vector<> with the passed handle
(index) and copy back the symbol table at that index. All of this copying
of std::map<> is taking a big toll performance-wise.

I have already tried adding an undo / redo feature that allows me to operate
only upon the actual symbol table itself. Unfortunately, I quickly
discovered that this is not robust enough. State is not properly
maintained. A careful run through of the example expression above
demonstrates this.

Can anybody suggest something more efficient than my current implementation?
It is a requirement that whatever I implement remain within the realm of
standard C++.

Thank you,
Dave
Jul 22 '05 #1
1 1539
"Dave" <be***********@ yahoo.com> wrote
I have written an expression interpreter, and a
profiler has told me that a specific part of it is
in need of optimization.
Smart profiler. Mine just tells me the relative execution times
of various parts of my code. ;-)
My purpose in posting to this newsgroup is to
solicit suggestions on how I might accomplish my
intended aim more efficiently.

My interpreter supports the "if-then-else" construct.
In addition, it supports variables, which means it has
a symbol table. As you probably suspect, the symbol
table is implemented using std::map<std::s tring, var_value_t>.
And here's your first chance at an optimization: a hash table
would be quite a bit faster, especially for large symbol tables.
It's not by chance that most compilers and interpreters use hash
tables for their symbol tables. If your particular implementation
of the standard library doesn't include a hash_map (which is
universally believed to be slated for adoption in the next
standard), you can:

a) use STLport
b) use the Dinkumware Standard C++ Library
c) use any available implementation of a hash table
d) roll your own (hash tables are actually among the most trivial
of data structures)
Currently, the processing of an if-then-else proceeds as follows:
1. Save the original symbol table state; say the returned handle to the saved state is 1

2. Parse the boolean condition and store the result in rval (note that it is not possible for the symbol table to be altered here; I do not allow assignment in the condition of an if-then-else)

3. Parse the "then" subexpression

4. Save the "then" symbol table state; say the returned handle
to the saved state is 2

5. Restore symbol table state 1

6. Parse the "else" subexpression

7. Save the "else" symbol table state; say the returned handle
to the saved state is 3

8. If rval is true, restore symbol table state 2, else restore
symbol table state 3

You'd save yourself a lot of trouble if you decoupled the parsing
from the semantic analysis. If you did, you wouldn't need to save
various states of your symbol table (very time consuming) and you
could skip over the 'then' or 'else' clauses depending on the
evaluation of the condition without impacting the symbol table or
any other program state.
What needs to be optimized is the saving and restoring
of the symbol table states.
No, it needs to be eliminated.

Can anybody suggest something more efficient than
my current implementation?
Done.
It is a requirement that whatever I implement remain
within the realm of standard C++.


Nothing that I suggested departs in any way from the C++ standard
(the standard does NOT preclude using data structures other than
the standard containers), so you're all set.

Claudio Puviani
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
389
by: Dave | last post by:
Hello all, I have written an expression interpreter, and a profiler has told me that a specific part of it is in need of optimization. My purpose in posting to this newsgroup is to solicit suggestions on how I might accomplish my intended aim more efficiently. My interpreter supports the "if-then-else" construct. In addition, it supports variables, which means it has a symbol table. As you probably suspect, the symbol table is...
2
1198
by: Joseph Geretz | last post by:
I don't know if this is the right group for my question, but I'm seeking advice from knowledgable .NET developers. Hopefully I've come to the right place. I work with a document management application which is tightly integrated with the workstation Windows environment. Our software does not necessarily handle any specific document type. User's can import any document type into our repository. On the display side, we use WinAPI to...
20
2044
by: Jim | last post by:
Hi, I am hoping that someone here can help me out. I am for the first time trying to implement a page design using only CSS instead of HTML tables. I've been able to get most of the page done ok. I have my header area and below it 3 columns. My problem is within the header area.
15
3241
by: Kay Schluehr | last post by:
I have a list of strings ls = and want to create a regular expression sx from it, such that sx.match(s) yields a SRE_Match object when s starts with an s_i for one i in . There might be relations between those strings: s_k.startswith(s_1) -> True or s_k.endswith(s_1) -> True. An extreme case would be ls = . For this reason SRE_Match should provide the longest possible match. Is there a Python module able to create an optimized regex rx...
4
2247
by: | last post by:
I am a recent college graduate and am looking for some advice on how to be a skilled C++ developer. My educational background is from a quite mediocre campus. Can anybody please explain what is expected of a fresh graduate in the industry? I have read starter level books(Stroustrup), and some intermediate books(Herb Sutter's Exceptional Series, Scott Meyers classic guides,C++ Common Knowledge, Efficient C++,Advanced C++ Programming Styles...
9
4101
by: Amod | last post by:
hi all, Kindly suggest some tips to optimize the C++ code for higher speed. regards, Amod
7
1770
by: Joseph Geretz | last post by:
I have a Service which runs OK, but I'm abviously not starting it properly. In my OnStart event I commence a long running process which polls a database table and performs various processing. Since this polling loop is entered synchronously from OnStart, basically the OnStart event doesn't terminate for the life of the program. This doesn't give the SCM the correct feedback that the service has started properly. Consequently, the SCM throws...
3
2640
by: Jia Lu | last post by:
Hello all I see there are lots of flat db or db-like modules in the standard python modules. What about the keywords seeking speed of them ? (I want to put about 10000 articles with 10000 IDs, and I can do searching keywords with them) The db-like modules are :
1
1125
by: sukatoa | last post by:
The code below, //I need help here.... public void processChangedLines(int offset, int length) throws BadLocationException { String text = getText(); ResetColor(); Set<String> syntax = keywords.keySet(); Color color ;Pattern p;Matcher m;
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.