optimize
Nasal Interpreter Test / linux-x86_64 (push) Failing after 19s Details
Nasal Interpreter Test / mac-aarch64 (push) Has been cancelled Details

This commit is contained in:
ValKmjolnir 2025-03-13 20:28:21 +08:00
parent 705df8dc1d
commit bde152b6e9
3 changed files with 30 additions and 26 deletions

View File

@ -194,9 +194,11 @@ void gc::sweep() {
for (auto& i : collect) { for (auto& i : collect) {
for (int j = 0; j < gc_type_size; ++j) { for (int j = 0; j < gc_type_size; ++j) {
for (auto ptr : i[j]) { unused[j].insert(
unused[j].push_back(ptr); unused[j].end(),
} std::make_move_iterator(i[j].begin()),
std::make_move_iterator(i[j].end())
);
} }
} }
return; return;
@ -240,21 +242,21 @@ void gc::extend(const vm_type type) {
} }
switch(type) { switch(type) {
case vm_type::vm_str: 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: 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: 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: 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: 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: 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: 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: 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; 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] = var::gcobj(new nas_val(vm_type::vm_str));
strs[i].val.gcobj->immutable = 1; strs[i].val.gcobj->immutable = 1;
strs[i].str() = constant_strings[i]; strs[i].str() = constant_strings[i];
total_memory_usage += strs[i].str().size(); total_object_count += strs[i].str().size();
total_memory_usage += sizeof(std::string); total_object_count += sizeof(std::string);
} }
// record arguments // record arguments
env_argv.resize(argv.size()); env_argv.resize(argv.size());
for (u64 i = 0; i < argv.size(); ++i) { for (u64 i = 0; i < argv.size(); ++i) {
// incremental initialization, avoid memory leak in repl mode // 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; continue;
} }
env_argv[i] = var::gcobj(new nas_val(vm_type::vm_str)); env_argv[i] = var::gcobj(new nas_val(vm_type::vm_str));
env_argv[i].val.gcobj->immutable = 1; env_argv[i].val.gcobj->immutable = 1;
env_argv[i].str() = argv[i]; env_argv[i].str() = argv[i];
total_memory_usage += env_argv[i].str().size(); total_object_count += env_argv[i].str().size();
total_memory_usage += sizeof(std::string); total_object_count += sizeof(std::string);
} }
} }

View File

@ -38,16 +38,16 @@ struct gc {
/* runtime context */ /* runtime context */
context* running_context = nullptr; context* running_context = nullptr;
nas_co* cort = nullptr; // running coroutine nas_co* cort = nullptr; // running coroutine
/* temporary space used in native/module functions */ /* temporary space used in native / module functions */
var temp = nil; var temp = nil;
/* constants and memory pool */ /* constants and memory pool */
std::vector<var> strs = {}; // reserved address for const vm_str std::vector<var> strs = {}; // reserved address for const vm_str
std::vector<var> env_argv = {}; // command line arguments std::vector<var> env_argv = {}; // command line arguments
std::vector<nas_val*> memory; // gc memory std::vector<nas_val*> memory; // gc memory
free_list unused; // gc free list free_list unused; // gc free list
/* heap increase size */ /* heap increase size */
u64 incr[gc_type_size] = { u64 incr[gc_type_size] = {
@ -60,8 +60,9 @@ struct gc {
4, // vm_co 4, // vm_co
1, // vm_map 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 */ /* values for analysis */
u64 size[gc_type_size]; u64 size[gc_type_size];
@ -115,8 +116,9 @@ public:
return worktime * 1.0 / den * 1000.0; return worktime * 1.0 / den * 1000.0;
} }
// not very accurate
double get_total_memory() const { 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: public:

View File

@ -51,7 +51,7 @@ struct nas_map; // mapper
// nas_val includes gc-managed types // nas_val includes gc-managed types
struct nas_val { struct nas_val {
enum class gc_status: u8 { enum class gc_status: u8 {
uncollected = 0, uncollected = 0,
collected, collected,
found found
}; };