Skip to content

Commit ff22634

Browse files
dbatyaifbmrk
andauthored
Refactor memory management (#2954)
This PR is a general cleanup for garbage collection and memory allocation code paths. Changes: * Removed an unnecesary local variable from 'ecma_gc_mark'. * Refactored 'ecma_gc_run' to have an implicit list head during iteration, which results in one less condition in the loops, and changed the loops to use compressed pointers to reduce the overall amount of compression/decompression. * Renamed 'jmem_free_unused_memory_severity_t' to 'jmem_pressure_t', and added additional values. * Removed 'jmem_free_unused_memory_callback', instead 'ecma_free_unused_memory' is now called directly. * Reworked 'ecma_free_unused_memory' to handle all code paths related to 'jmem_pressure_t', and moved all relevant code paths into this function. This simplifies the code paths in other places. * Reworked 'jmem_heap_gc_and_alloc_block' to be more streamlined. * Changed mem-stats to not report unused pool chunks as allocated memory. * Created an allocator internal API for allocating/freeing memory blocks that are not reported as used memory in mem-stats. * Removed iteration statistics for the jerry allocator from mem-stats, as they don't provide any actually useful information. Co-authored-by: Marko Fabo <mfabo@inf.u-szeged.hu> JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
1 parent a44d584 commit ff22634

27 files changed

+272
-513
lines changed

docs/02.API-REFERENCE.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ Option bits for [jerry_parse](#jerry_parse) and
8787

8888
Set garbage collection operational mode
8989

90-
- JERRY_GC_SEVERITY_LOW - free unused objects
91-
- JERRY_GC_SEVERITY_HIGH - free as much memory as possible
90+
- JERRY_GC_PRESSURE_LOW - free unused objects
91+
- JERRY_GC_PRESSURE_HIGH - free as much memory as possible
9292

93-
The difference between `JERRY_GC_SEVERITY_LOW` and `JERRY_GC_SEVERITY_HIGH`
93+
The difference between `JERRY_GC_PRESSURE_LOW` and `JERRY_GC_PRESSURE_HIGH`
9494
is that the former keeps memory allocated for performance improvements such
9595
as property hash tables for large objects. The latter frees all possible
9696
memory blocks but the performance may drop after the garbage collection.
@@ -844,7 +844,7 @@ main (void)
844844
jerry_value_t object_value = jerry_create_object ();
845845
jerry_release_value (object_value);
846846

847-
jerry_gc (JERRY_GC_SEVERITY_LOW);
847+
jerry_gc (JERRY_GC_PRESSURE_LOW);
848848

849849
jerry_cleanup ();
850850
}

docs/10.EXT-REFERENCE-HANDLER.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ An alias to `jerryx_handler_assert_fatal`.
6969
**Summary**
7070

7171
Expose garbage collector to scripts. If the first argument of the function
72-
is logical true, it performs a high severity gc. Otherwise a low severity
72+
is logical true, it performs a high pressure gc. Otherwise a low pressure
7373
gc is performed, which is also the default if no parameters passed.
7474

7575
**Prototype**

docs/14.EXT-REFERENCE-HANDLE-SCOPE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ main (void)
4141
jerry_init (JERRY_INIT_EMPTY);
4242

4343
test_handle_scope_val ();
44-
jerry_gc (JERRY_GC_SEVERITY_LOW);
44+
jerry_gc (JERRY_GC_PRESSURE_LOW);
4545

4646
jerry_cleanup ();
4747
} /* main */
@@ -93,7 +93,7 @@ main (void)
9393
jerry_init (JERRY_INIT_EMPTY);
9494
9595
test_handle_scope_val ();
96-
jerry_gc (JERRY_GC_SEVERITY_LOW);
96+
jerry_gc (JERRY_GC_PRESSURE_LOW);
9797
9898
jerry_cleanup ();
9999
} /* main */

jerry-core/api/jerry.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,14 @@ jerry_gc (jerry_gc_mode_t mode) /**< operational mode */
292292
{
293293
jerry_assert_api_available ();
294294

295-
ecma_gc_run (mode == JERRY_GC_SEVERITY_LOW ? JMEM_FREE_UNUSED_MEMORY_SEVERITY_LOW
296-
: JMEM_FREE_UNUSED_MEMORY_SEVERITY_HIGH);
295+
if (mode == JERRY_GC_PRESSURE_LOW)
296+
{
297+
/* Call GC directly, because 'ecma_free_unused_memory' might decide it's not yet worth it. */
298+
ecma_gc_run ();
299+
return;
300+
}
301+
302+
ecma_free_unused_memory (JMEM_PRESSURE_HIGH);
297303
} /* jerry_gc */
298304

299305
/**

0 commit comments

Comments
 (0)