I'm looking at the Icons declaration in compose, ...
# getting-started
g
I'm looking at the Icons declaration in compose, an i have a style question. Right now the icons declaration looks like this:
Copy code
public 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:
Copy code
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?
z
You should ask this in #compose
h
Do you mean the use of lazy vs private null field? Lazy creates some overhead
g
Yes, where can i learn more about that overheard.
h
Just look at the source of
lazy
. 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.
g
Thanks. 👍