optimize codes and delete unused variables

This commit is contained in:
ValKmjolnir 2022-07-05 00:49:38 +08:00
parent 92d68b357c
commit 2bc15697df
2 changed files with 38 additions and 38 deletions

View File

@ -46,5 +46,5 @@ test:nasal
-@ ./nasal -op -c -t test/tetris.nas -@ ./nasal -op -c -t test/tetris.nas
@ ./nasal -op -c -t -d test/turingmachine.nas @ ./nasal -op -c -t -d test/turingmachine.nas
@ ./nasal -op -c -t -d -o test/ycombinator.nas @ ./nasal -op -c -t -d -o test/ycombinator.nas
@ ./nasal -op -e test/wavecollapse.nas @ ./nasal -op -d -o test/wavecollapse.nas

View File

@ -17,12 +17,12 @@ enum vm_type:std::uint32_t{
vm_upval, vm_upval,
vm_obj, vm_obj,
vm_co, vm_co,
vm_type_size vm_tsize
}; };
const uint32_t gc_obj_size=vm_type_size-vm_str; const uint32_t gc_tsize=vm_tsize-vm_str;
// change parameters here to make your own efficient gc // change parameters here to make your own efficient gc
// better set bigger number on vm_vec // better set bigger number on vm_vec
const uint32_t ini[gc_obj_size]= const uint32_t ini[gc_tsize]=
{ {
128, // vm_str 128, // vm_str
512, // vm_func 512, // vm_func
@ -32,7 +32,7 @@ const uint32_t ini[gc_obj_size]=
0, // vm_obj 0, // vm_obj
0 // vm_co 0 // vm_co
}; };
const uint32_t incr[gc_obj_size]= const uint32_t incr[gc_tsize]=
{ {
256, // vm_str 256, // vm_str
512, // vm_func 512, // vm_func
@ -447,26 +447,26 @@ struct nasal_gc
} main_ctx; } main_ctx;
/* runtime context */ /* runtime context */
uint32_t& pc; // program counter uint32_t& pc; // program counter
nasal_ref*& localr; // local scope register nasal_ref*& localr; // local scope register
nasal_ref*& memr; // used for mem_call nasal_ref*& memr; // used for mem_call
nasal_ref& funcr; // function register nasal_ref& funcr; // function register
nasal_ref& upvalr; // upvalue register nasal_ref& upvalr; // upvalue register
nasal_ref*& canary; // avoid stackoverflow nasal_ref*& canary; // avoid stackoverflow
nasal_ref*& top; // stack top nasal_ref*& top; // stack top
nasal_ref* stack; // stack pointer nasal_ref* stack; // stack pointer
nasal_co* coroutine; // running coroutine nasal_co* coroutine; // running coroutine
/* constants and memory pool */ /* constants and memory pool */
std::vector<nasal_ref> strs; // reserved address for const vm_str std::vector<nasal_ref> strs; // reserved address for const vm_str
std::vector<nasal_val*> memory; // gc memory std::vector<nasal_val*> memory; // gc memory
std::queue<nasal_val*> free_list[vm_type_size]; // gc free list std::queue<nasal_val*> free_list[gc_tsize]; // gc free list
std::vector<nasal_ref> env_argv; // command line arguments std::vector<nasal_ref> env_argv; // command line arguments
/* values for analysis */ /* values for analysis */
uint64_t size[gc_obj_size]; uint64_t size[gc_tsize];
uint64_t count[gc_obj_size]; uint64_t count[gc_tsize];
uint64_t allocc[gc_obj_size]; uint64_t allocc[gc_tsize];
nasal_gc( nasal_gc(
uint32_t& _pc, uint32_t& _pc,
nasal_ref*& _localr, nasal_ref*& _localr,
@ -567,7 +567,7 @@ void nasal_gc::sweep()
case vm_obj: i->ptr.obj->clear(); break; case vm_obj: i->ptr.obj->clear(); break;
case vm_co: i->ptr.co->clear(); break; case vm_co: i->ptr.co->clear(); break;
} }
free_list[i->type].push(i); free_list[i->type-vm_str].push(i);
i->mark=GC_COLLECTED; i->mark=GC_COLLECTED;
} }
else if(i->mark==GC_FOUND) else if(i->mark==GC_FOUND)
@ -579,12 +579,12 @@ void nasal_gc::init(const std::vector<std::string>& s,const std::vector<std::str
// initiaize function register // initiaize function register
funcr=nil; funcr=nil;
for(uint8_t i=0;i<gc_obj_size;++i) for(uint8_t i=0;i<gc_tsize;++i)
size[i]=count[i]=allocc[i]=0; size[i]=count[i]=allocc[i]=0;
for(uint8_t i=vm_str;i<vm_type_size;++i) for(uint8_t i=0;i<gc_tsize;++i)
for(uint32_t j=0;j<ini[i-vm_str];++j) for(uint32_t j=0;j<ini[i];++j)
{ {
nasal_val* tmp=new nasal_val(i); nasal_val* tmp=new nasal_val(i+vm_str);
memory.push_back(tmp); memory.push_back(tmp);
free_list[i].push(tmp); free_list[i].push(tmp);
} }
@ -611,7 +611,7 @@ void nasal_gc::clear()
for(auto i:memory) for(auto i:memory)
delete i; delete i;
memory.clear(); memory.clear();
for(uint8_t i=0;i<vm_type_size;++i) for(uint8_t i=0;i<gc_tsize;++i)
while(!free_list[i].empty()) while(!free_list[i].empty())
free_list[i].pop(); free_list[i].pop();
for(auto& i:strs) for(auto& i:strs)
@ -623,35 +623,35 @@ void nasal_gc::info()
{ {
const char* name[]={"str ","func ","vec ","hash ","upval","obj ","co "}; const char* name[]={"str ","func ","vec ","hash ","upval","obj ","co "};
std::cout<<"\ngarbage collector info\n"; std::cout<<"\ngarbage collector info\n";
for(uint8_t i=0;i<gc_obj_size;++i) for(uint8_t i=0;i<gc_tsize;++i)
std::cout<<" "<<name[i]<<" | gc | "<<count[i]<<"\n" std::cout<<" "<<name[i]<<" | gc | "<<count[i]<<"\n"
<<" | new | "<<allocc[i]<<"\n"; <<" | new | "<<allocc[i]<<"\n";
std::cout<<"\nmemory allocator info(max size)\n"; std::cout<<"\nmemory allocator info(max size)\n";
for(uint8_t i=0;i<gc_obj_size;++i) for(uint8_t i=0;i<gc_tsize;++i)
std::cout<<" "<<name[i]<<" | "<<ini[i]+size[i]*incr[i]<<" (+"<<size[i]<<")\n"; std::cout<<" "<<name[i]<<" | "<<ini[i]+size[i]*incr[i]<<" (+"<<size[i]<<")\n";
} }
nasal_ref nasal_gc::alloc(uint8_t type) nasal_ref nasal_gc::alloc(uint8_t type)
{ {
++allocc[type-vm_str]; ++allocc[type-vm_str];
if(free_list[type].empty()) if(free_list[type-vm_str].empty())
{ {
++count[type-vm_str]; ++count[type-vm_str];
mark(); mark();
sweep(); sweep();
} }
if(free_list[type].empty()) if(free_list[type-vm_str].empty())
{ {
++size[type-vm_str]; ++size[type-vm_str];
for(uint32_t i=0;i<incr[type-vm_str];++i) for(uint32_t i=0;i<incr[type-vm_str];++i)
{ {
nasal_val* tmp=new nasal_val(type); nasal_val* tmp=new nasal_val(type);
memory.push_back(tmp); memory.push_back(tmp);
free_list[type].push(tmp); free_list[type-vm_str].push(tmp);
} }
} }
nasal_ref ret={type,free_list[type].front()}; nasal_ref ret={type,free_list[type-vm_str].front()};
ret.value.gcobj->mark=GC_UNCOLLECTED; ret.value.gcobj->mark=GC_UNCOLLECTED;
free_list[type].pop(); free_list[type-vm_str].pop();
return ret; return ret;
} }
nasal_ref nasal_gc::builtin_alloc(uint8_t type) nasal_ref nasal_gc::builtin_alloc(uint8_t type)
@ -661,19 +661,19 @@ nasal_ref nasal_gc::builtin_alloc(uint8_t type)
// and the value got before will be collected,this is a fatal error // and the value got before will be collected,this is a fatal error
// so use builtin_alloc in builtin functions if this function uses alloc more then one time // so use builtin_alloc in builtin functions if this function uses alloc more then one time
++allocc[type-vm_str]; ++allocc[type-vm_str];
if(free_list[type].empty()) if(free_list[type-vm_str].empty())
{ {
++size[type-vm_str]; ++size[type-vm_str];
for(uint32_t i=0;i<incr[type-vm_str];++i) for(uint32_t i=0;i<incr[type-vm_str];++i)
{ {
nasal_val* tmp=new nasal_val(type); nasal_val* tmp=new nasal_val(type);
memory.push_back(tmp); memory.push_back(tmp);
free_list[type].push(tmp); free_list[type-vm_str].push(tmp);
} }
} }
nasal_ref ret={type,free_list[type].front()}; nasal_ref ret={type,free_list[type-vm_str].front()};
ret.value.gcobj->mark=GC_UNCOLLECTED; ret.value.gcobj->mark=GC_UNCOLLECTED;
free_list[type].pop(); free_list[type-vm_str].pop();
return ret; return ret;
} }
void nasal_gc::ctxchg(nasal_co& context) void nasal_gc::ctxchg(nasal_co& context)