Compacting pool header to 8 bytes. Replaced typed pool chunks with fixed-size untyped ones. loop_arithmetics_1kk.js benchmark: 2.98517 -> 2.9443.
This commit is contained in:
+39
-64
@@ -31,13 +31,14 @@
|
||||
#include "mem-allocator.h"
|
||||
#include "mem-pool.h"
|
||||
|
||||
/**
|
||||
* Magic number to fill free chunks in debug version
|
||||
*/
|
||||
static const uint8_t mem_pool_free_chunk_magic_num = 0x71;
|
||||
|
||||
static void mem_check_pool( mem_pool_state_t *pool_p);
|
||||
|
||||
/**
|
||||
* Get address of pool chunk with specified index
|
||||
*/
|
||||
#define MEM_POOL_CHUNK_ADDRESS( pool_header_p, chunk_index) ( (uint8_t*) ( MEM_POOL_SPACE_START( pool_p) + \
|
||||
MEM_POOL_CHUNK_SIZE * chunk_index ) )
|
||||
|
||||
/**
|
||||
* Initialization of memory pool.
|
||||
*
|
||||
@@ -46,66 +47,40 @@ static void mem_check_pool( mem_pool_state_t *pool_p);
|
||||
*/
|
||||
void
|
||||
mem_pool_init(mem_pool_state_t *pool_p, /**< pool */
|
||||
size_t chunk_size, /**< size of pool's chunk */
|
||||
uint8_t *pool_start, /**< start of pool space */
|
||||
size_t pool_size) /**< pool space size */
|
||||
size_t pool_size) /**< pool size */
|
||||
{
|
||||
JERRY_ASSERT( pool_p != NULL );
|
||||
JERRY_ASSERT( (uintptr_t) pool_start % MEM_ALIGNMENT == 0);
|
||||
JERRY_ASSERT( (size_t)MEM_POOL_SPACE_START( pool_p) % MEM_ALIGNMENT == 0);
|
||||
|
||||
pool_p->pool_start_p = pool_start;
|
||||
pool_p->pool_size = pool_size;
|
||||
JERRY_STATIC_ASSERT( MEM_POOL_CHUNK_SIZE % MEM_ALIGNMENT == 0 );
|
||||
JERRY_STATIC_ASSERT( MEM_POOL_MAX_CHUNKS_NUMBER_LOG <= sizeof(mem_pool_chunk_index_t) * JERRY_BITSINBYTE );
|
||||
JERRY_ASSERT( sizeof(mem_pool_chunk_index_t) <= MEM_POOL_CHUNK_SIZE );
|
||||
|
||||
switch ( chunk_size )
|
||||
{
|
||||
case 4:
|
||||
pool_p->chunk_size_log = 2;
|
||||
break;
|
||||
const size_t pool_space_size = pool_size - sizeof(mem_pool_state_t);
|
||||
const size_t chunks_number = pool_space_size / MEM_POOL_CHUNK_SIZE;
|
||||
|
||||
case 8:
|
||||
pool_p->chunk_size_log = 3;
|
||||
break;
|
||||
JERRY_ASSERT( ( (mem_pool_chunk_index_t) chunks_number ) == chunks_number );
|
||||
|
||||
case 16:
|
||||
pool_p->chunk_size_log = 4;
|
||||
break;
|
||||
|
||||
case 32:
|
||||
pool_p->chunk_size_log = 5;
|
||||
break;
|
||||
|
||||
case 64:
|
||||
pool_p->chunk_size_log = 6;
|
||||
break;
|
||||
|
||||
default:
|
||||
JERRY_UNREACHABLE();
|
||||
}
|
||||
|
||||
JERRY_ASSERT( chunk_size % MEM_ALIGNMENT == 0 );
|
||||
JERRY_ASSERT( chunk_size >= sizeof(mem_pool_chunk_offset_t) );
|
||||
|
||||
size_t chunks_area_size = JERRY_ALIGNDOWN( pool_size, chunk_size);
|
||||
size_t chunks_number = chunks_area_size / chunk_size;
|
||||
|
||||
JERRY_ASSERT( ( (mem_pool_chunk_offset_t) chunks_number ) == chunks_number );
|
||||
pool_p->chunks_number = (mem_pool_chunk_offset_t) chunks_number;
|
||||
|
||||
pool_p->first_free_chunk = 0;
|
||||
pool_p->chunks_number = (mem_pool_chunk_index_t) chunks_number;
|
||||
|
||||
/*
|
||||
* All chunks are free right after initialization
|
||||
*/
|
||||
pool_p->free_chunks_number = pool_p->chunks_number;
|
||||
|
||||
for ( uint32_t chunk_index = 0;
|
||||
/*
|
||||
* Chunk with zero index is first free chunk in the pool now
|
||||
*/
|
||||
pool_p->first_free_chunk = 0;
|
||||
|
||||
for ( mem_pool_chunk_index_t chunk_index = 0;
|
||||
chunk_index < chunks_number;
|
||||
chunk_index++ )
|
||||
{
|
||||
mem_pool_chunk_offset_t *next_free_chunk_offset_p =
|
||||
(mem_pool_chunk_offset_t*) ( pool_p->pool_start_p + chunk_size * chunk_index );
|
||||
mem_pool_chunk_index_t *next_free_chunk_index_p =
|
||||
(mem_pool_chunk_index_t*) MEM_POOL_CHUNK_ADDRESS( pool_p, chunk_index);
|
||||
|
||||
*next_free_chunk_offset_p = chunk_index + 1;
|
||||
*next_free_chunk_index_p = (mem_pool_chunk_index_t) (chunk_index + 1u);
|
||||
}
|
||||
|
||||
mem_check_pool( pool_p);
|
||||
@@ -128,11 +103,11 @@ mem_pool_alloc_chunk(mem_pool_state_t *pool_p) /**< pool */
|
||||
|
||||
JERRY_ASSERT( pool_p->first_free_chunk < pool_p->chunks_number );
|
||||
|
||||
mem_pool_chunk_offset_t chunk_index = pool_p->first_free_chunk;
|
||||
uint8_t *chunk_p = pool_p->pool_start_p + ( chunk_index << pool_p->chunk_size_log );
|
||||
mem_pool_chunk_index_t chunk_index = pool_p->first_free_chunk;
|
||||
uint8_t *chunk_p = MEM_POOL_CHUNK_ADDRESS( pool_p, chunk_index);
|
||||
|
||||
mem_pool_chunk_offset_t *next_free_chunk_offset_p = (mem_pool_chunk_offset_t*) chunk_p;
|
||||
pool_p->first_free_chunk = *next_free_chunk_offset_p;
|
||||
mem_pool_chunk_index_t *next_free_chunk_index_p = (mem_pool_chunk_index_t*) chunk_p;
|
||||
pool_p->first_free_chunk = *next_free_chunk_index_p;
|
||||
pool_p->free_chunks_number--;
|
||||
|
||||
mem_check_pool( pool_p);
|
||||
@@ -148,16 +123,17 @@ mem_pool_free_chunk(mem_pool_state_t *pool_p, /**< pool */
|
||||
uint8_t *chunk_p) /**< chunk pointer */
|
||||
{
|
||||
JERRY_ASSERT( pool_p->free_chunks_number < pool_p->chunks_number );
|
||||
JERRY_ASSERT( chunk_p >= pool_p->pool_start_p && chunk_p <= pool_p->pool_start_p + pool_p->chunks_number * ( 1u << pool_p->chunk_size_log ) );
|
||||
JERRY_ASSERT( ( (uintptr_t) chunk_p - (uintptr_t) pool_p->pool_start_p ) % ( 1u << pool_p->chunk_size_log ) == 0 );
|
||||
JERRY_ASSERT( chunk_p >= MEM_POOL_SPACE_START( pool_p) && chunk_p <= MEM_POOL_SPACE_START( pool_p) + pool_p->chunks_number * MEM_POOL_CHUNK_SIZE );
|
||||
JERRY_ASSERT( ( (uintptr_t) chunk_p - (uintptr_t) MEM_POOL_SPACE_START( pool_p) ) % MEM_POOL_CHUNK_SIZE == 0 );
|
||||
|
||||
mem_check_pool( pool_p);
|
||||
|
||||
const size_t chunk_byte_offset = (size_t) (chunk_p - pool_p->pool_start_p);
|
||||
const mem_pool_chunk_offset_t chunk_index = (mem_pool_chunk_offset_t) (chunk_byte_offset >> pool_p->chunk_size_log);
|
||||
mem_pool_chunk_offset_t *next_free_chunk_offset_p = (mem_pool_chunk_offset_t*) chunk_p;
|
||||
const size_t chunk_byte_offset = (size_t) (chunk_p - MEM_POOL_SPACE_START( pool_p));
|
||||
const mem_pool_chunk_index_t chunk_index = (mem_pool_chunk_index_t) (chunk_byte_offset / MEM_POOL_CHUNK_SIZE);
|
||||
|
||||
*next_free_chunk_offset_p = pool_p->first_free_chunk;
|
||||
mem_pool_chunk_index_t *next_free_chunk_index_p = (mem_pool_chunk_index_t*) chunk_p;
|
||||
|
||||
*next_free_chunk_index_p = pool_p->first_free_chunk;
|
||||
|
||||
pool_p->first_free_chunk = chunk_index;
|
||||
pool_p->free_chunks_number++;
|
||||
@@ -173,20 +149,19 @@ mem_check_pool( mem_pool_state_t __unused *pool_p) /**< pool (unused #ifdef JERR
|
||||
{
|
||||
#ifndef JERRY_NDEBUG
|
||||
JERRY_ASSERT( pool_p->chunks_number != 0 );
|
||||
JERRY_ASSERT( pool_p->chunks_number * ( 1u << pool_p->chunk_size_log ) <= pool_p->pool_size );
|
||||
JERRY_ASSERT( pool_p->free_chunks_number <= pool_p->chunks_number );
|
||||
|
||||
size_t met_free_chunks_number = 0;
|
||||
mem_pool_chunk_offset_t chunk_index = pool_p->first_free_chunk;
|
||||
mem_pool_chunk_index_t chunk_index = pool_p->first_free_chunk;
|
||||
|
||||
while ( chunk_index != pool_p->chunks_number )
|
||||
{
|
||||
uint8_t *chunk_p = pool_p->pool_start_p + ( 1u << pool_p->chunk_size_log ) * chunk_index;
|
||||
mem_pool_chunk_offset_t *next_free_chunk_offset_p = (mem_pool_chunk_offset_t*) chunk_p;
|
||||
uint8_t *chunk_p = MEM_POOL_CHUNK_ADDRESS( pool_p, chunk_index);
|
||||
mem_pool_chunk_index_t *next_free_chunk_index_p = (mem_pool_chunk_index_t*) chunk_p;
|
||||
|
||||
met_free_chunks_number++;
|
||||
|
||||
chunk_index = *next_free_chunk_offset_p;
|
||||
chunk_index = *next_free_chunk_index_p;
|
||||
}
|
||||
|
||||
JERRY_ASSERT( met_free_chunks_number == pool_p->free_chunks_number );
|
||||
|
||||
Reference in New Issue
Block a user