From 5df86511104154f621bde073b9cc04810d7ae947 Mon Sep 17 00:00:00 2001 From: GillesDuvert Date: Thu, 14 Dec 2023 00:10:24 +0100 Subject: [PATCH] much smaller size of SCC in memory and saved file due to removal of unrelated Semicompiled Code nodes. --- src/dcompiler.cpp | 4 ++-- src/dinterpreter.cpp | 9 +++++++- src/dpro.cpp | 34 +++++++++++++++++------------- src/dpro.hpp | 10 ++------- src/gdlhelp.cpp | 8 +++---- src/saverestore.cpp | 50 +++++++++++++++++++++++--------------------- 6 files changed, 61 insertions(+), 54 deletions(-) diff --git a/src/dcompiler.cpp b/src/dcompiler.cpp index c0c5562f0..9ce2f06a8 100644 --- a/src/dcompiler.cpp +++ b/src/dcompiler.cpp @@ -154,7 +154,7 @@ void DCompiler::StartPro(const string& n, const int compileOpt, const string& o, pro->SetCompileOpt(compileOpt); //"restored" procedures have no file name associated. They will be marked as "CompileHidden", to avoid the message "Compiled module" issued when (re)compiling the Lexer code. if (actualFile.length()==0) pro->AddHiddenToCompileOpt(); - pro->SetAstTree(semiCompiledTree); //entry number + pro->SetSCC(semiCompiledTree); //entry number } else { @@ -177,7 +177,7 @@ void DCompiler::StartFun(const string& n, const int compileOpt = 0, const string pro->SetCompileOpt(compileOpt); //"restored" procedures have no file name associated. They will be marked as "CompileHidden", to avoid the message "Compiled module" issued when (re)compiling the Lexer code. if (actualFile.length() == 0) pro->AddHiddenToCompileOpt(); - pro->SetAstTree(semiCompiledTree); //entry number + pro->SetSCC(semiCompiledTree); //entry number } bool DCompiler::IsActivePro( DSub* p) diff --git a/src/dinterpreter.cpp b/src/dinterpreter.cpp index 5836456d3..0adc75836 100644 --- a/src/dinterpreter.cpp +++ b/src/dinterpreter.cpp @@ -666,7 +666,7 @@ bool GDLInterpreter::CompileFile(const string& f, const string& untilPro, bool s #ifdef GDL_DEBUG cout << "Parser output:" << endl; antlr::print_tree pt; - pt.pr_tree(static_cast(theSemiCompiledAST)); + pt.pr_tree(static_cast(theAST)); cout << "CompileFile: Parser end." << endl; #endif @@ -714,6 +714,13 @@ bool GDLInterpreter::CompileFile(const string& f, const string& untilPro, bool s bool GDLInterpreter::CompileSaveFile(RefDNode theSemiCompiledAST) { +#ifdef GDL_DEBUG + cout << "Parser output:" << endl; + antlr::print_tree pt; + pt.pr_tree(static_cast(theSemiCompiledAST)); + cout << "CompileFile: Parser end." << endl; +#endif + #ifdef GDL_DEBUG RefDNode trAST; #endif diff --git a/src/dpro.cpp b/src/dpro.cpp index 20beff8c6..a65e8bdcc 100644 --- a/src/dpro.cpp +++ b/src/dpro.cpp @@ -35,7 +35,6 @@ #include "print_tree.hpp" #endif -CodeListT codeList; SCCodeListT sccList; using namespace std; @@ -70,9 +69,11 @@ using namespace std; savenode.ligne = dNode->getLine(); savenode.flags=dNode->GetCompileOpt(); // trick to avoid saving a Text and a CData containing the same text, either as a string or a value converted to the good BaseGDL type. - // if dNode->CData() is non-nil, set savenode.Text to "" - savenode.var=dNode->CData(); - if (savenode.var == NULL) savenode.Text = dNode->getText(); else savenode.Text = ""; + // if dNode->CData() is non-nil, set savenode.Text to "" and save CData as copy. + if (dNode->CData() != NULL) { + savenode.var=dNode->CData()->Convert2(dNode->CData()->Type(),BaseGDL::COPY); + savenode.Text = ""; + } else savenode.Text = dNode->getText(); nodes.push_back(savenode); } @@ -101,9 +102,11 @@ using namespace std; process_leaves(top,nodes,addrList); } } - + //same as index_tree: no not use void process_tree(RefDNode top, SCCStructV &nodes, SCCodeAddresses &addrList) { - RefDNode t; + std::cerr<<"use of process_tree, please report."<getNextSibling()) { process_top(t,nodes,addrList); } @@ -115,7 +118,7 @@ void indexNodeAddress(RefDNode node, SCCodeAddresses &addrList, int &i) { // associate node address to an increasing number starting at 1 DNode* ast = node.get(); addrList.insert(std::pair(ast, ++i)); //+1 as address 0 is special for all nodes -} + } void index_leaves(RefDNode top, SCCodeAddresses &addrList, int &i) { RefDNode t; @@ -140,7 +143,10 @@ void index_top(RefDNode top, SCCodeAddresses &addrList, int &i) { } } +//this would be to index a full tree, but our use at the time is of index_top, that serialize only the current PRO or FUN . void index_tree(RefDNode top, SCCodeAddresses &addrList, int &i) { + std::cerr<<"use of index_tree, please report."<getNextSibling()) { index_top(t, addrList, i); @@ -369,7 +375,7 @@ DSubUD::~DSubUD() labelList.Clear(); delete tree; - codeList.erase(this); + sccList.erase(this); } DSubUD::DSubUD(const string& n,const string& o,const string& f) : @@ -525,20 +531,18 @@ void DSubUD::SetTree( RefDNode n) } //converts a SemiCompiledCode (chained list of DNodes) to a 'flat' vector of sccstruct and insert the vector in the map pointed by "sccList" -void DSubUD::SetAstTree(RefDNode n) { - //first, walk RefDnode tree to associate right,down, and self addresses with a number in the list: +void DSubUD::SetSCC(RefDNode n) { + //first, walk RefDnode tree to associate right,down, and self addresses with a number in the list. SCCodeAddresses addrList; int i=0; - index_tree(n, addrList, i); + //serialize only the current PRO or FUN by using index_top and *not* index_tree. + index_top(n, addrList, i); SCCStructV sccv; - process_tree(n, sccv, addrList); + process_top(n, sccv, addrList); //PRO or FUNCTION top node may contain a 'wrong' right pointer in the first node, due to the way the whole .pro file is compiled at once. //Remove it: sccv[0].right=0; sccList.insert(std::pair(this, sccv)); - - //to be removed - codeList.insert(std::pair(this, n)); } bool DSubUD::GetCommonVarName(const BaseGDL* p, std::string& varName) { diff --git a/src/dpro.hpp b/src/dpro.hpp index aebc060ca..9bd8d90e1 100644 --- a/src/dpro.hpp +++ b/src/dpro.hpp @@ -374,7 +374,7 @@ class DSubUD: public DSub void SetTree( ProgNodeP t) { tree = t;} //converts a SemiCompiledCode (chained list of DNodes) to a 'flat' vector of sccstruct and insert the vector in the map pointed by "sccList" - void SetAstTree( RefDNode n); + void SetSCC( RefDNode n); void AddCommon(DCommonBase* c) { common.push_back(c);} void DeleteLastAddedCommon(bool kill=true) @@ -568,13 +568,7 @@ void ReName( SizeT ix, const std::string& s) { return tree; } - RefDNode GetAstTree() - { - //find Semicompiled code saved in codeList - CodeListIterator i = codeList.find(this); - if (i!=codeList.end()) return (*i).second; - return NULL; - } + SCCStructV* GetSCC() { //find Semicompiled code saved in codeList diff --git a/src/gdlhelp.cpp b/src/gdlhelp.cpp index ea487142b..a2f443b90 100644 --- a/src/gdlhelp.cpp +++ b/src/gdlhelp.cpp @@ -396,8 +396,8 @@ static void help_object(std::ostream* ostrp, DStructDesc* objDesc, bool verbose //if object is purely GDL internal , do not display it if not verbose: if (!verbose) { bool internal=true; - for (auto i=0; i< funlist.size(); ++i) if (funlist[i]->GetAstTree()!=NULL) {internal=false; break;} - if (internal) for (auto i=0; i< prolist.size(); ++i) if (prolist[i]->GetAstTree()!=NULL) {internal=false; break;} + for (auto i=0; i< funlist.size(); ++i) if (funlist[i]->GetSCC()!=NULL) {internal=false; break;} + if (internal) for (auto i=0; i< prolist.size(); ++i) if (prolist[i]->GetSCC()!=NULL) {internal=false; break;} if (internal) return; } int num_methods = funlist.size() + prolist.size(); @@ -1030,7 +1030,7 @@ void help_help(EnvT* e) DStructDesc* s = structList[i]; for (auto j = 0; j < s->FunList().size(); ++j) { DFun* fun = (s->FunList())[j]; - if (fun->GetAstTree() != NULL) { //compiled objects, not native ones + if (fun->GetSCC() != NULL) { //compiled objects, not native ones if (fullKW or !fun->isHidden()) { fList.push_back(fun->ObjectName()); ++nfun; @@ -1052,7 +1052,7 @@ void help_help(EnvT* e) DStructDesc* s = structList[i]; for (auto j = 0; j < s->ProList().size(); ++j) { DPro * pro = (s->ProList())[j]; - if (pro->GetAstTree() != NULL) { //compiled objects, not native ones + if (pro->GetSCC() != NULL) { //compiled objects, not native ones if (fullKW or !pro->isHidden()) { pList.push_back(pro->ObjectName()); ++npro; diff --git a/src/saverestore.cpp b/src/saverestore.cpp index 4bb6705d1..a75e25316 100644 --- a/src/saverestore.cpp +++ b/src/saverestore.cpp @@ -121,35 +121,37 @@ enum { // write a copy of the SemiCompiledCode stored along with any new compiled routine. - void wr_writeNode(sccstruct node, XDR* xdrs) { + void wr_writeNode(sccstruct savenode, XDR* xdrs) { //write node index: - xdr_u_int(xdrs, & node.node); + xdr_u_int(xdrs, & savenode.node); //write right index: - xdr_u_int(xdrs, & node.right); + xdr_u_int(xdrs, & savenode.right); //write down index: - xdr_u_int(xdrs, & node.down); + xdr_u_int(xdrs, & savenode.down); //other infos - xdr_u_int(xdrs, &node.nodeType); //ttype - uint l = (node.Text).length(); + xdr_u_int(xdrs, &savenode.nodeType); //ttype + uint l = (savenode.Text).length(); xdr_u_int(xdrs, &l); - char *t = (char*) (node.Text).c_str(); + char *t = (char*) (savenode.Text).c_str(); xdr_string(xdrs, &t, l); //TEXT - xdr_u_int(xdrs, & node.ligne); //LineNUM - xdr_u_int(xdrs, & node.flags); + xdr_u_int(xdrs, & savenode.ligne); //LineNUM + xdr_u_int(xdrs, & savenode.flags); int varType = 0; - if (node.var) varType = node.var->Type(); //will be 0 otherwise + if (savenode.var!=NULL) { + varType = savenode.var->Type(); //will be 0 otherwise + } xdr_int32_t(xdrs, &varType); if (varType > 0) { switch (varType) { case GDL_FLOAT: { - DFloat f = (*static_cast (node.var))[0]; + DFloat f = (*static_cast (savenode.var))[0]; xdr_float(xdrs, &f); break; } case GDL_DOUBLE: { - DDouble d = (*static_cast (node.var))[0]; + DDouble d = (*static_cast (savenode.var))[0]; xdr_double(xdrs, & d); break; } @@ -157,49 +159,49 @@ enum { // case GDL_COMPLEXDBL: case GDL_ULONG64: { - u_int64_t ul6 = (*static_cast (node.var))[0]; + u_int64_t ul6 = (*static_cast (savenode.var))[0]; xdr_u_int64_t(xdrs, & ul6); break; } case GDL_LONG64: { - int64_t l6 = (*static_cast (node.var))[0]; + int64_t l6 = (*static_cast (savenode.var))[0]; xdr_int64_t(xdrs, & l6); break; } case GDL_BYTE: { - DByte b = (*static_cast (node.var))[0]; + DByte b = (*static_cast (savenode.var))[0]; xdr_u_char(xdrs, & b); break; } case GDL_INT: { - DInt i = (*static_cast (node.var))[0]; + DInt i = (*static_cast (savenode.var))[0]; xdr_short(xdrs, & i); break; } case GDL_UINT: { - DUInt ui = (*static_cast (node.var))[0]; + DUInt ui = (*static_cast (savenode.var))[0]; xdr_u_short(xdrs, & ui); break; } case GDL_LONG: { - int32_t l = (*static_cast (node.var))[0]; + int32_t l = (*static_cast (savenode.var))[0]; xdr_int32_t(xdrs, & l); break; } case GDL_ULONG: { - u_int32_t ul = (*static_cast (node.var))[0]; + u_int32_t ul = (*static_cast (savenode.var))[0]; xdr_u_int32_t(xdrs, & ul); break; } case GDL_STRING: { - DString s = (*static_cast (node.var))[0]; + DString s = (*static_cast (savenode.var))[0]; u_int l = s.length(); char* c = (char*) s.c_str(); xdr_string(xdrs, &c, l); @@ -2860,7 +2862,7 @@ if (!doRoutines){ for (ProListT::iterator i = proList.begin(); i != proList.end(); ++i) { if ((*i)->ObjectName() == name) { DPro * p = (*i); - if (p->GetAstTree() != NULL) { + if (p->GetSCC() != NULL) { nextptr = writeDSubUD(xdrs, p, true); notFound = false; } @@ -2871,7 +2873,7 @@ if (!doRoutines){ for (FunListT::iterator i = funList.begin(); i != funList.end(); ++i) { if ((*i)->ObjectName() == name) { DFun * f = (*i); - if (f->GetAstTree() != NULL) { + if (f->GetSCC() != NULL) { nextptr = writeDSubUD(xdrs, f, false); notFound = false; } @@ -2895,11 +2897,11 @@ if (!doRoutines){ DStructDesc* s=structList[i]; for (auto j = 0; j < s->ProList().size(); ++j) { DPro * p = (s->ProList())[j]; - if (p->GetAstTree() != NULL) if (ignore_nosave || !(p->isNoSave())) nextptr = writeDSubUD(xdrs, p, true); + if (p->GetSCC() != NULL) if (ignore_nosave || !(p->isNoSave())) nextptr = writeDSubUD(xdrs, p, true); } for (auto j= 0 ; j < s->FunList().size(); ++j) { DFun * f = (s->FunList())[j]; - if (f->GetAstTree() != NULL) if (ignore_nosave || !(f->isNoSave())) nextptr = writeDSubUD(xdrs, f, false); + if (f->GetSCC() != NULL) if (ignore_nosave || !(f->isNoSave())) nextptr = writeDSubUD(xdrs, f, false); } }