Problem
The lexer and parser create many temporary strings during parsing. For example:
// In lexer.cpp
while (isalnum(current()) || current() == '_') {
value += current(); // String allocation per character
advance();
}
// In parser - string concatenation
string qualified_name = ident + "." + member_name; // Allocation
decl->type = "List<" + element_type + ">"; // Allocation
Suggested Solution
Use `std::string_view` where possible:
-
Lexer: Return views into the source string
struct Token {
TokenType type;
string_view value; // View into source
int line;
};
-
Parser lookups: Use string_view for comparisons
bool is_primitive_type(string_view type);
const StructDef* get_struct(string_view name);
-
Type extraction: Return views for type components
string_view extract_element_type(string_view generic_type, string_view prefix);
Trade-offs
Pros:
- Reduced allocations during parsing
- Faster keyword/type comparisons
- Lower memory pressure
Cons:
- Must ensure source string lifetime exceeds tokens
- Some APIs may still need owned strings
Priority
Low - This is a performance optimization. Profile first to confirm benefit.
Problem
The lexer and parser create many temporary strings during parsing. For example:
Suggested Solution
Use `std::string_view` where possible:
Lexer: Return views into the source string
Parser lookups: Use string_view for comparisons
Type extraction: Return views for type components
string_view extract_element_type(string_view generic_type, string_view prefix);Trade-offs
Pros:
Cons:
Priority
Low - This is a performance optimization. Profile first to confirm benefit.