Skip to content

Commit

Permalink
perf improvements, use fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Jun 1, 2022
1 parent 071fa0d commit ec1cf8d
Show file tree
Hide file tree
Showing 22 changed files with 483 additions and 309 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "libs/tracy"]
path = libs/tracy
url = [email protected]:wolfpld/tracy.git
[submodule "libs/fmt"]
path = libs/fmt
url = [email protected]:fmtlib/fmt.git
13 changes: 8 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
cmake_minimum_required(VERSION 3.22)
project(typescript)

set(CMAKE_CXX_STANDARD 20)

# enable for profiling
#add_definitions(-DTRACY_ENABLE)
set(CMAKE_CXX_STANDARD 23)

include_directories(libs/tracy/)
include_directories(libs/fmt/include/)
include_directories(libs)

list(APPEND CMAKE_MODULE_PATH libs/)

#add_definitions(-DTRACY_ENABLE)
#include_directories(libs/tracy/)

add_subdirectory(libs/tracy)
add_subdirectory(libs/fmt)

# enable for profiling
#add_definitions(-DTRACY_ENABLE)
#link_libraries(Tracy::TracyClient)

link_libraries(Tracy::TracyClient)
add_subdirectory(src)

include_directories(libs)
Expand Down
1 change: 1 addition & 0 deletions libs/fmt
Submodule fmt added at 9d6039
8 changes: 5 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
cmake_minimum_required(VERSION 3.22)
project(typescript)

add_definitions(-DTRACY_ENABLE)
#add_definitions(-DTRACY_ENABLE)

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

add_subdirectory(tests)


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
factory.h factory.cpp parenthesizer.h parenthesizer.cpp scanner.h scanner.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../libs/tracy/TracyClient.cpp
)
# ${CMAKE_CURRENT_SOURCE_DIR}/../libs/tracy/TracyClient.cpp

target_link_libraries(typescript fmt)
21 changes: 0 additions & 21 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,4 @@ namespace ts {
while (s.size() > 0 && s.compare(s.size() - 1, 1, " ") == 0) s.erase(s.end() - 1);
return s;
}
std::string format(const std::string &fmt, ...) {
int size = ((int) fmt.size()) * 2 + 50; // Use a rubric appropriate for your code
std::string str;
va_list ap;
while (1) { // Maximum two passes on a POSIX system...
str.resize(size);
va_start(ap, fmt);
int n = vsnprintf((char *) str.data(), size, fmt.c_str(), ap);
va_end(ap);
if (n > -1 && n < size) { // Everything worked
str.resize(n);
return str;
}
if (n > -1) // Needed size returned
size = n + 1; // For null char
else
size *= 2; // Guess at a larger size (OS specific)
}
return str;
}

}
12 changes: 6 additions & 6 deletions src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <optional>
#include <sstream>
#include <iostream>
#include <fmt/core.h>

#define CALLBACK(name) [this](auto ...a) { return name(a...); }

Expand Down Expand Up @@ -36,7 +37,7 @@ namespace ts {
return const_hash(input.c_str());
}

