apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Maxim Yegorushkin" <maxim.yegorush...@gmail.com>
Subject [PATCH] greener apr allocator
Date Fri, 04 Jan 2008 11:02:26 GMT
Hi there,

I am using apr as a system layer in the development of a cross-platform
financial system in London. While using it there has been spotted an
opportunity to reduce the memory footprint of the allocator, to make it a
bit greener. Not to save a forest ;), however, to provide extra 16 bytes
(32-bit system) to the user from each apr_memnode_t allocation.

In a test run, a newly created pool is now able to accommodate 1015 8-byte
objects before requesting a new memory node, instead of 1013.

Summary of changes:

* There is a bug in several memory allocation functions in apr_pools.c,
  where memory request size is compared with free memory available from the
  node this way:

    /* If the active node has enough bytes left, use it. */
    if (size < (apr_size_t)(active->endp - active->first_avail)) {

  Thus, if size is 8 and the active node has 8 bytes left free, the strict
  comparison yields false. The comparisons changed to <=. This change
  provides extra 8 bytes of memory to the user (since the minimum allocation
  size is 8).

* apr_memnode_t structure cleaned up.

  apr_memnode_t::endp member has been removed. Instead, the value is
  calculated this way:

    static inline char* memnode_end(apr_memnode_t* node)
    {
        return (char*)node + (1 + node->index << BOUNDARY_INDEX);
    }

    static inline apr_size_t memnode_space_left(apr_memnode_t* node)
    {
        return memnode_end(node) - node->first_avail;
    }

  apr_memnode_t::free_index member is removed. The value is calculated this
  way:

    static inline apr_size_t memnode_free_index(apr_memnode_t* node)
    {
        return APR_ALIGN(memnode_space_left(node) + 1, BOUNDARY_SIZE)
            - BOUNDARY_SIZE >> BOUNDARY_INDEX;
    }

  Removing these two members yields another 8 extra bytes.

* allocator_alloc() has been refactored to eliminate race conditions. It
  used to hold the mutex while changing apr_allocator_t members, but not
  while reading.

* Some duplicate code was extracted into functions. apr_pool.c code is now
  63 lines shorter.

Please comment it.

--
Max

Mime
View raw message