diff --git a/src/nasal_gc.cpp b/src/nasal_gc.cpp index 12ed05f..00975b9 100644 --- a/src/nasal_gc.cpp +++ b/src/nasal_gc.cpp @@ -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& 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); } } diff --git a/src/nasal_gc.h b/src/nasal_gc.h index 4e3691d..36adead 100644 --- a/src/nasal_gc.h +++ b/src/nasal_gc.h @@ -38,16 +38,16 @@ struct gc { /* runtime context */ 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; /* constants and memory pool */ - std::vector strs = {}; // reserved address for const vm_str - std::vector env_argv = {}; // command line arguments - std::vector memory; // gc memory - free_list unused; // gc free list + std::vector strs = {}; // reserved address for const vm_str + std::vector env_argv = {}; // command line arguments + std::vector memory; // gc memory + free_list unused; // gc free list /* heap increase size */ u64 incr[gc_type_size] = { @@ -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: diff --git a/src/nasal_type.h b/src/nasal_type.h index fb283ed..7cda862 100644 --- a/src/nasal_type.h +++ b/src/nasal_type.h @@ -51,7 +51,7 @@ struct nas_map; // mapper // nas_val includes gc-managed types struct nas_val { enum class gc_status: u8 { - uncollected = 0, + uncollected = 0, collected, found };