From 071fa0dcb5d3fe05a490bac7fc82b4dd3ba6143f Mon Sep 17 00:00:00 2001 From: "Marc J. Schmidt" Date: Wed, 1 Jun 2022 01:14:26 +0200 Subject: [PATCH] more const --- src/CMakeLists.txt | 5 +++-- src/core.cpp | 2 +- src/core.h | 4 ++-- src/factory.cpp | 6 ++++-- src/factory.h | 2 +- src/node_test.cpp | 2 +- src/parser2.h | 30 ++++++++++++++++++------------ src/scanner.cpp | 14 +++++++------- src/scanner.h | 2 +- src/tests/test_newtypes.cpp | 2 +- src/tests/test_parser.cpp | 13 ++++++++----- src/tests/test_scanner.cpp | 2 +- src/types.h | 2 +- src/utilities.cpp | 2 +- 14 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a04ce9a..fd4bd6b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/core.cpp b/src/core.cpp index 319a6a4..8a6c66e 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -52,7 +52,7 @@ namespace ts { } vector charToStringVector(vector chars) { vector 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) { diff --git a/src/core.h b/src/core.h index 1480456..00d8506 100644 --- a/src/core.h +++ b/src/core.h @@ -148,7 +148,7 @@ namespace ts { //inline bool some(optional> array) { if (array) { if (predicate) { - for (auto &v: (*array)) { + for (auto &&v: (*array)) { if ((*predicate)(v)) { return true; } @@ -211,7 +211,7 @@ namespace ts { const unordered_map &map1 ) { unordered_map res; - for (auto &i: map1) { + for (auto &&i: map1) { res[i.second] = i.first; } return res; diff --git a/src/factory.cpp b/src/factory.cpp index a207c12..2a3b051 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -24,15 +24,16 @@ namespace ts { void Factory::aggregateChildrenFlags(shared &children) { int subtreeFlags = (int) TransformFlags::None; - for (auto &child: children->list) { + for (auto &&child: children->list) { subtreeFlags |= propagateChildFlags(child); } children->transformFlags = subtreeFlags; } shared Factory::createNodeArray(sharedOpt elements, optional 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); @@ -247,6 +248,7 @@ namespace ts { // @api shared Factory::createIdentifier(string text, sharedOpt typeArguments, optional 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 diff --git a/src/factory.h b/src/factory.h index 3c07a92..660d292 100644 --- a/src/factory.h +++ b/src/factory.h @@ -52,7 +52,7 @@ namespace ts { template inline shared asNodeArray(const vector> &array) { auto nodeArray = make_shared(); - for (auto node: array) nodeArray->list.push_back(node); + for (auto &&node: array) nodeArray->list.push_back(node); return nodeArray; } diff --git a/src/node_test.cpp b/src/node_test.cpp index 142db80..88453f4 100644 --- a/src/node_test.cpp +++ b/src/node_test.cpp @@ -60,7 +60,7 @@ namespace ts { int modifiersToFlags(shared modifiers) { int flags = (int) ModifierFlags::None; if (modifiers) { - for (auto &modifier: modifiers->list) { + for (auto &&modifier: modifiers->list) { flags |= (int) modifierToFlag(modifier->kind); } } diff --git a/src/parser2.h b/src/parser2.h index fa5bd0d..ce0b8f8 100644 --- a/src/parser2.h +++ b/src/parser2.h @@ -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; @@ -175,7 +175,7 @@ namespace ts { inline bool some(const sharedOpt &array, const function value)> &predicate) { if (array) { - for (auto &v: array->list) { + for (auto &&v: array->list) { if (predicate(v)) { return true; } @@ -1705,14 +1705,16 @@ namespace ts { return to(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() { @@ -1725,6 +1727,7 @@ namespace ts { } shared parseBindingIdentifier(optional privateIdentifierDiagnosticMessage = {}) { + ZoneScoped; return createIdentifier(isBindingIdentifier(), /*diagnosticMessage*/ {}, privateIdentifierDiagnosticMessage); } @@ -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 createIdentifier(bool isIdentifier, optional diagnosticMessage = {}, optional privateIdentifierDiagnosticMessage = {}) { + ZoneScoped; if (isIdentifier) { identifierCount++; auto pos = getNodePos(); @@ -3838,6 +3842,7 @@ namespace ts { } shared parseArrayBindingPattern() { + ZoneScoped; auto pos = getNodePos(); parseExpected(SyntaxKind::OpenBracketToken); auto elements = parseDelimitedList(ParsingContext::ArrayBindingElements, CALLBACK(parseArrayBindingElement)); @@ -3950,6 +3955,7 @@ namespace ts { } shared parseObjectBindingPattern() { + ZoneScoped; auto pos = getNodePos(); parseExpected(SyntaxKind::OpenBraceToken); auto elements = parseDelimitedList(ParsingContext::ObjectBindingElements, CALLBACK(parseObjectBindingElement)); @@ -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>((int) NodeFlags::Ambient, [&]() { @@ -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>((int) NodeFlags::Ambient, [this, &pos, &hasJSDoc, &decorators, &modifiers]() { return parseDeclarationWorker(pos, hasJSDoc, decorators, modifiers); }); diff --git a/src/scanner.cpp b/src/scanner.cpp index 6b992e1..bcc1099 100644 --- a/src/scanner.cpp +++ b/src/scanner.cpp @@ -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); @@ -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) { @@ -394,7 +394,7 @@ namespace ts { return result + substring(text, start, pos); } - map textToKeyword{ + static map textToKeyword{ {"abstract", SyntaxKind::AbstractKeyword}, {"any", SyntaxKind::AnyKeyword}, {"as", SyntaxKind::AsKeyword}, @@ -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. @@ -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"; @@ -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}; } @@ -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; } diff --git a/src/scanner.h b/src/scanner.h index ac28d56..335876d 100644 --- a/src/scanner.h +++ b/src/scanner.h @@ -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 appendIfCommentDirective(vector &commentDirectives, const string &text, const regex &commentDirectiveRegEx, int lineStart); diff --git a/src/tests/test_newtypes.cpp b/src/tests/test_newtypes.cpp index b524c43..b229270 100644 --- a/src/tests/test_newtypes.cpp +++ b/src/tests/test_newtypes.cpp @@ -201,7 +201,7 @@ void visitNode(Node &node, const function &cbNode) { void visitNodes(NodeArray &nodes, const function &cbNode) { debug("visit nodes %d", (int) nodes.list.size()); - for (auto node: nodes.list) { + for (auto &&node: nodes.list) { cbNode(node); } } diff --git a/src/tests/test_parser.cpp b/src/tests/test_parser.cpp index 9c4c9ce..31bfd16 100644 --- a/src/tests/test_parser.cpp +++ b/src/tests/test_parser.cpp @@ -7,8 +7,6 @@ using namespace ts; TEST(parser, bench) { Parser parser; - - string code = "const i = 123;"; /** ConstKeyword WhitespaceTrivia @@ -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(), make_shared(), 0, [](auto s) {}); - } +// } std::chrono::duration 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()); } \ No newline at end of file diff --git a/src/tests/test_scanner.cpp b/src/tests/test_scanner.cpp index 297f10c..bee5951 100644 --- a/src/tests/test_scanner.cpp +++ b/src/tests/test_scanner.cpp @@ -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++) { diff --git a/src/types.h b/src/types.h index 8bc56c3..9186d64 100644 --- a/src/types.h +++ b/src/types.h @@ -1211,7 +1211,7 @@ namespace ts { template bool some(sharedOpt array, function)> callback) { if (!array) return false; - for (auto &item: array->list) { + for (auto &&item: array->list) { if (callback(reinterpret_pointer_cast(item))) return true; } return false; diff --git a/src/utilities.cpp b/src/utilities.cpp index 5f54441..45e7d12 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -315,7 +315,7 @@ namespace ts { diagnostic.relatedInformation = vector(); } // 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; }