Skip to content

Commit e143a25

Browse files
committed
improve seekInstanceEnd performance
1 parent b802710 commit e143a25

4 files changed

Lines changed: 49 additions & 10 deletions

File tree

src/cllazyfile/lazyP21DataSectionReader.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ lazyP21DataSectionReader::lazyP21DataSectionReader( lazyFileReader * parent, std
3434
// part of readdata1
3535
const namedLazyInstance lazyP21DataSectionReader::nextInstance() {
3636
namedLazyInstance i;
37+
std::streampos end = -1;
3738

3839
//TODO detect comments
3940

@@ -50,9 +51,9 @@ const namedLazyInstance lazyP21DataSectionReader::nextInstance() {
5051
skipWS();
5152
i.name = getDelimitedKeyword(";( /\\");
5253
if( _file.good() ) {
53-
seekInstanceEnd();
54+
end = seekInstanceEnd();
5455
}
55-
if( !_file.good() ) {
56+
if( ( !_file.good() ) || ( end == -1 ) ) {
5657
//invalid instance, so clear everything
5758
i.loc.begin = -1;
5859
delete i.name;

src/cllazyfile/lazyTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ typedef std::multimap< instanceID, instanceID > instanceRefMMap_t;
4141
typedef std::pair< instanceID, instanceID > instanceRefMMap_pair;
4242
typedef std::pair< instanceRefMMap_t::iterator, instanceRefMMap_t::iterator > instanceRefMMap_range;
4343

44+
//TODO try unordered_multimap for this
4445
// instanceTypeMMap - multimap from instance type to instanceID's
4546
typedef std::multimap< std::string, instanceID > instanceTypeMMap_t;
4647
typedef std::pair< std::string, instanceID > instanceTypeMMap_pair;

src/cllazyfile/p21HeaderSectionReader.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ const namedLazyInstance p21HeaderSectionReader::nextInstance() {
4646

4747
assert( i.name->length() > 0 );
4848

49-
if( seekInstanceEnd() >= _sectionEnd ) {
49+
std::streampos end = seekInstanceEnd();
50+
if( (end == -1 ) || ( end >= _sectionEnd ) ) {
5051
//invalid instance, so clear everything
5152
i.loc.begin = -1;
5253
delete i.name;

src/cllazyfile/sectionReader.cc

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ std::streampos sectionReader::findNormalString( const std::string& str, bool sem
4040
}
4141
if( c == '\'' ) {
4242
//push past string
43-
_file.putback( c );
43+
_file.unget();
4444
ToExpressStr( _file, _lazyFile->getInstMgr()->getErrorDesc() );
4545
}
4646
if( ( c == '/' ) && ( _file.peek() == '*' ) ) {
@@ -101,10 +101,46 @@ std::string * sectionReader::getDelimitedKeyword( const char * delimiters ) {
101101
}
102102

103103
std::streampos sectionReader::seekInstanceEnd() {
104-
//TODO do it without findNormalString(), counting parenthesis and looking for ");" outside of a comment/string
105-
//should we use our own string parsing function? could be faster if it doesn't need to store the data...
106-
//or modify the existing one?
107-
return findNormalString( ")", true );
104+
char c;
105+
int parenDepth;
106+
while( c = _file.get(), _file.good() ) {
107+
switch( c ) {
108+
case '(':
109+
parenDepth++;
110+
break;
111+
case '/':
112+
if( _file.peek() == '*' ) {
113+
findNormalString( "*/" );
114+
} else {
115+
return -1;
116+
}
117+
break;
118+
case '\'':
119+
_file.unget();
120+
ToExpressStr( _file, _lazyFile->getInstMgr()->getErrorDesc() );
121+
break;
122+
case '=':
123+
return -1;
124+
case '#':
125+
//TODO record references
126+
break;
127+
case ')':
128+
if( --parenDepth == 0 ) {
129+
skipWS();
130+
if( _file.get() == ';' ) {
131+
return _file.tellg();
132+
} else {
133+
_file.unget();
134+
}
135+
}
136+
default:
137+
break;
138+
}
139+
}
140+
return -1;
141+
//NOTE - old way: return findNormalString( ")", true );
142+
// old memory consumption: 673728kb; User CPU time: 35480ms; System CPU time: 17710ms (with 266MB catia-ferrari-sharknose.stp)
143+
// new memory: 673340kb; User CPU time: 29890ms; System CPU time: 11650ms
108144
}
109145

110146
void sectionReader::locateAllInstances() {
@@ -157,8 +193,8 @@ instanceID sectionReader::readInstanceNumber() {
157193

158194
//TODO: most of the rest of readdata1, all of readdata2
159195
SDAI_Application_instance * sectionReader::getRealInstance( lazyInstanceLoc* inst ) {
160-
// assert( inst->instance == -1 );
161-
std::cerr << "getRealInstance() isn't implemented. Instance #" << inst->instance << std::endl;
196+
// assert( inst->instance < 4 );
197+
std::cerr << "getRealInstance() isn't implemented. Instance #" << inst->instance << std::endl;
162198
return 0;
163199
}
164200

0 commit comments

Comments
 (0)