How much performance impact `copy` function of `da...
# android
k
How much performance impact
copy
function of
data
class have if called many times ?? for example i have
data class Position(val x:Float,val y:Float)
and i'm doing
copy
of this class's instance inside android's touch listener when user's finger is moved(ACTION_MOVE) like ->
Copy code
var position = Position(0f, 0f)
view.setOnTouchListener { v, event ->
    when (event.action){
        MotionEvent.ACTION_DOWN -> /**/
        MotionEvent.ACTION_MOVE -> {
            position = position.copy(x =computedX,y=computedY)//how much performance impact of this ??
        }
    }

}
i don't want to use mutable properties for data class for some reasons
g
essentailly the same as Position(computedX, computedY), it creates an object
also not sure why you need copy here, you modify all the available properties, nothing to copy, it’s the same as create a new one
mutable will be better of course, less pressure on GC
but if it doesn’t cause any problems on practice it would be fine
k
I don't want to make properties mutable in real app because i want to update properties at specific place
g
so what kind choice you have? 🙂
or be a bit more optimized and mutable, or immutable and create an object on every touch event
on practice you will not notice any slowdown, it’s not onDraw after all
k
so what kind choice you have?
only
copy
😉, but i was curious to know how much its going to effect
g
method call, object cretion, nothing magical there
only 
copy
Actuallyu in case of Position you even don’t need copy, probably call of constructor would be more explicit
k
you are right, but in real app i have more properties in Position 🙂
No issues, thank you for help
g
Copy code
@NotNull
public final Position copy(int x, int y) {
   return new Position(x, y);
}
👍 1
decompiled copy method
k
got it, BTW when will be those previously(unwanted) created Position objects collected by GC ?
g
depends on GC, different Android GCs has a bit different strategies
If you curious, recommend to check this talk

https://www.youtube.com/watch?v=oKMsPrDMprE

It gives some introduction on different GCs
k
yeah sure, i will definitely check it
r
make ur own data class with mutable properties, when done use the non mutable. seems like what your trying to do is the root of all evil