Skip to content

Consider memory alloc API with explicit size param for m_free() #2

Description

@pfalcon

When dealing with interpreters, object size size is either implicitly known (for example, basic representation of object has fixed size, like 8 or 32 bytes), or size is stored explicitly on interpreter's level (for example, array size needs to be stored in object header anyway). This means that it may be possible to optimize low-level memory allocation system by not storing memory chunk size (thus saving memory), instead relying that higher levels will pass size explicitly.

Quick look at current sources doesn't show that MicroPython is ideally suited for such optimization, but that's why I write - to propose to add that as a (non-immediate) design goal.

First steps towards that can be simple: make m_free() have signature m_free(ptr, size), and m_realloc(ptr, old_size, new_size), then while going over code to adjust call sites, see if what to pass as "size" param is obvious. In case it's not, well, pass 0, and leave larger refactors to someone who really will implement alternative allocators. Ultimately, there're good reasons for having explicit size field for all variable-length objects, IMHO.

Activity

  1. dpgeorge commented on Dec 29, 2013

    @dpgeorge
    Member

    Good idea.

    In the current garbage collector (that runs properly only on the MCU), the object size is implicitly known. There is a bitmap at the very start of the heap which indicates which blocks (32 bytes each) are allocated. So an object size can be computed by working out how many blocks are contiguously allocated. There is also a bit to indicate "start of run of blocks".

  2. dpgeorge commented on Dec 29, 2013

    @dpgeorge
    Member

    Okay, I've implemented it as you suggested, with an explicit size parameter for m_free and m_realloc. The macros you should use are m_del, m_del_obj and m_renew.

  3. added a commit that references this issue on Aug 28, 2016
  4. added a commit that references this issue on Oct 16, 2016
    0e1fd9a
  5. added a commit that references this issue on Oct 6, 2017
  6. added a commit that references this issue on Jan 11, 2018
  7. added a commit that references this issue on Aug 16, 2018
  8. added a commit that references this issue on Jan 15, 2019
  9. added a commit that references this issue on Nov 25, 2019
  10. added 2 commits that reference this issue on Nov 26, 2019
  11. added a commit that references this issue on Mar 24, 2020
  12. 5 remaining items

  13. added a commit that references this issue on May 19, 2020
  14. added a commit that references this issue on Oct 1, 2020
  15. added a commit that references this issue on Aug 26, 2021
  16. added a commit that references this issue on Apr 16, 2023
  17. added a commit that references this issue on Feb 12, 2024
  18. added a commit that references this issue on Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions