Acknowledged

API docs state that Arrays are fixed size, but they aren't

Both the Connect IQ Reference Manual and the api-docs (CIQ 7.4.3) state that "Array objects are fixed size...", but this isn't true. Arrays have methods that allow you to change their size.

Parents
  • One would hope that CIQ uses something like a linked-list to make the operations efficient.

    I don’t think it does. You can tell by creating a handful of Number arrays of different sizes (e.g. 1 element, 10 elements, 100 elements), running the app in the sim, and using the memory viewer.

    You will find that there’s a certain amount of overhead for the array object itself (15 bytes). Each element takes exactly 5 bytes (1 bytes for type and 4 bytes for value).

    Besides, implementing an array via linked-list would only be more time efficient for the use cases of adding and removing elements.

    For other use cases, the performance would be a lot worse:

    e.g. Read all items from 1000-element array:

    (implemented as an actual array): this is basically a single memcpy of 5000 bytes

    (implemented as a linked list): (follow pointer, copy 5 bytes) x 1000

    e.g. Read item from arbitrary array index:

    (implemented as an actual array): O(1) lookup

    (implemented as a linked list): O(n) lookup (unless you also have a hash table to map array indices to individual items)

    Clearly a linked-list would be a lot worse than an array in terms of space (memory) efficiency.

Comment
  • One would hope that CIQ uses something like a linked-list to make the operations efficient.

    I don’t think it does. You can tell by creating a handful of Number arrays of different sizes (e.g. 1 element, 10 elements, 100 elements), running the app in the sim, and using the memory viewer.

    You will find that there’s a certain amount of overhead for the array object itself (15 bytes). Each element takes exactly 5 bytes (1 bytes for type and 4 bytes for value).

    Besides, implementing an array via linked-list would only be more time efficient for the use cases of adding and removing elements.

    For other use cases, the performance would be a lot worse:

    e.g. Read all items from 1000-element array:

    (implemented as an actual array): this is basically a single memcpy of 5000 bytes

    (implemented as a linked list): (follow pointer, copy 5 bytes) x 1000

    e.g. Read item from arbitrary array index:

    (implemented as an actual array): O(1) lookup

    (implemented as a linked list): O(n) lookup (unless you also have a hash table to map array indices to individual items)

    Clearly a linked-list would be a lot worse than an array in terms of space (memory) efficiency.

Children