I'm looking at the Icons declaration in compose, an i have a style question. Right now the icons de...
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?
l
It’s more about the bytecode size, since there are so many icons the extra cost of using lazy in terms of generated code adds up. Icons used to use lazy, but when we changed from lazy to the current approach we saw a more than 50% AAR size reduction - there have been some optimizations in the compiler since then but it will still be more code and hence larger aar size
By default (like in the code you have)
lazy
is thread-synchronized, so that definitely will have a performance cost, but you can change the synchronization to none and then the performance overhead is minimal
g
Awesome. Thanks.
l
For normal code both of these are probably not important, but when shipping 1000s of small icons that all look the same it definitely adds up