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 -d test/turingmachine.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_obj,
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
// better set bigger number on vm_vec
const uint32_t ini[gc_obj_size]=
const uint32_t ini[gc_tsize]=
{
128, // vm_str
512, // vm_func
@ -32,7 +32,7 @@ const uint32_t ini[gc_obj_size]=
0, // vm_obj
0 // vm_co
};
const uint32_t incr[gc_obj_size]=
const uint32_t incr[gc_tsize]=
{
256, // vm_str
512, // vm_func
@ -460,13 +460,13 @@ struct nasal_gc
/* constants and memory pool */
std::vector<nasal_ref> strs; // reserved address for const vm_str
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
/* values for analysis */
uint64_t size[gc_obj_size];
uint64_t count[gc_obj_size];
uint64_t allocc[gc_obj_size];
uint64_t size[gc_tsize];
uint64_t count[gc_tsize];
uint64_t allocc[gc_tsize];
nasal_gc(
uint32_t& _pc,
nasal_ref*& _localr,
@ -567,7 +567,7 @@ void nasal_gc::sweep()
case vm_obj: i->ptr.obj->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;
}
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
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;
for(uint8_t i=vm_str;i<vm_type_size;++i)
for(uint32_t j=0;j<ini[i-vm_str];++j)
for(uint8_t i=0;i<gc_tsize;++i)
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);
free_list[i].push(tmp);
}
@ -611,7 +611,7 @@ void nasal_gc::clear()
for(auto i:memory)
delete i;
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())
free_list[i].pop();
for(auto& i:strs)
@ -623,35 +623,35 @@ void nasal_gc::info()
{
const char* name[]={"str ","func ","vec ","hash ","upval","obj ","co "};
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"
<<" | new | "<<allocc[i]<<"\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";
}
nasal_ref nasal_gc::alloc(uint8_t type)
{
++allocc[type-vm_str];
if(free_list[type].empty())
if(free_list[type-vm_str].empty())
{
++count[type-vm_str];
mark();
sweep();
}
if(free_list[type].empty())
if(free_list[type-vm_str].empty())
{
++size[type-vm_str];
for(uint32_t i=0;i<incr[type-vm_str];++i)
{
nasal_val* tmp=new nasal_val(type);
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;
free_list[type].pop();
free_list[type-vm_str].pop();
return ret;
}
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
// so use builtin_alloc in builtin functions if this function uses alloc more then one time
++allocc[type-vm_str];
if(free_list[type].empty())
if(free_list[type-vm_str].empty())
{
++size[type-vm_str];
for(uint32_t i=0;i<incr[type-vm_str];++i)
{
nasal_val* tmp=new nasal_val(type);
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;
free_list[type].pop();
free_list[type-vm_str].pop();
return ret;
}
void nasal_gc::ctxchg(nasal_co& context)