Skip to content

refactor(pandagl): replace linear-scan image reader table with fixed-index array#321

Merged
lc-soft merged 1 commit intodevelopfrom
copilot/refactor-image-reader-method-table
Apr 29, 2026
Merged

refactor(pandagl): replace linear-scan image reader table with fixed-index array#321
lc-soft merged 1 commit intodevelopfrom
copilot/refactor-image-reader-method-table

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

pd_image_readers was a variable-length array whose entries shifted depending on which backend libraries (libpng, libjpeg) were compiled in, forcing pd_image_reader_get_methods to do a linear scan matching on type to find the right entry.

Changes

  • Fixed-index arraypd_image_readers is now pd_image_reader_methods_t pd_image_readers[PD_READER_COUNT] using C99 designated initializers, so type value == array index:

    static pd_image_reader_methods_t pd_image_readers[PD_READER_COUNT] = {
    #ifdef PANDAGL_HAS_LIBPNG
        [PD_PNG_READER]  = { ".png",      PD_PNG_READER,  pd_png_reader_create,  ... },
    #endif
    #ifdef PANDAGL_HAS_LIBJPEG
        [PD_JPEG_READER] = { ".jpeg .jpg", PD_JPEG_READER, pd_jpeg_reader_create, ... },
    #endif
        [PD_BMP_READER]  = { ".bmp",      PD_BMP_READER,  pd_bmp_reader_create,  ... },
    };

    Absent-library slots are zero-initialized (all-NULL pointers) — no missing symbol references at link time.

  • O(1) pd_image_reader_get_methods — removes the for loop; direct index access with two guards:

    • bounds: type <= PD_UNKNOWN_READER || type >= PD_READER_COUNTNULL
    • capability: !m->createNULL (catches zero-initialized absent-library slots)
  • pd_image_reader_detect_suffix — iteration now skips entries where suffix == NULL, preventing strstr(NULL, …) on zero-initialized slots.

@lc-soft lc-soft marked this pull request as ready for review April 29, 2026 14:25
@lc-soft lc-soft merged commit 1beafdf into develop Apr 29, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants