Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selectively disabling checked iterators

I am writing a fairly complex application that makes heavy use of STL containers. The application has a single, relatively simple, performance sensitive section that iterates over multiple std::maps and is executed thousands of times. Testing has shown than compiling with checked iterators disabled (_SECURE_SCL set to 0) results in almost a 2x speedup of the program as a whole, entirely concentrated in this section.

However, I cannot compile the application with _SECURE_SCL set to 0 because need to link with libraries that were compiled with _SECURE_SCL enabled and mixing _SECURE_SCL settings leads to problems. Also, I find it rather silly to use unchecked iterators in the entire application, when all the performance sensitive bits happen in a single screenful of code. That would be tantamount to tossing out the baby with the bathwater.

What options do I have to selectively use unchecked iterators for performance sensitive code/containers while maintaining compatibility with libraries compiled with checked iterators?

like image 424
drxzcl Avatar asked Jun 21 '11 09:06

drxzcl


2 Answers

As you have already found out, you can't mix code that uses checked/unchecked iterators, so in order to use it in part of your code, you need to give that part an interface that does not require passing any containers and iterators. (Note that this extends to std::string.)
And of course you will have to put that code into its own executable (DLL). Of course, this requires that there aren't too many calls back and forth across that API.

To be on the safe side, I'd even consider putting that part into a DLL with a C interface.

like image 133
sbi Avatar answered Sep 16 '22 15:09

sbi


This works for me:

vector<BYTE> v;
vector<BYTE>::iterator i;
vector<BYTE>::iterator::_Unchecked_type ui;

i = v.end();
ui = i._Unchecked();

ui++;
like image 21
nnn Avatar answered Sep 20 '22 15:09

nnn