writeCollectionOptimized

inline fun <T> Parcel.writeCollectionOptimized(collection: Collection<T>?, elementWriter: (Parcel, T) -> Unit)

Optimized way to write a collection of T objects to a Parcel.

The default, Parcel.writeList uses Parcel.writeValue which determines the list element type at runtime and adds this type for each list element to the parcel. This results in doubling the size of a parcel for an int list. An alternative is to convert the list type to an array. This comes with the price of copying the whole list twice. Parcel.writeTypedList is only capable of writing Parcelable elements.

This variant writes a collection and uses the lambda elementWriter to write the elements. As such, the list can have any element type that is supported by Parcel.