Skip to content

Commit

Permalink
more const
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed May 31, 2022
1 parent c2ee83e commit 071fa0d
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 38 deletions.
5 changes: 3 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ project(typescript)

add_definitions(-DTRACY_ENABLE)

set(CMAKE_CXX_FLAGS "-Wall -Wextra -O3 -ffast-math")
#set(CMAKE_CXX_FLAGS "-Wall -Wextra -O2")

add_subdirectory(tests)

set(CMAKE_CXX_FLAGS "-Wall -Wextra -O2")
#set(CMAKE_CXX_FLAGS "-Wall -Wextra -O3 -ffast-math")

add_library(typescript utf.h utf.cpp core.h core.cpp utilities.h utilities.cpp node_test.h node_test.cpp
syntax_cursor.h syntax_cursor.cpp parser2.h parser2.cpp types.h types.cpp path.h path.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace ts {
}
vector<string> charToStringVector(vector<const char *> chars) {
vector<string> s;
for (auto c: chars) s.push_back(c);
for (auto &&c: chars) s.push_back(c);
return s;
}
bool startsWith(const string &str, const string &suffix) {
Expand Down
4 changes: 2 additions & 2 deletions src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace ts {
//inline bool some(optional<vector<T>> array) {
if (array) {
if (predicate) {
for (auto &v: (*array)) {
for (auto &&v: (*array)) {
if ((*predicate)(v)) {
return true;
}
Expand Down Expand Up @@ -211,7 +211,7 @@ namespace ts {
const unordered_map<K, V> &map1
) {
unordered_map<V, K> res;
for (auto &i: map1) {
for (auto &&i: map1) {
res[i.second] = i.first;
}
return res;
Expand Down
6 changes: 4 additions & 2 deletions src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ namespace ts {

void Factory::aggregateChildrenFlags(shared<NodeArray> &children) {
int subtreeFlags = (int) TransformFlags::None;
for (auto &child: children->list) {
for (auto &&child: children->list) {
subtreeFlags |= propagateChildFlags(child);
}
children->transformFlags = subtreeFlags;
}

shared<NodeArray> Factory::createNodeArray(sharedOpt<NodeArray> elements, optional<bool> hasTrailingComma) {
ZoneScoped;
if (elements) {
if (!hasTrailingComma || elements->hasTrailingComma == hasTrailingComma) {
if (!hasTrailingComma || elements->hasTrailingComma == *hasTrailingComma) {
// Ensure the transform flags have been aggregated for this NodeArray
if (elements->transformFlags == (int) types::TransformFlags::None) {
aggregateChildrenFlags(elements);
Expand Down Expand Up @@ -247,6 +248,7 @@ namespace ts {

// @api
shared<Identifier> Factory::createIdentifier(string text, sharedOpt<NodeArray> typeArguments, optional<SyntaxKind> originalKeywordKind) {
ZoneScoped;
auto node = createBaseIdentifier(std::move(text), originalKeywordKind);
if (typeArguments) {
// NOTE: we do not use `setChildren` here because typeArguments in an identifier do not contribute to transformations
Expand Down
2 changes: 1 addition & 1 deletion src/factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace ts {
template<class T>
inline shared<NodeArray> asNodeArray(const vector<shared<T>> &array) {
auto nodeArray = make_shared<NodeArray>();
for (auto node: array) nodeArray->list.push_back(node);
for (auto &&node: array) nodeArray->list.push_back(node);
return nodeArray;
}

Expand Down
2 changes: 1 addition & 1 deletion src/node_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace ts {
int modifiersToFlags(shared<NodeArray> modifiers) {
int flags = (int) ModifierFlags::None;
if (modifiers) {
for (auto &modifier: modifiers->list) {
for (auto &&modifier: modifiers->list) {
flags |= (int) modifierToFlag(modifier->kind);
}
}
Expand Down
30 changes: 18 additions & 12 deletions src/parser2.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ namespace ts {
if (cbNodes) {
return cbNodes(nodes);
}
for (auto node: nodes->list) {
for (auto &&node: nodes->list) {
auto result = cbNode(node);
if (result) {
return result;
Expand Down Expand Up @@ -175,7 +175,7 @@ namespace ts {

inline bool some(const sharedOpt<NodeArray> &array, const function<bool(shared<Node> value)> &predicate) {
if (array) {
for (auto &v: array->list) {
for (auto &&v: array->list) {
if (predicate(v)) {
return true;
}
Expand Down Expand Up @@ -1705,14 +1705,16 @@ namespace ts {
return to<T>(finishNode(result, pos));
}

string internIdentifier(string text) {
auto identifier = get(identifiers, text);
if (!identifier) {
identifier = text;
identifiers[text] = text;
// set(identifiers, text, *identifier);
}
return *identifier;
string internIdentifier(const string &text) {
//todo: add back
return text;
// auto identifier = get(identifiers, text);
// if (!identifier) {
// identifier = text;
//// identifiers[text] = text;
//// set(identifiers, text, *identifier);
// }
// return *identifier;
}

bool isBindingIdentifier() {
Expand All @@ -1725,6 +1727,7 @@ namespace ts {
}

shared<Identifier> parseBindingIdentifier(optional<DiagnosticMessage> privateIdentifierDiagnosticMessage = {}) {
ZoneScoped;
return createIdentifier(isBindingIdentifier(), /*diagnosticMessage*/ {}, privateIdentifierDiagnosticMessage);
}

Expand Down Expand Up @@ -3044,6 +3047,7 @@ namespace ts {
// with magic property names like '__proto__'. The 'identifiers' object is used to share a single string instance for
// each identifier in order to reduce memory consumption.
shared<Identifier> createIdentifier(bool isIdentifier, optional<DiagnosticMessage> diagnosticMessage = {}, optional<DiagnosticMessage> privateIdentifierDiagnosticMessage = {}) {
ZoneScoped;
if (isIdentifier) {
identifierCount++;
auto pos = getNodePos();
Expand Down Expand Up @@ -3838,6 +3842,7 @@ namespace ts {
}

shared<ArrayBindingPattern> parseArrayBindingPattern() {
ZoneScoped;
auto pos = getNodePos();
parseExpected(SyntaxKind::OpenBracketToken);
auto elements = parseDelimitedList(ParsingContext::ArrayBindingElements, CALLBACK(parseArrayBindingElement));
Expand Down Expand Up @@ -3950,6 +3955,7 @@ namespace ts {
}

shared<ObjectBindingPattern> parseObjectBindingPattern() {
ZoneScoped;
auto pos = getNodePos();
parseExpected(SyntaxKind::OpenBraceToken);
auto elements = parseDelimitedList(ParsingContext::ObjectBindingElements, CALLBACK(parseObjectBindingElement));
Expand Down Expand Up @@ -5161,7 +5167,7 @@ namespace ts {
token() == SyntaxKind::OpenBracketToken) {
auto isAmbient = some(modifiers, CALLBACK(isDeclareModifier));
if (isAmbient && modifiers) {
for (auto &m: modifiers->list) {
for (auto &&m: modifiers->list) {
m->flags |= (int) NodeFlags::Ambient;
}
return doInsideOfContext<shared<Node>>((int) NodeFlags::Ambient, [&]() {
Expand Down Expand Up @@ -7222,7 +7228,7 @@ namespace ts {
auto decorators = parseDecorators();
auto modifiers = parseModifiers();
if (isAmbient) {
for (auto &m: modifiers->list) {
for (auto &&m: modifiers->list) {
m->flags |= (int) NodeFlags::Ambient;
}
return doInsideOfContext<shared<Statement>>((int) NodeFlags::Ambient, [this, &pos, &hasJSDoc, &decorators, &modifiers]() { return parseDeclarationWorker(pos, hasJSDoc, decorators, modifiers); });
Expand Down
14 changes: 7 additions & 7 deletions src/scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using namespace ts;
using namespace std;

namespace ts {
bool isShebangTrivia(string &text, int pos) {
bool isShebangTrivia(const string &text, int pos) {
// Shebangs check must only be done at the start of the file
// Debug.assert(pos == 0);
// return shebangTriviaRegex.test(text);
Expand Down Expand Up @@ -39,7 +39,7 @@ namespace ts {
string Scanner::scanString(bool jsxAttributeString) {
auto quote = charCodeAt(text, pos);
pos++;
string result = "";
string result;
auto start = pos;
while (true) {
if (pos >= end) {
Expand Down Expand Up @@ -394,7 +394,7 @@ namespace ts {
return result + substring(text, start, pos);
}

map<string, SyntaxKind> textToKeyword{
static map<string, SyntaxKind> textToKeyword{
{"abstract", SyntaxKind::AbstractKeyword},
{"any", SyntaxKind::AnyKeyword},
{"as", SyntaxKind::AsKeyword},
Expand Down Expand Up @@ -491,7 +491,7 @@ namespace ts {
// a <<<<<<< or >>>>>>> marker then it is also followed by a space.
const unsigned long mergeConflictMarkerLength = size("<<<<<<<") - 1;

bool isConflictMarkerTrivia(const string text, int pos) {
bool isConflictMarkerTrivia(const string &text, int pos) {
assert(pos >= 0);

// Conflict markers must be at the start of a line.
Expand All @@ -512,7 +512,7 @@ namespace ts {
return false;
}

int Scanner::error(DiagnosticMessage message, int errPos, int length) {
int Scanner::error(const DiagnosticMessage &message, int errPos, int length) {
if (errPos == -1) errPos = pos;

cout << "Error: " << message.code << ": " << message.message << " at " << errPos << "\n";
Expand Down Expand Up @@ -741,7 +741,7 @@ namespace ts {
auto start = pos;
pos += 3;
auto escapedValueString = scanMinimumNumberOfHexDigits(1, /*canHaveSeparators*/ false);
auto escapedValue = escapedValueString.size() ? stoi(escapedValueString, nullptr, 16) : -1;
auto escapedValue = !escapedValueString.empty() ? stoi(escapedValueString, nullptr, 16) : -1;
pos = start;
return {escapedValue, 1};
}
Expand Down Expand Up @@ -1390,7 +1390,7 @@ namespace ts {
}
// Try to parse as an octal
if (pos + 1 < end && isOctalDigit(charCodeAt(text, pos + 1))) {
tokenValue = "" + scanOctalDigits();
tokenValue = to_string(scanOctalDigits());
tokenFlags |= TokenFlags::Octal;
return token = SyntaxKind::NumericLiteral;
}
Expand Down
2 changes: 1 addition & 1 deletion src/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ namespace ts {

bool isOctalDigit(const CharCode &code);

int error(DiagnosticMessage message, int errPos = -1, int length = -1);
int error(const DiagnosticMessage &message, int errPos = -1, int length = -1);

vector<CommentDirective> appendIfCommentDirective(vector<CommentDirective> &commentDirectives, const string &text, const regex &commentDirectiveRegEx, int lineStart);

Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_newtypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void visitNode(Node &node, const function<void(Node &)> &cbNode) {

void visitNodes(NodeArray &nodes, const function<void(Node &)> &cbNode) {
debug("visit nodes %d", (int) nodes.list.size());
for (auto node: nodes.list) {
for (auto &&node: nodes.list) {
cbNode(node);
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/tests/test_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ using namespace ts;

TEST(parser, bench) {
Parser parser;

string code = "const i = 123;";
/**
ConstKeyword
WhitespaceTrivia
Expand All @@ -21,13 +19,18 @@ SemicolonToken
EndOfFileToken
*/

string code;
for (int i = 0; i <20000; i++) {
code += string("const i").append(to_string(i)).append(" = 123;");
}

auto start = std::chrono::high_resolution_clock::now();

auto i = 0;
for (i = 0; i <10000; i++) {
// for (i = 0; i <10000; i++) {
auto result = parser.parseSourceFile("app.ts", code, ts::types::ScriptTarget::Latest, false, ScriptKind::TS, {});
// auto sourceFile = parser.createSourceFile("app.ts", ts::types::ScriptTarget::Latest, ScriptKind::TS, false, make_shared<NodeArray>(), make_shared<EndOfFileToken>(), 0, [](auto s) {});
}
// }
std::chrono::duration<double, std::milli> took = std::chrono::high_resolution_clock::now() - start;
debug("parse %d took %fms", i, took.count());
debug("parse %d bytes took %fms", code.size(), took.count());
}
2 changes: 1 addition & 1 deletion src/tests/test_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using namespace magic_enum;
TEST(scanner, basisc) {

auto start = std::chrono::high_resolution_clock::now();
Scanner scanner("const i = 123;");
Scanner scanner("const i = 123;const i2 = 123;const i3 = 123;const i4 = 123;const i5 = 123;const i6 = 123;const i7 = 123;const i8 = 123;");

auto i = 0;
for (i = 0; i <10000; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ namespace ts {
template<class T>
bool some(sharedOpt<NodeArray> array, function<bool(shared<T>)> callback) {
if (!array) return false;
for (auto &item: array->list) {
for (auto &&item: array->list) {
if (callback(reinterpret_pointer_cast<T>(item))) return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ namespace ts {
diagnostic.relatedInformation = vector<DiagnosticRelatedInformation>();
}
// Debug::assert(diagnostic.relatedInformation !== emptyArray, "Diagnostic had empty array singleton for related info, but is still being constructed!");
for (auto &v: relatedInformation) (*diagnostic.relatedInformation).push_back(v);
for (auto &&v: relatedInformation) (*diagnostic.relatedInformation).push_back(v);
return diagnostic;
}

Expand Down

0 comments on commit 071fa0d

Please sign in to comment.