Gordon
11/20/2022, 12:34 PMpublic val Icons.Filled.Circle: ImageVector
get() {
if (_circle != null) {
return _circle!!
}
_circle = materialIcon(name = "Filled.Circle") {
materialPath {
moveTo(12.0f, 2.0f)
curveTo(6.47f, 2.0f, 2.0f, 6.47f, 2.0f, 12.0f)
reflectiveCurveToRelative(4.47f, 10.0f, 10.0f, 10.0f)
reflectiveCurveToRelative(10.0f, -4.47f, 10.0f, -10.0f)
reflectiveCurveTo(17.53f, 2.0f, 12.0f, 2.0f)
close()
}
}
return _circle!!
}
private var _circle: ImageVector? = null
And i wonder why doesn't it just use lazy initialization?
As in something like this:
public val Icons.Filled.Circle: ImageVector by lazy {
materialIcon(name = "Filled.Circle") {
materialPath {
moveTo(12.0f, 2.0f)
curveTo(6.47f, 2.0f, 2.0f, 6.47f, 2.0f, 12.0f)
reflectiveCurveToRelative(4.47f, 10.0f, 10.0f, 10.0f)
reflectiveCurveToRelative(10.0f, -4.47f, 10.0f, -10.0f)
reflectiveCurveTo(17.53f, 2.0f, 12.0f, 2.0f)
close()
}
}
}
Is it just a style issue? Or the second approach has hidden performance cost?Zaki Shaikh
11/20/2022, 1:06 PMhfhbd
11/20/2022, 3:34 PMGordon
11/20/2022, 4:26 PMhfhbd
11/20/2022, 4:33 PMlazy
. It creates a new instance that supports thread synchronization by default. Even lazy(mode = LazyThreadSafetyMode.NONE) {}
creates a UnsafeLazyImpl
. So using a private field is more performant. I would still recommend using lazy
unless you are absolutely sure you need to optimize this behavior.Gordon
11/20/2022, 4:35 PM