# Confetti Cleanup

DevFeed: [Confetti Cleanup](<https://devfeed.tech/articles/confetti-cleanup-32044.md>)

Original publisher: [Read original article](<https://www.maiatoday.net/p/confetti-cleanup/>)

Published: 2021-12-31T05:37:42Z

Content type: tutorial

Language: en

Sources: [maiatoday](<https://devfeed.tech/sources/maiatoday.md>)

Topics: [Jetpack Compose](<https://devfeed.tech/topics/jetpack-compose.md>), [Code](<https://devfeed.tech/topics/code.md>), [implementation](<https://devfeed.tech/topics/implementation.md>), [function](<https://devfeed.tech/topics/function.md>)

Tags: [animate](<https://devfeed.tech/tags/animate.md>), [animation](<https://devfeed.tech/tags/animation.md>), [code](<https://devfeed.tech/tags/code.md>), [compose](<https://devfeed.tech/tags/compose.md>), [extension-function](<https://devfeed.tech/tags/extension-function.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [memory](<https://devfeed.tech/tags/memory.md>), [state](<https://devfeed.tech/tags/state.md>)

## AI overview

The article revisits a confetti animation modifier for Jetpack Compose and examines object allocation in its particle system. It explains how using the immutable Offset class reduces extra allocations while retaining Compose's state-change requirements, and briefly shows how to create and apply a drawing modifier.

## Source excerpt

In my exploration of animation in Jetpack Compose I built a Confetti modifier that could be applied to any composable. I first showed the modifier in my presentation for Chicago Roboto but since I made it something was bothering me. The way that I can get the state in Compose to register change is by making copies of objects or making new objects. For a modifier that uses particles this can be many objects. I profiled my original modifier and sure enough I see the object allocation and also the garbage collectore cleaning up when the confetti modifier is visible. Fast forward to Dec 2021 and my friends over at Code with Italians built a similar snow modifier. Revisiting my modifier, I cleaned it up. I managed to get rid of some of the object allocation. However since it is a particle system and each of the particles change position, it needs some form of state that changes as the position of the particles change. I used the Offset class as this is the object that a Circle needs to draw and the object has been optimised to pack the x and y floats. It also has some other useful functions and operators. Offset is immutable so if the particle changes position a new object is made. It is smaller than my original implementation. It isn't really fair to compare the screenshot below with the one above as the time over which the heap capture ran is is not the same. The shallow size of Offset is 16 and the shallow size of the original Confetto objects is 24 and the point inside the Confetto object also has a shallow size of 16. The improvements eliminated the allocation of the extra Confetto object. My intuition says it is preferable to sprinkle the memory with fewer smaller pieces of confetti rather than many larger ones. Still there isn't really a way around it since Offset is immutable and Compose is built around tracking unidirectional state flow. Mutable State holder classes won't register a change if the object that holds the state doesn't change. This means if I keep a