inline constexpr unsigned operator ""_hash(const char *s, size_t) {
inline consteval unsigned operator ""_hash(const char *s, size_t) {
return const_hash(s);
}

Expand Down Expand Up @@ -242,10 +243,9 @@ namespace ts {
return map.find(key) != map.end();
};

std::string format(const std::string &fmt, ...);

template<typename...Args>
inline void debug(const std::string &fmt, Args &&...args) {
std::cout << format(fmt, args...) << "\n";
template<typename T, typename...Args>
inline void debug(T fmt, Args &&...args) {
// std::cout << fmt::format(fmt, std::forward<Args>(args)...) << "\n";
std::cout << fmt::format(fmt, args...) << "\n";
}
}
5 changes: 3 additions & 2 deletions src/diagnostic_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#include "types.h"
#include <string>
#include <memory>
#include <type_traits>

namespace ts {
using types::DiagnosticMessage;
using types::DiagnosticCategory;

static DiagnosticMessage diag(int code, DiagnosticCategory category, const std::string &key, const std::string &message, bool reportsUnnecessary = false, bool elidedInCompatabilityPyramid = false, bool reportsDeprecated = false) {
return DiagnosticMessage{code, category, key, message, reportsUnnecessary, elidedInCompatabilityPyramid, reportsDeprecated};
inline shared<DiagnosticMessage> diag(int code, DiagnosticCategory category, const std::string_view &key, const std::string_view &message, bool reportsUnnecessary = false, bool elidedInCompatabilityPyramid = false, bool reportsDeprecated = false) {
return std::make_shared<DiagnosticMessage>(code, category, key, message, reportsUnnecessary, elidedInCompatabilityPyramid, reportsDeprecated);
}

class Diagnostics {
Expand Down
75 changes: 42 additions & 33 deletions src/factory.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "factory.h"
#include <fmt/core.h>

namespace ts {
int Factory::propagatePropertyNameFlagsOfChild(shared<ts::Node> &node, int transformFlags) {
Expand All @@ -18,42 +19,41 @@ namespace ts {
return propagateChildFlags(std::move(node)) & ~(int) TransformFlags::ContainsPossibleTopLevelAwait;
}

int Factory::propagateChildrenFlags(sharedOpt<NodeArray> children) {
int Factory::propagateChildrenFlags(const sharedOpt<NodeArray> &children) {
return children ? children->transformFlags : (int) TransformFlags::None;
}

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

shared<NodeArray> Factory::createNodeArray(sharedOpt<NodeArray> elements, optional<bool> hasTrailingComma) {
shared<NodeArray> Factory::createNodeArray(const sharedOpt<NodeArray> &elements, bool hasTrailingComma) {
ZoneScoped;
if (elements) {
if (!hasTrailingComma || elements->hasTrailingComma == *hasTrailingComma) {
// Ensure the transform flags have been aggregated for this NodeArray
if (elements->transformFlags == (int) types::TransformFlags::None) {
aggregateChildrenFlags(elements);
}
// Debug.attachNodeArrayDebugInfo(elements);
return elements;
if (elements && elements->hasTrailingComma == hasTrailingComma) {
// Ensure the transform flags have been aggregated for this NodeArray
if (elements->transformFlags == (int) types::TransformFlags::None) {
aggregateChildrenFlags(elements);
}
// Debug.attachNodeArrayDebugInfo(elements);
return elements;
}

elements->hasTrailingComma = hasTrailingComma;
return elements;
// This *was* a `NodeArray`, but the `hasTrailingComma` option differs. Recreate the
// array with the same elements, text range, and transform flags but with the updated
// value for `hasTrailingComma`
auto array = make_shared<NodeArray>();
if (elements) array = elements;
array->pos = elements->pos;
array->end = elements->end;
array->hasTrailingComma = *hasTrailingComma;
array->transformFlags = elements->transformFlags;
// Debug.attachNodeArrayDebugInfo(array);
return array;
// auto array = make_shared<NodeArray>();
// if (elements) array = elements;
// array->pos = elements->pos;
// array->end = elements->end;
// array->hasTrailingComma = *hasTrailingComma;
// array->transformFlags = elements->transformFlags;
//// Debug.attachNodeArrayDebugInfo(array);
// return array;
}

sharedOpt<NodeArray> Factory::asNodeArray(sharedOpt<NodeArray> elements) {
Expand Down Expand Up @@ -2498,7 +2498,7 @@ namespace ts {
node->transformFlags |= (int) TransformFlags::ContainsESNext;
break;
default:
throw runtime_error(format("invalid keyword token %d", keywordToken));
throw runtime_error(fmt::format("invalid keyword token {}", keywordToken));
}
return node;
}
Expand Down Expand Up @@ -2555,7 +2555,7 @@ namespace ts {
}

// @api
shared<VariableDeclarationList> Factory::createVariableDeclarationList(shared<NodeArray> declarations, int flags) {
shared<VariableDeclarationList> Factory::createVariableDeclarationList(const shared<NodeArray> &declarations, int flags) {
auto node = createBaseNode<VariableDeclarationList>(SyntaxKind::VariableDeclarationList);
node->flags |= flags & (int) NodeFlags::BlockScoped;
node->declarations = declarations;
Expand All @@ -2570,10 +2570,6 @@ namespace ts {
return node;
}

shared<VariableDeclarationList> Factory::createVariableDeclarationList(vector<shared<VariableDeclaration>> declarations, int flags) {
return createVariableDeclarationList(createNodeArray<VariableDeclaration>(declarations), flags);
}

// // @api
// function updateBlock(node: Block, statements: readonly Statement[]) {
// return node->statements != statements
Expand All @@ -2582,14 +2578,9 @@ namespace ts {
// }

// @api
shared<VariableStatement> Factory::createVariableStatement(sharedOpt<NodeArray> modifiers, variant<shared<VariableDeclarationList>, vector<shared<VariableDeclaration>>> declarationList) {
shared<VariableStatement> Factory::createVariableStatement(sharedOpt<NodeArray> modifiers, shared<VariableDeclarationList> declarationList) {
auto node = createBaseDeclaration<VariableStatement>(SyntaxKind::VariableStatement, /*decorators*/ nullptr, modifiers);

if (holds_alternative<shared<VariableDeclarationList>>(declarationList)) {
node->declarationList = get<shared<VariableDeclarationList>>(declarationList);
} else {
node->declarationList = createVariableDeclarationList(get<vector<shared<VariableDeclaration>>>(declarationList));
}
node->declarationList = declarationList;
node->transformFlags |=
propagateChildFlags(node->declarationList);
if (modifiersToFlags(node->modifiers) & (int) ModifierFlags::Ambient) {
Expand All @@ -2598,6 +2589,23 @@ namespace ts {
return node;
}

// // @api
// shared<VariableStatement> Factory::createVariableStatement(sharedOpt<NodeArray> modifiers, variant<shared<VariableDeclarationList>, vector<shared<VariableDeclaration>>> declarationList) {
// auto node = createBaseDeclaration<VariableStatement>(SyntaxKind::VariableStatement, /*decorators*/ nullptr, modifiers);
//
// if (holds_alternative<shared<VariableDeclarationList>>(declarationList)) {
// node->declarationList = get<shared<VariableDeclarationList>>(declarationList);
// } else {
// node->declarationList = createVariableDeclarationList(get<vector<shared<VariableDeclaration>>>(declarationList));
// }
// node->transformFlags |=
// propagateChildFlags(node->declarationList);
// if (modifiersToFlags(node->modifiers) & (int) ModifierFlags::Ambient) {
// node->transformFlags = (int) TransformFlags::ContainsTypeScript;
// }
// return node;
// }

// // @api
// function updateVariableStatement(node: VariableStatement, sharedOpt<NodeArray> modifiers, declarationList: VariableDeclarationList) {
// return node->modifiers != modifiers
Expand Down Expand Up @@ -2933,6 +2941,7 @@ namespace ts {
//
// @api
shared<VariableDeclaration> Factory::createVariableDeclaration(NameType name, sharedOpt<ExclamationToken> exclamationToken, sharedOpt<TypeNode> type, sharedOpt<Expression> initializer) {
ZoneScoped;
auto node = createBaseVariableLikeDeclaration<VariableDeclaration>(
SyntaxKind::VariableDeclaration,
/*decorators*/ nullptr,
Expand Down
29 changes: 15 additions & 14 deletions src/factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ namespace ts {

int propagateIdentifierNameFlags(shared<Node> node);

int propagateChildrenFlags(sharedOpt<NodeArray> children);
int propagateChildrenFlags(const sharedOpt<NodeArray> &children);

void aggregateChildrenFlags(shared<NodeArray> &children);
void aggregateChildrenFlags(const shared<NodeArray> &children);

shared<NodeArray> createNodeArray(sharedOpt<NodeArray> elements, optional<bool> hasTrailingComma = {});
shared<NodeArray> createNodeArray(const sharedOpt<NodeArray> &elements, bool hasTrailingComma = false);

sharedOpt<NodeArray> asNodeArray(sharedOpt<NodeArray> elements);

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);
return nodeArray;
}
// 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);
// return nodeArray;
// }

// template<class T>
// sharedOpt<NodeArray> asNodeArray(optional<vector<shared<T>>> &array) {
Expand All @@ -65,7 +65,7 @@ namespace ts {

// @api
template<class T>
shared<NodeArray> createNodeArray(const vector<shared<T>> &elements, optional<bool> hasTrailingComma = {}) {
shared<NodeArray> createNodeArray(const vector<shared<T>> &elements, bool hasTrailingComma = false) {
// Since the element list of a node array is typically created by starting with an empty array and
// repeatedly calling push(), the list may not have the optimal memory layout. We invoke slice() for
// small arrays (1 to 4 elements) to give the VM a chance to allocate an optimal representation.
Expand Down Expand Up @@ -128,6 +128,7 @@ namespace ts {
// function createToken<TKind extends SyntaxKind>(token: TKind): Token<TKind>;
template<class T>
shared<T> createToken(SyntaxKind token) {
ZoneScoped
// Debug::asserts(token >= SyntaxKind::FirstToken && token <= SyntaxKind::LastToken, "Invalid token");
// Debug::asserts(token <= SyntaxKind::FirstTemplateToken || token >= SyntaxKind::LastTemplateToken, "Invalid token. Use 'createTemplateLiteralLikeNode' to create template literals.");
// Debug::asserts(token <= SyntaxKind::FirstLiteralToken || token >= SyntaxKind::LastLiteralToken, "Invalid token. Use 'createLiteralLikeNode' to create literals.");
Expand Down Expand Up @@ -1873,9 +1874,7 @@ namespace ts {
shared<Block> createBlock(shared<NodeArray> statements, bool multiLine);

// @api
shared<VariableDeclarationList> createVariableDeclarationList(shared<NodeArray> declarations, int flags = (int) NodeFlags::None);

shared<VariableDeclarationList> createVariableDeclarationList(vector<shared<VariableDeclaration>> declarations, int flags = (int) NodeFlags::None);
shared<VariableDeclarationList> createVariableDeclarationList(const shared<NodeArray> &declarations, int flags = (int) NodeFlags::None);

// // @api
// function updateBlock(node: Block, statements: readonly Statement[]) {
Expand All @@ -1885,7 +1884,9 @@ namespace ts {
// }

// @api
shared<VariableStatement> createVariableStatement(sharedOpt<NodeArray> modifiers, variant<shared<VariableDeclarationList>, vector<shared<VariableDeclaration>>> declarationList);
shared<VariableStatement> createVariableStatement(sharedOpt<NodeArray> modifiers, shared<VariableDeclarationList> declarationList);

// shared<VariableStatement> createVariableStatement(sharedOpt<NodeArray> modifiers, variant<shared<VariableDeclarationList>, vector<shared<VariableDeclaration>>> declarationList);

// // @api
// function updateVariableStatement(node: VariableStatement, sharedOpt<NodeArray> modifiers, declarationList: VariableDeclarationList) {
Expand Down
9 changes: 5 additions & 4 deletions src/node_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "node_test.h"
#include <fmt/core.h>

namespace ts {
/**
Expand Down Expand Up @@ -127,7 +128,7 @@ namespace ts {
return reinterpret_pointer_cast<PrivateIdentifier>(node);
}

throw runtime_error(format("resolveNamToNode with kind %d no valid name property", (int) node->kind));
throw runtime_error(fmt::format("resolveNamToNode with kind {} no valid name property", (int) node->kind));
}

bool isFunctionOrConstructorTypeNode(shared<Node> node) {
Expand Down Expand Up @@ -168,7 +169,7 @@ namespace ts {
case SyntaxKind::IndexSignature:
return node->to<IndexSignatureDeclaration>().typeParameters;
default:
throw runtime_error(format("node %d has no typeParameters", node->kind));
throw runtime_error(fmt::format("node {} has no typeParameters", node->kind));
}
}

Expand All @@ -179,7 +180,7 @@ namespace ts {
case SyntaxKind::JsxOpeningElement:
return node->to<JsxOpeningElement>().tagName;
default:
throw runtime_error(format("node %d has no tagName", node->kind));
throw runtime_error(fmt::format("node {} has no tagName", node->kind));
}
}

Expand All @@ -191,7 +192,7 @@ namespace ts {
return reinterpret_pointer_cast<PrivateIdentifier>(node)->escapedText;
}

throw runtime_error(format("getEscapedName with kind %d no valid", (int) node->kind));
throw runtime_error(fmt::format("getEscapedName with kind {} no valid", (int) node->kind));
}

sharedOpt<NodeUnion(PropertyName)> getName(const shared<Node> &node) {
Expand Down
Loading

0 comments on commit ec1cf8d

Please sign in to comment.