h3-py icon indicating copy to clipboard operation
h3-py copied to clipboard

v4: improve memory management tooling

Open ajfriend opened this issue 3 years ago • 1 comments

I'd like a better Cython primitive for allocating, filling, and converting from an H3 index array to a Cython memview than our current approach of paired function calls to create_ptr and create_mv.

Example:

cpdef H3int[:] get_pentagon_indexes(int res):
    cdef:
        h3lib.H3Error err

    check_res(res)

    n = h3lib.pentagonCount()

    ptr = create_ptr(n)
    err = h3lib.getPentagons(res, ptr)
    mv = create_mv(ptr, n)

    return mv

Ideas:

  • context manager (i'm not sure this can be done ergonomically)
  • "memory manager" object that's in control of the size, allocation, type, and memory-view-creation of the memory
    • avoid the need to mentally pair the memory and the memory size
    • can have accessor methods or Python attributes to create/access the pointer and memory view

Other kinds of arrays

This pattern also doesn't cover when we have to allocate space for non-H3Index arrays.

  • https://github.com/uber/h3-py/pull/248

ajfriend avatar May 25 '22 06:05 ajfriend

Another idea if this ends up being a Cython class: we could use the class to do handy things like removing H3_NULL values and freeing the extra memory, or canonicalizing the array.

ajfriend avatar May 26 '22 16:05 ajfriend