Skip to content

Commit

Permalink
removed google Test, improved MemoryPool
Browse files Browse the repository at this point in the history
  • Loading branch information
marcj committed Jul 4, 2022
1 parent e530289 commit 38e7f7b
Show file tree
Hide file tree
Showing 9 changed files with 475 additions and 354 deletions.
365 changes: 153 additions & 212 deletions src/checker/MemoryPool.h

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/checker/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ namespace ts::checker {
unsigned int storageEnd = 0;
bool newSubRoutine = false;
DebugBinResult result;
if (print) fmt::print("Bin {} bytes: ", bin.size());
if (print) std::cout << fmt::format("Bin {} bytes: ", bin.size());

for (unsigned int i = 0; i < end; i++) {
if (storageEnd) {
while (i < storageEnd) {
auto size = vm::readUint16(bin, i + 8);
auto data = bin.substr(i + 8 + 2, size);
if (print) fmt::print("(Storage ({})\"{}\") ", size, data);
if (print) std::cout << fmt::format("(Storage ({})\"{}\") ", size, data);
result.storages.push_back(string(data));
i += 8 + 2 + size;
}
Expand All @@ -60,15 +60,15 @@ namespace ts::checker {
unsigned int j = 0;
for (auto &&r: result.subroutines) {
if (r.address == i) {
if (print) fmt::print("\n&{} {}(): ", j, r.name);
if (print) std::cout << fmt::format("\n&{} {}(): ", j, r.name);
result.activeSubroutine = &r;
found = true;
break;
}
j++;
}
if (!found) {
if (print) fmt::print("\nunknown!(): ");
if (print) std::cout << fmt::format("\nunknown!(): ");
}
newSubRoutine = false;
}
Expand Down Expand Up @@ -189,7 +189,7 @@ namespace ts::checker {
std::cout << "[" << startI << "](" << text << ") ";
}
}
if (print) fmt::print("\n");
if (print) std::cout << "\n";
return result;
}

Expand Down
9 changes: 5 additions & 4 deletions src/checker/vm2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ namespace ts::vm2 {
case OP::Assign: {
auto rvalue = pop();
auto lvalue = pop();
// debug("assign {} = {}", stringify(rvalue), stringify(lvalue));
debug("assign {} = {}", stringify(rvalue), stringify(lvalue));
if (!extends(lvalue, rvalue)) {
// auto error = stack.errorMessage();
// error.ip = ip;
Expand Down Expand Up @@ -915,14 +915,14 @@ namespace ts::vm2 {
} else if (T->kind == TypeKind::Tuple) {
//type T = [y, z];
//type New = [...T, x]; => [y, z, x];
// auto length = refLength((TypeRef *) T->type);
// debug("...T of size {} with {} users *{}", length, T->users, (void *) T);
auto length = refLength((TypeRef *) T->type);
debug("...T of size {} with refCount={} *{}", length, T->refCount, (void *) T);

//if type has no owner, we can just use it as the new type
//T.users is minimum 1, because the T is owned by Rest, and Rest owned by TupleMember, and TupleMember by nobody,
//if T comes from a type argument, it is 2 since argument belongs to the caller.
//thus an expression of [...T] yields always T.users >= 1.
if (T->refCount == 1 && firstType->flag & TypeFlag::RestReuse && !(firstType->flag & TypeFlag::Stored)) {
if (T->refCount == 2 && firstType->flag & TypeFlag::RestReuse && !(firstType->flag & TypeFlag::Stored)) {
item = use(T);
} else {
item = allocate(TypeKind::Tuple);
Expand All @@ -949,6 +949,7 @@ namespace ts::vm2 {
}
//drop Rest operator, since it was consumed now, so its resources are freed.
//the tuple member has users=0 in a [...T] operation, so it also GCs its REST type.
//decreases T->refCount
gc(firstTupleMember);
} else {
item = allocate(TypeKind::Tuple);
Expand Down
8 changes: 5 additions & 3 deletions src/checker/vm2.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace ts::vm2 {
// MemoryPool<TypePropertySignature, memoryDefault> propertySignature;
// };

constexpr auto poolSize = sizeof(Type) * 2048;
constexpr auto poolSize = 10000;
inline MemoryPool<Type, poolSize> pool;
inline MemoryPool<TypeRef, poolSize> poolRef;
void gcFlush();
Expand Down Expand Up @@ -167,8 +167,10 @@ namespace ts::vm2 {

static void run(shared<Module> module) {
// profiler.clear();
pool = MemoryPool<Type, poolSize>();
poolRef = MemoryPool<TypeRef, poolSize>();
// pool = MemoryPool<Type, poolSize>();
// poolRef = MemoryPool<TypeRef, poolSize>();
pool.clear();
poolRef.clear();

gcQueueIdx = 0;
gcQueueRefIdx = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ namespace ts {

inline void bench(string title, int iterations, const function<void()> &callback) {
auto took = benchRun(iterations, callback);
fmt::print("{} {} iterations took {:.9f}ms, {:.9f}ms per iteration\n", title, iterations, took.count(), took.count()/iterations);
std::cout << fmt::format("{} {} iterations took {:.9f}ms, {:.9f}ms per iteration\n", title, iterations, took.count(), took.count()/iterations);
}

inline void bench(int iterations, const function<void()> &callback) {
Expand Down
50 changes: 31 additions & 19 deletions src/tests/CmakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ cmake_minimum_required(VERSION 3.10)
project(Tests)


Include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.11.0
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
#set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

#include_directories(${})


#Include(FetchContent)
#FetchContent_Declare(
# googletest
# GIT_REPOSITORY https://github.com/google/googletest.git
# GIT_TAG release-1.11.0
#)
## For Windows: Prevent overriding the parent project's compiler/linker settings
##set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
#FetchContent_MakeAvailable(googletest)
#
##include_directories(${})


#project(Tests)
Expand Down Expand Up @@ -50,20 +48,34 @@ FetchContent_MakeAvailable(googletest)
##link_libraries(PRIVATE Catch2::Catch2WithMain)
##link_libraries(typescript)


Include(FetchContent)

FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.13.9 # or a later release
)

FetchContent_MakeAvailable(Catch2)

file(GLOB TESTS test*.cpp)

#add_executable(Tests_run test_core.cpp)
#target_link_libraries(Tests_run gtest gtest_main typescript)

foreach(file ${TESTS})
foreach (file ${TESTS})
get_filename_component(name ${file} NAME_WE)
MESSAGE("Test found typescript_${name}.")
MESSAGE("Catch2_SOURCE_DIR ${Catch2_SOURCE_DIR}")
add_executable(typescript_${name} ${file})

# target_link_libraries(typescript_${name} PUBLIC Tracy::TracyClient)
# target_link_libraries(typescript_${name} PUBLIC Tracy::TracyClient)

# target_link_libraries(typescript_${name} gtest_main)
target_link_libraries(typescript_${name} gtest gtest_main typescript)
# target_link_libraries(typescript_${name} PRIVATE Catch2::Catch2WithMain)
# target_link_libraries(typescript_${name} PRIVATE typescript)
endforeach()
target_include_directories(typescript_${name} PUBLIC ${Catch2_SOURCE_DIR}/single_include)
target_link_libraries(typescript_${name} PRIVATE Catch2::Catch2 typescript )
# target_link_libraries(typescript_${name} typescript)
# target_link_libraries(typescript_${name} PRIVATE Catch2::Catch2WithMain)
# target_link_libraries(typescript_${name} PRIVATE typescript)
endforeach ()
160 changes: 160 additions & 0 deletions src/tests/test_allocator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#define CATCH_CONFIG_MAIN

#include <catch2/catch.hpp>
#include <string>
#include "../checker/MemoryPool.h"
#include <vector>

struct Item {
std::string_view title;
unsigned int i;
std::vector<int> jopp;
};

TEST_CASE("allocator1") {
MemoryPool<Item, 2> pool;
REQUIRE(pool.blocks == 0);
REQUIRE(pool.active == 0);

auto p1 = pool.allocate();
REQUIRE(pool.active == 1);
REQUIRE(pool.blocks == 1);

auto p2 = pool.allocate();
REQUIRE(pool.blocks == 1);
REQUIRE(pool.active == 2);

pool.deallocate(p1);
REQUIRE(pool.blocks == 1);
REQUIRE(pool.active == 1);

pool.deallocate(p2);
REQUIRE(pool.blocks == 1);
REQUIRE(pool.active == 0);

auto p3 = pool.allocate();
REQUIRE(pool.active == 1);
REQUIRE(pool.blocks == 1);

auto p4 = pool.allocate();
REQUIRE(pool.blocks == 1);
REQUIRE(pool.active == 2);
}

TEST_CASE("allocator2") {
MemoryPool<Item, 2> pool;
auto p1 = pool.allocate();
auto p2 = pool.allocate();
REQUIRE(pool.active == 2);
REQUIRE(pool.blocks == 1);

auto p3 = pool.allocate();
REQUIRE(pool.active == 3);
REQUIRE(pool.blocks == 2);

auto p4 = pool.allocate();
REQUIRE(pool.active == 4);
REQUIRE(pool.blocks == 2);

auto p5 = pool.allocate();
REQUIRE(pool.active == 5);
REQUIRE(pool.blocks == 3);
}

TEST_CASE("allocator3") {
MemoryPool<Item, 2> pool;
{
auto p1 = pool.allocate();
auto p2 = pool.allocate();
REQUIRE(pool.active == 2);
REQUIRE(pool.blocks == 1);

auto p3 = pool.allocate();
REQUIRE(pool.active == 3);
REQUIRE(pool.blocks == 2);

auto p4 = pool.allocate();
REQUIRE(pool.active == 4);
REQUIRE(pool.blocks == 2);
}

{
pool.clear();
auto p1 = pool.allocate();
REQUIRE(pool.active == 1);
REQUIRE(pool.blocks == 2);

auto p2 = pool.allocate();
REQUIRE(pool.active == 2);
REQUIRE(pool.blocks == 2);

//now block 2 should be reused
auto p3 = pool.allocate();
REQUIRE(pool.active == 3);
REQUIRE(pool.blocks == 2);

//now block 2 should be reused
auto p4 = pool.allocate();
REQUIRE(pool.active == 4);
REQUIRE(pool.blocks == 2);

//now block 3 should be created
auto p5 = pool.allocate();
REQUIRE(pool.active == 5);
REQUIRE(pool.blocks == 3);
}
}

TEST_CASE("allocator4") {
MemoryPool<Item, 2> pool;
pool.clear();

auto p1 = pool.newElement();
REQUIRE(p1->i == 0);
REQUIRE(p1->title == "");

auto p2 = pool.newElement();
REQUIRE(p2->i == 0);
REQUIRE(p2->title == "");
p2->i = 2;
REQUIRE(p2->i == 2);

pool.deleteElement(p2);

auto p3 = pool.newElement();
REQUIRE(p3 == p2);
REQUIRE(p3->i == 0);
REQUIRE(p3->title == "");
}

TEST_CASE("allocator5") {
MemoryPool<Item, 2> pool;
pool.clear();

auto p1 = pool.newElement();
p1->i = 1;
auto p2 = pool.newElement();
p2->i = 2;

auto p3 = pool.newElement();
p3->i = 3;
auto p4 = pool.newElement();
p4->i = 4;

auto p5 = pool.newElement();
p5->i = 5;
auto p6_ = pool.newElement();
pool.deleteElement(p6_);

auto p6 = pool.newElement();
p6->i = 6;
REQUIRE(pool.active == 6);
REQUIRE(pool.blocks == 3);

REQUIRE(p1->i == 1);
REQUIRE(p2->i == 2);
REQUIRE(p3->i == 3);
REQUIRE(p4->i == 4);
REQUIRE(p5->i == 5);
REQUIRE(p6->i == 6);
}
Loading

0 comments on commit 38e7f7b

Please sign in to comment.