Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font caching Issue for ios #712

Open
nibblis opened this issue May 17, 2024 · 9 comments
Open

Font caching Issue for ios #712

nibblis opened this issue May 17, 2024 · 9 comments
Labels
need-info Required more details about issue

Comments

@nibblis
Copy link

nibblis commented May 17, 2024

In my project, fonts consume a significant amount of memory when launched on ios devices. There are no issues on android.

According to the FontResource code, fonts should be cached.

In my project, I specify a list of all fonts (18 in total) in a map:

@Composable
fun MontserratFontFamily() = mapOf(
    Montserrat.Black to fontFamilyResource(MR.fonts.montserrat_black.montserrat_black),
    Montserrat.Light to fontFamilyResource(MR.fonts.montserrat_light.montserrat_light),
    Montserrat.Bold to fontFamilyResource(MR.fonts.montserrat_bold.montserrat_bold),
    ...
)

Without fonts, the app usually takes up 150-200 MB of memory. With fonts, it takes 1.5-2 GB.
2024-05-17

Here is a screenshot from the profiler showing 3000 entries for fonts:
2024-05-17

@ExNDY
Copy link
Contributor

ExNDY commented May 20, 2024

Try to use:
https://github.com/Kotlin/kotlinx.collections.immutable
map is not Immutable type, i think on android you doesn't have problem because in latest versions of jetpack compose has optimisations with copy this library under hood(or you have problem on android, but don't see it). My opinion you should use persistentMapOf, not simple map.

@nibblis
Copy link
Author

nibblis commented May 20, 2024

@ExNDY, it's the same with persistentMapOf
according to the profiler in android, the memory usage does not exceed 160 MB
2024-05-20

@ExNDY
Copy link
Contributor

ExNDY commented May 20, 2024

Ios != Android,
for android used jetpack compose, for ios jetbrains solution. U should think about recomposition and ios platform is sensitive for this.
Please try on ios and say your result

@ExNDY ExNDY added the need-info Required more details about issue label May 21, 2024
@nibblis
Copy link
Author

nibblis commented May 23, 2024

not sure what exactly you mean by recomposition

i tried copying the fontFamilyResource code to my project: removed @Composable and remember, made the fonts a constant. after that, memory consumption does not exceed 200 MB

i don't think the problem is with the moko-resources library

val montserratFontFamilyMap: Map<Montserrat, FontFamily> by lazy {
    mapOf(
    Montserrat.Black to fontFamilyResource(MR.fonts.montserrat_black.montserrat_black),
    Montserrat.Light to fontFamilyResource(MR.fonts.montserrat_light.montserrat_light),
    Montserrat.Bold to fontFamilyResource(MR.fonts.montserrat_bold.montserrat_bold),
    ...
    )
)

@ExNDY
Copy link
Contributor

ExNDY commented May 23, 2024

i don't cleary understand you, you find problem with your logic on ui? or what's happined? Recomposition for me is base of compose:) If you use unstable types inside composition on every recomposition of screen (recreating/update screen content you create new values for unstable variable) because one of solution for best performance don't usage of list for remember, for example.
Because i'm question for you, about usage in yours code unstable map.
If your problem is solved - is cool.

@nibblis
Copy link
Author

nibblis commented May 23, 2024

i haven't solved the problem, most likely it's because of remember and how it's implemented for ios

if you think i'm using unstable types, then why isn't there this problem on android?

@ExNDY
Copy link
Contributor

ExNDY commented May 24, 2024

you read my first message? Android has optimisations under hood. How much experience you have in compose? I need understand what i can do for this issue

@nibblis
Copy link
Author

nibblis commented May 24, 2024

yes, i saw the message. but your assumption that android has optimization under the hood doesn't seem convincing. simply removing remember eliminates the problem

fonts don't depend on the ui state. is it possible to make fontFamilyResource not @Composable?

@ExNDY
Copy link
Contributor

ExNDY commented May 24, 2024

Hm, man, you "rewrite" on non compose style. You don't hear important argument: jetpack compose is not compose Multiplatform on other platforms, that framework very sensitive for data types. Usage of lazy inside Composable talking about you don't know principles of Compose. Sorry, i'm question you about your experience in Compose for that. You ignore me with this question. Question about persistentMapOf on ios was ignored too. If somebody wants chat with you that's cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-info Required more details about issue
Projects
None yet
Development

No branches or pull requests

2 participants