Backstory:
- Third time building a sgml parser:
-
QRegularExpression
– Flexible & Unreliable.
-
QString
– Slow & Reliable.
-
QStringView
+ Multithreading – Fast & Clean
Multithreading Strategy:
- Assume every index can represent one token.
-
- Store tokens by that index
-
- Tokens can be checked for by simply checking if its index integer is present
- Avoid Overhead & Doublework
-
- Have one thread work forwards, and the other work backwards.
-
- Forward thread:
p
|q
:Backwards thread
- Forward thread:
-
- Each thread simply checks if its current index has gone past the other thread’s index:
void QSgmlParser::forward()
{
while ( ++p_I < q_I ) { ... }
}
void QSgmlParser::backward()
{
while ( --q_I > p_I ) { ... }
}
Why not divide the stringview into 8 portions and tokenize it as such?
- I may try it, however I fear that the overhead for resolving false positives, or finding appropriate nodes, would negate most performance gains.
- Starting in the middle of a tag would be a nightmare
- The only doublework for a Forward & Backward thread would be at most, one token.
Proof of Concept:
qtoken.h
#ifndef QTOKEN_H
#define QTOKEN_H
#include <QStringView>
class QToken : public QStringView
{
public:
enum Token {
None = 0
, Tag // Ambiguous Tag
, Tag_s // Self Closing Tag
, Tag_p // Starting Tag
, Tag_q // Closing Tag
, Name // Attribute Name
, Value // Attribute value
, Comment // comment
, CharacterData // CDATA
, Instruction // instruction
, Content // content
, Declaration //
, Script
};
inline QToken( Token token, const QStringView &sv )
: QStringView(sv), m_Token(token)
{}
inline bool operator==( const QToken &token ) const { return this->begin() == token.begin(); }
private:
Token m_Token;
};
#endif // QTOKEN_H
qsgmlparser.h
#ifndef QSGMLPARSER_H
#define QSGMLPARSER_H
#include <QObject>
#include <QtParsing/qtoken.h>
#include <QtConcurrent>
#include <QFutureWatcher>
class QSgmlParser : public QObject
{
Q_OBJECT
public:
explicit QSgmlParser( const QString &sgml, QObject *parent = nullptr );
protected:
void benchmark_multithread();
void benchmark_forwards();
void benchmark_backwards();
void forward ();
void backward();
inline void switch_p_LT();
inline void switch_p_LT_AZ();
inline void switch_q_GT();
inline void switch_q_GT_FS();
private:
QFutureWatcher<void> m_FutureWatcher;
const QStringView p_Sgml;
const QStringView q_Sgml;
int p_I; // "p" Forward
int q_I; // "q" Backward
int p_Position;
int q_Position;
QList<QToken> p_Set; // Have tried different containers
QList<QToken> q_Set; // QSet, QMap, QList, etc
QList<QToken> pq_Set;// Does not affect multithreading
const int m_Length;
};
#endif // QSGMLPARSER_H
qsgmlparser.cpp
#include "qsgmlparser.h"
#define ct_Benchmark(EXPR, repeat){
QElapsedTimer et;
int initialTime(0);
int totalTime(0);
int repeated = repeat;
QList<int> times;
et.start();
EXPR;
initialTime = et.restart();
for(int ___i_=0;___i_<repeated;___i_++) {
EXPR;
}
totalTime = et.restart();
double averageTime = double(totalTime/repeated);
const QString expression(#EXPR);
const QString iterations(" Cycles: "+QString::number(repeated));
const QString initial ("Initial: "+QString::number(initialTime));
const QString total (" Total: "+QString::number(totalTime));
const QString average ("Average: "+QString::number(averageTime));
qDebug() << expression << iterations << initial << total << average;
}
QSgmlParser::QSgmlParser( const QString &sgml, QObject *parent )
: QObject{parent}
, p_Sgml(sgml)
, q_Sgml(sgml)
, p_I(-1)
, q_I( sgml.length() )
, m_Length(q_I)
{
/* Tokenization */
ct_Benchmark( this->benchmark_forwards();, 1000 );
ct_Benchmark( this->benchmark_backwards();, 1000 );
ct_Benchmark( this->benchmark_multithread();, 1000 );
pq_Set << p_Set << q_Set;
QStringList sl;
foreach( QToken t, pq_Set ) {
sl << t.toString();
}
sl.sort();
ct_Halt(sl);
}
void QSgmlParser::benchmark_multithread()
{
p_I = -1;
q_I = p_Sgml.length();
p_Set.clear();
q_Set.clear();
QtConcurrent::run( &QSgmlParser::forward , this );
m_FutureWatcher.setFuture( QtConcurrent::run( &QSgmlParser::backward, this ) );
QEventLoop wait;
connect( &m_FutureWatcher, &QFutureWatcher<void>::finished, &wait, &QEventLoop::quit );
wait.exec();
}
void QSgmlParser::benchmark_forwards()
{
p_I = -1;
q_I = p_Sgml.length();
p_Set.clear();
q_Set.clear();
this->forward();
}
void QSgmlParser::benchmark_backwards()
{
p_I = -1;
q_I = p_Sgml.length();
p_Set.clear();
q_Set.clear();
this->backward();
}
void QSgmlParser::forward()
{
while ( ++p_I < q_I ) {
switch ( p_Sgml[p_I].unicode() ) {
case '<':
this->switch_p_LT();
break;
default:
break;
}
}
}
void QSgmlParser::backward()
{
while ( --q_I > p_I ) {
switch ( q_Sgml[q_I].unicode() ) {
case '>':
this->switch_q_GT();
break;
default:
break;
}
}
}
#define p_ADD( TOKEN, STRING_VIEW )
p_Set << QToken( TOKEN, STRING_VIEW )
#define q_ADD( TOKEN, STRING_VIEW )
q_Set << QToken( TOKEN, STRING_VIEW )
#define Double_Quotes '"': case U'”'
#define IS_SINGLE_OR_DOUBLE(SGML,ITERATOR)
if ( isSingle ) {
if ( SGML[ITERATOR] == QChar(''') ) {
isSingle = false;
}
continue;
}
if ( isDouble ) {
if ( SGML[ITERATOR] == QChar('"') ) {
isDouble = false;
}
continue;
}
void QSgmlParser::switch_p_LT()
{
p_Position = p_I;
switch ( p_Sgml[++p_I].unicode() ) {
default:
this->switch_p_LT_AZ();
return;
}
}
void QSgmlParser::switch_p_LT_AZ()
{
bool isSingle(false);
bool isDouble(false);
while ( ++p_I < m_Length ) {
IS_SINGLE_OR_DOUBLE(p_Sgml,p_I)
switch ( p_Sgml[p_I].unicode() ) {
case ''':
isSingle = true;
continue;
case '"':
isDouble = true;
continue;
case '>':
p_ADD( QToken::Tag_s, p_Sgml.sliced( p_Position, p_I - p_Position + 1 ) );
return;
}
}
}
void QSgmlParser::switch_q_GT()
{
q_Position = q_I;
switch ( q_Sgml[q_I].unicode() ) {
default:
this->switch_q_GT_FS();
return;
}
}
void QSgmlParser::switch_q_GT_FS()
{
bool isSingle(false);
bool isDouble(false);
while ( --q_I >= 0 ) {
IS_SINGLE_OR_DOUBLE(q_Sgml,q_I)
switch ( q_Sgml[q_I].unicode() ) {
case ''':
isSingle = true;
continue;
case '"':
isDouble = true;
continue;
case '<':
q_ADD( QToken::Tag_s, q_Sgml.sliced( q_I, q_Position - q_I + 1 ) );
return;
}
}
}
Sample sgml
<foo0001/>
<foo0001/>
<foo0001/>
<foo0002/>
<foo0002/>
<foo0002/>
...
<foo0998/>
<foo0999/>
<foo0999/>
<foo0999/>
<foo1000/>
<foo1000/>
<foo1000/>
Results
Build | Forward | Backward | Multithreading |
---|---|---|---|
Release | 778 | 766 | 1578 |
Debug | 12484 | 12247 | 14127 |
IE under both builds, the multithreading is taking longer. I am using GCC.
Given the overhead, I feel like I should be able to get a score of at least 500 on the multithreading.
This persists regardless of how large my sample size becomes, which makes me rule out the threading overhead. My goal is to at least be able to get a modest gain but as of now, I feel like there is a large oversight that I simply do not understand, and I don’t think its the overhead. And before I actually expend the effort on flushing out the parser, I need to see if I can actually gain an advantage for the rudimentary parsing first.
I tried asking around but have not gotten much feedback. AI did suggest something about CPU cache, but I couldnt really follow what it was trying to say, and suspected it might just be giving me some answer that is not particularly relevant.
Can anyone help me get to the bottom of why this rudimentary parsing is taking twice as long instead of half as long?
What am I overlooking?
Thanks.