⚡ optimize
This commit is contained in:
parent
705df8dc1d
commit
bde152b6e9
|
@ -194,9 +194,11 @@ void gc::sweep() {
|
|||
|
||||
for (auto& i : collect) {
|
||||
for (int j = 0; j < gc_type_size; ++j) {
|
||||
for (auto ptr : i[j]) {
|
||||
unused[j].push_back(ptr);
|
||||
}
|
||||
unused[j].insert(
|
||||
unused[j].end(),
|
||||
std::make_move_iterator(i[j].begin()),
|
||||
std::make_move_iterator(i[j].end())
|
||||
);
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
@ -240,21 +242,21 @@ void gc::extend(const vm_type type) {
|
|||
}
|
||||
switch(type) {
|
||||
case vm_type::vm_str:
|
||||
total_memory_usage += incr[index] * sizeof(std::string); break;
|
||||
total_object_count += incr[index] * sizeof(std::string); break;
|
||||
case vm_type::vm_vec:
|
||||
total_memory_usage += incr[index] * sizeof(nas_vec); break;
|
||||
total_object_count += incr[index] * sizeof(nas_vec); break;
|
||||
case vm_type::vm_hash:
|
||||
total_memory_usage += incr[index] * sizeof(nas_hash); break;
|
||||
total_object_count += incr[index] * sizeof(nas_hash); break;
|
||||
case vm_type::vm_func:
|
||||
total_memory_usage += incr[index] * sizeof(nas_func); break;
|
||||
total_object_count += incr[index] * sizeof(nas_func); break;
|
||||
case vm_type::vm_upval:
|
||||
total_memory_usage += incr[index] * sizeof(nas_upval); break;
|
||||
total_object_count += incr[index] * sizeof(nas_upval); break;
|
||||
case vm_type::vm_ghost:
|
||||
total_memory_usage += incr[index] * sizeof(nas_ghost); break;
|
||||
total_object_count += incr[index] * sizeof(nas_ghost); break;
|
||||
case vm_type::vm_co:
|
||||
total_memory_usage += incr[index] * sizeof(nas_co); break;
|
||||
total_object_count += incr[index] * sizeof(nas_co); break;
|
||||
case vm_type::vm_map:
|
||||
total_memory_usage += incr[index] * sizeof(nas_map); break;
|
||||
total_object_count += incr[index] * sizeof(nas_map); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
|
@ -283,22 +285,22 @@ void gc::init(const std::vector<std::string>& constant_strings,
|
|||
strs[i] = var::gcobj(new nas_val(vm_type::vm_str));
|
||||
strs[i].val.gcobj->immutable = 1;
|
||||
strs[i].str() = constant_strings[i];
|
||||
total_memory_usage += strs[i].str().size();
|
||||
total_memory_usage += sizeof(std::string);
|
||||
total_object_count += strs[i].str().size();
|
||||
total_object_count += sizeof(std::string);
|
||||
}
|
||||
|
||||
// record arguments
|
||||
env_argv.resize(argv.size());
|
||||
for (u64 i = 0; i < argv.size(); ++i) {
|
||||
// incremental initialization, avoid memory leak in repl mode
|
||||
if (env_argv[i].is_str() && env_argv[i].str()==argv[i]) {
|
||||
if (env_argv[i].is_str() && env_argv[i].str() == argv[i]) {
|
||||
continue;
|
||||
}
|
||||
env_argv[i] = var::gcobj(new nas_val(vm_type::vm_str));
|
||||
env_argv[i].val.gcobj->immutable = 1;
|
||||
env_argv[i].str() = argv[i];
|
||||
total_memory_usage += env_argv[i].str().size();
|
||||
total_memory_usage += sizeof(std::string);
|
||||
total_object_count += env_argv[i].str().size();
|
||||
total_object_count += sizeof(std::string);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ struct gc {
|
|||
context* running_context = nullptr;
|
||||
nas_co* cort = nullptr; // running coroutine
|
||||
|
||||
/* temporary space used in native/module functions */
|
||||
/* temporary space used in native / module functions */
|
||||
var temp = nil;
|
||||
|
||||
/* constants and memory pool */
|
||||
|
@ -60,8 +60,9 @@ struct gc {
|
|||
4, // vm_co
|
||||
1, // vm_map
|
||||
};
|
||||
// total memory usage, not very accurate
|
||||
u64 total_memory_usage = 0;
|
||||
|
||||
// total object count
|
||||
u64 total_object_count = 0;
|
||||
|
||||
/* values for analysis */
|
||||
u64 size[gc_type_size];
|
||||
|
@ -115,8 +116,9 @@ public:
|
|||
return worktime * 1.0 / den * 1000.0;
|
||||
}
|
||||
|
||||
// not very accurate
|
||||
double get_total_memory() const {
|
||||
return total_memory_usage * 1.0 / 1024.0 / 1024.0;
|
||||
return total_object_count * 3.5 / 1024.0 / 1024.0;
|
||||
}
|
||||
|
||||
public:
|
||||
|
|
Loading…
Reference in New Issue