mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-10 14:07:43 +00:00
Performance improvement of IfcParse tokenizer (#82)
* Applied MSVC tabify to IfcParse.cpp/h. * Now Argument base class has throwing implementation for each conversion operator (removed those from derived classes). * Added NullArgument class for the case when null arguments appear after setting elements. * Now operator tokens are handled just like any other token. * Refactoring: Token is now a struct with good fields, not a nameless pair. * Now end position of token is set at its construction. * Now type of token is determined on construction, as* function simply check stored type. * Now int-s, float-s and some etc are parsed when token is created. * Implemented simple way to remove spaces in parsing. * Tokens are now created without allocations. * Call RemoveTokenSeparators once for every token. * Added temporary std::string object in lexer (for optimization purposes). * Reimplemented asString without allocations. * Parsing keywords without allocations. * Added checks for type to tokenFunc::as*** functions.
This commit is contained in:
committed by
Thomas Krijnen
parent
cbbdb61a0c
commit
8725c66306
+55
-38
@@ -60,7 +60,34 @@ namespace IfcParse {
|
||||
class IfcFile;
|
||||
class IfcSpfLexer;
|
||||
|
||||
typedef std::pair<IfcSpfLexer*, unsigned> Token;
|
||||
enum TokenType {
|
||||
Token_NONE,
|
||||
Token_STRING,
|
||||
Token_IDENTIFIER,
|
||||
Token_OPERATOR,
|
||||
Token_ENUMERATION,
|
||||
Token_KEYWORD,
|
||||
Token_INT,
|
||||
Token_BOOL,
|
||||
Token_FLOAT,
|
||||
Token_BINARY
|
||||
};
|
||||
|
||||
struct Token {
|
||||
IfcSpfLexer* lexer; //TODO: remove it from here
|
||||
unsigned startPos, endPos;
|
||||
TokenType type;
|
||||
union {
|
||||
bool value_bool; //types: BOOL
|
||||
char value_char; //types: OPERATOR
|
||||
int value_int; //types: INT, IDENTIFIER
|
||||
double value_double; //types: FLOAT
|
||||
};
|
||||
|
||||
Token() : lexer(0), startPos(0), endPos(0), type(Token_NONE) {}
|
||||
Token(IfcSpfLexer* _lexer, unsigned _startPos, unsigned _endPos, TokenType _type)
|
||||
: lexer(_lexer), startPos(_startPos), endPos(_endPos), type(_type) {}
|
||||
};
|
||||
|
||||
/// Provides functions to convert Tokens to binary data
|
||||
/// Tokens are merely offsets to where they can be read in the file
|
||||
@@ -75,7 +102,9 @@ namespace IfcParse {
|
||||
/// Returns whether the token can be interpreted as an identifier
|
||||
static bool isIdentifier(const Token& t);
|
||||
/// Returns whether the token can be interpreted as a syntactical operator
|
||||
static bool isOperator(const Token& t, char op = 0);
|
||||
static bool isOperator(const Token& t);
|
||||
/// Returns whether the token is a given operator
|
||||
static bool isOperator(const Token& t, char op);
|
||||
/// Returns whether the token can be interpreted as an enumerated value
|
||||
static bool isEnumeration(const Token& t);
|
||||
/// Returns whether the token can be interpreted as a datatype name
|
||||
@@ -90,12 +119,16 @@ namespace IfcParse {
|
||||
static bool isBinary(const Token& t);
|
||||
/// Returns the token interpreted as an integer
|
||||
static int asInt(const Token& t);
|
||||
/// Returns the token interpreted as an identifier
|
||||
static int asIdentifier(const Token& t);
|
||||
/// Returns the token interpreted as an boolean (.T. or .F.)
|
||||
static bool asBool(const Token& t);
|
||||
/// Returns the token as a floating point number
|
||||
static double asFloat(const Token& t);
|
||||
/// Returns the token as a string (without the dot or apostrophe)
|
||||
static std::string asString(const Token& t);
|
||||
/// Returns the token as a string in internal buffer (for optimization purposes)
|
||||
static const std::string &asStringRef(const Token& t);
|
||||
/// Returns the token as a string (without the dot or apostrophe)
|
||||
static boost::dynamic_bitset<> asBinary(const Token& t);
|
||||
/// Returns a string representation of the token (including the dot or apostrophe)
|
||||
@@ -106,23 +139,26 @@ namespace IfcParse {
|
||||
// Functions for creating Tokens from an arbitary file offset
|
||||
// The first 4 bits are reserved for Tokens of type ()=,;$*
|
||||
//
|
||||
Token TokenPtr(IfcSpfLexer* tokens, unsigned int offset);
|
||||
Token TokenPtr(char c);
|
||||
Token TokenPtr();
|
||||
Token OperatorTokenPtr(IfcSpfLexer* tokens, unsigned start, unsigned end);
|
||||
Token GeneralTokenPtr(IfcSpfLexer* tokens, unsigned start, unsigned end);
|
||||
Token NoneTokenPtr();
|
||||
|
||||
/// A stream of tokens to be read from a IfcSpfStream.
|
||||
class IfcParse_EXPORT IfcSpfLexer {
|
||||
private:
|
||||
IfcCharacterDecoder* decoder;
|
||||
//storage for temporary string without allocation
|
||||
mutable std::string _tempString;
|
||||
unsigned int skipWhitespace();
|
||||
unsigned int skipComment();
|
||||
public:
|
||||
std::string &GetTempString() const { return _tempString; }
|
||||
IfcSpfStream* stream;
|
||||
IfcFile* file;
|
||||
IfcSpfLexer(IfcSpfStream* s, IfcFile* f);
|
||||
Token Next();
|
||||
~IfcSpfLexer();
|
||||
std::string TokenString(unsigned int offset);
|
||||
void TokenString(unsigned int offset, std::string &result);
|
||||
};
|
||||
|
||||
/// Argument of type list, e.g.
|
||||
@@ -139,13 +175,6 @@ namespace IfcParse {
|
||||
|
||||
IfcUtil::ArgumentType type() const;
|
||||
|
||||
operator int() const;
|
||||
operator bool() const;
|
||||
operator double() const;
|
||||
operator std::string() const;
|
||||
operator boost::dynamic_bitset<>() const;
|
||||
operator IfcUtil::IfcBaseClass*() const;
|
||||
|
||||
operator std::vector<int>() const;
|
||||
operator std::vector<double>() const;
|
||||
operator std::vector<std::string>() const;
|
||||
@@ -165,6 +194,19 @@ namespace IfcParse {
|
||||
std::string toString(bool upper=false) const;
|
||||
};
|
||||
|
||||
|
||||
/// Argument being null, e.g. '$'
|
||||
/// == ===
|
||||
class IfcParse_EXPORT NullArgument : public Argument {
|
||||
public:
|
||||
NullArgument() {}
|
||||
IfcUtil::ArgumentType type() const { return IfcUtil::Argument_NULL; }
|
||||
bool isNull() const { return true; }
|
||||
unsigned int size() const { return 1; }
|
||||
Argument* operator [] (unsigned int /*i*/) const { throw IfcException("Argument is not a list of attributes"); }
|
||||
std::string toString(bool /*upper=false*/) const { return "$"; }
|
||||
};
|
||||
|
||||
/// Argument of type scalar or string, e.g.
|
||||
/// #1=IfcVector(#2,1.0);
|
||||
/// == ===
|
||||
@@ -184,16 +226,6 @@ namespace IfcParse {
|
||||
operator boost::dynamic_bitset<>() const;
|
||||
operator IfcUtil::IfcBaseClass*() const;
|
||||
|
||||
operator std::vector<int>() const;
|
||||
operator std::vector<double>() const;
|
||||
operator std::vector<std::string>() const;
|
||||
operator std::vector<boost::dynamic_bitset<> >() const;
|
||||
operator IfcEntityList::ptr() const;
|
||||
|
||||
operator std::vector< std::vector<int> >() const;
|
||||
operator std::vector< std::vector<double> >() const;
|
||||
operator IfcEntityListList::ptr() const;
|
||||
|
||||
bool isNull() const;
|
||||
unsigned int size() const;
|
||||
|
||||
@@ -213,23 +245,8 @@ namespace IfcParse {
|
||||
|
||||
IfcUtil::ArgumentType type() const;
|
||||
|
||||
operator int() const;
|
||||
operator bool() const;
|
||||
operator double() const;
|
||||
operator std::string() const;
|
||||
operator boost::dynamic_bitset<>() const;
|
||||
operator IfcUtil::IfcBaseClass*() const;
|
||||
|
||||
operator std::vector<int>() const;
|
||||
operator std::vector<double>() const;
|
||||
operator std::vector<std::string>() const;
|
||||
operator std::vector<boost::dynamic_bitset<> >() const;
|
||||
operator IfcEntityList::ptr() const;
|
||||
|
||||
operator std::vector< std::vector<int> >() const;
|
||||
operator std::vector< std::vector<double> >() const;
|
||||
operator IfcEntityListList::ptr() const;
|
||||
|
||||
bool isNull() const;
|
||||
unsigned int size() const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user