Skip to content

Commit

Permalink
perf: ✨ use c++ inline to simpify code and improve speed
Browse files Browse the repository at this point in the history
- ADD FuncStatus & funcStatusTable
  • Loading branch information
asdawej committed Dec 4, 2023
1 parent 02a939e commit 0e21da9
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 32 deletions.
6 changes: 3 additions & 3 deletions include/AssistFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ constexpr int pow2(int x) {
}

// 左值二进制写入
void binaryIn(std::ostream &ostr, auto &data) { ostr.write((char *)&data, sizeof(data)); }
inline void binaryIn(std::ostream &ostr, auto &data) { ostr.write((char *)&data, sizeof(data)); }
// 右值二进制写入
void binaryIn(std::ostream &ostr, auto &&data) { ostr.write((char *)&data, sizeof(data)); }
inline void binaryIn(std::ostream &ostr, auto &&data) { ostr.write((char *)&data, sizeof(data)); }
// 二进制读出
template <typename _T> _T binaryOut(std::istream &istr) {
template <typename _T> inline _T binaryOut(std::istream &istr) {
_T data;
istr.read((char *)&data, sizeof(data));
return data;
Expand Down
8 changes: 4 additions & 4 deletions include/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct ThreadNode : public FunctionNode {
FunctionNode *thread; // 子线程代码树

Type_FunctionNode type() override;
void operator()() override; // # 暂时无法实现
void operator()() override;
void serialize(std::ostream &) override;
void deserialize(std::istream &) override;
};
Expand All @@ -61,7 +61,7 @@ struct AtomNode : public FunctionNode {
FunctionNode *atom; // 原子操作代码树

Type_FunctionNode type() override;
void operator()() override; // # 暂时无法实现
void operator()() override;
void serialize(std::ostream &) override;
void deserialize(std::istream &) override;
};
Expand Down Expand Up @@ -106,7 +106,7 @@ struct DefineNode : public FunctionNode {
// `6`结点, 条件转移
struct ConditionNode : public FunctionNode {
ConditionNode() : cond(0), func_T(nullptr), func_F(nullptr){};
~ConditionNode() { delete func_T, func_F; };
~ConditionNode() { delete func_T, delete func_F; };
tff::TryteExpr cond; // 条件
FunctionNode *func_T, *func_F; // 正值代码树func_T,负值代码树func_F

Expand All @@ -118,7 +118,7 @@ struct ConditionNode : public FunctionNode {

// `7`结点, 导通两个流
struct StreamIONode : public FunctionNode {
StreamIONode() : str_I(nullptr), str_O(nullptr), size(0){};
StreamIONode() : str_O(nullptr), str_I(nullptr), size(0){};
~StreamIONode();
Stream::Ostream *str_O; // 输出流
Stream::Istream *str_I; // 输入流
Expand Down
13 changes: 10 additions & 3 deletions include/tff.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ namespace tff {

constexpr int stackLength = 2 * BTS::maxValue + 1;

using FuncTree = Function::FunctionNode *;
using FuncTree = Function::FunctionNode *; // 代码树

// 内存栈
extern BTS::Tryte *stack;
inline BTS::Tryte *stack = &(BTS::Tryte[stackLength]){}[BTS::maxValue];

// 代码树表
extern FuncTree *funcTable;
inline FuncTree *funcTable = &(FuncTree[stackLength]){}[BTS::maxValue];

enum struct FuncStatus { normal, fork, lock };

// 代码树线程状态
inline FuncStatus *funcStatusTable = &(FuncStatus[stackLength]){}[BTS::maxValue];

}; // namespace tff

Expand Down
9 changes: 9 additions & 0 deletions src/base/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
#include "std/Register.h"
#include "tff.h"

#ifdef __MINGW32__ // https://github.com/meganz/mingw-std-threads
#include <mingw.mutex.h>
#include <mingw.thread.h>
#else
#include <mutex>
#include <thread>
#endif

namespace Function {

FunctionNode *factory_FunctionNode(Type_FunctionNode &&_tp) {
Expand Down Expand Up @@ -43,6 +51,7 @@ FunctionNode *factory_FunctionNode(Type_FunctionNode &&_tp) {
default:
break;
}
return nullptr;
}

FunctionNode::NodePair FunctionNode::findEndNode(FunctionNode *start) {
Expand Down
2 changes: 1 addition & 1 deletion src/base/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Stream {

// 辅助函数, 取表达式值
BTS::Tryte _getValue(tff::TryteExpr expr) {
inline BTS::Tryte _getValue(tff::TryteExpr expr) {
auto count = expr.suffix;
auto ret = expr.value;
while (count > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/base/TryteExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ char *TryteExpr::repr() {
char *s_value = value.repr();
std::copy(s_value, s_value + BTS::tryteTritNum, ret);
delete[] s_value;
for (auto i = BTS::tryteTritNum; i < BTS::tryteTritNum + suffix; i++)
for (size_t i = BTS::tryteTritNum; i < BTS::tryteTritNum + suffix; i++)
ret[i] = '2';
ret[BTS::tryteTritNum + suffix] = '\0';
return ret;
Expand Down
11 changes: 0 additions & 11 deletions src/base/tff.cpp

This file was deleted.

8 changes: 4 additions & 4 deletions src/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using StdFunc = Function::StdFunctionNode::Func;
auto factory = Function::factory_FunctionNode;

// 标准对象转义处理
void *token2ObjectStd(TokenPtr &tkCur, TokenPtr &vecEnd) {
inline void *token2ObjectStd(TokenPtr &tkCur, TokenPtr &vecEnd) {
if (*tkCur == "(")
tkCur++;
auto ret = Register::dict_ID2ObjectStd[Register::dict_Token2ID[*tkCur]];
Expand All @@ -19,7 +19,7 @@ void *token2ObjectStd(TokenPtr &tkCur, TokenPtr &vecEnd) {
}

// 小括号内部提取
string tokenInBracket(TokenPtr &tkCur, TokenPtr &vecEnd) {
inline string tokenInBracket(TokenPtr &tkCur, TokenPtr &vecEnd) {
if (*tkCur == "(")
tkCur++;
auto ret = *tkCur;
Expand All @@ -28,7 +28,7 @@ string tokenInBracket(TokenPtr &tkCur, TokenPtr &vecEnd) {
}

// 前置大括号处理
void startCreateNode(TokenPtr &treeStart, size_t &count, TokenPtr &vecEnd) {
inline void startCreateNode(TokenPtr &treeStart, size_t &count, TokenPtr &vecEnd) {
while (treeStart < vecEnd) {
if (*treeStart == "{") {
count++;
Expand All @@ -40,7 +40,7 @@ void startCreateNode(TokenPtr &treeStart, size_t &count, TokenPtr &vecEnd) {
}

// 后置大括号处理
void endCreateNode(TokenPtr &treeStart, size_t &count, TokenPtr &vecEnd) {
inline void endCreateNode(TokenPtr &treeStart, size_t &count, TokenPtr &vecEnd) {
while (treeStart < vecEnd) {
if (*treeStart == "}") {
count--;
Expand Down
1 change: 0 additions & 1 deletion src/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ void interpreter(const char *tffb_file) {
root->deserialize(compiled_code);
compiled_code.close();
(*root)();
system("pause");
}
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main(int argc, const char *argv[]) {
strcpy(tffb_file, argv[3]);
}
compiler(analyzer(tffl_file), tffb_file);
delete tffl_file, tffb_file;
delete tffl_file, delete tffb_file;
} else if (!strcasecmp("-r", argv[1])) {
auto len = strlen(argv[2]);
tffb_file = new char[len + 1];
Expand All @@ -54,7 +54,7 @@ int main(int argc, const char *argv[]) {
}
compiler(analyzer(tffl_file), tffb_file);
interpreter(tffb_file);
delete tffl_file, tffb_file;
delete tffl_file, delete tffb_file;
} else
goto L_ARGWRONG;
} else
Expand Down
4 changes: 2 additions & 2 deletions src/std/StdFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include "tff.h"
#include <chrono>

#ifdef __MINGW32__
#include <mingw.thread.h> // https://github.com/meganz/mingw-std-threads
#ifdef __MINGW32__ // https://github.com/meganz/mingw-std-threads
#include <mingw.thread.h>
#else
#include <thread>
#endif
Expand Down

0 comments on commit 0e21da9

Please sign in to comment.