Giter VIP home page Giter VIP logo

Comments (7)

eymar avatar eymar commented on August 24, 2024 2

An improvement:

internal val labelTypography: Typography
    @Composable
    get() {
        val ff = helveticaNeueFontFamily // call getter only once, so the `fetch` wan't happen every time
        return Typography(
            displayLarge = MaterialTheme.typography.displayLarge.copy(fontFamily = ff),
            displayMedium = MaterialTheme.typography.displayMedium.copy(fontFamily = ff),
            displaySmall = MaterialTheme.typography.displaySmall.copy(fontFamily = ff),
            headlineLarge = MaterialTheme.typography.headlineLarge.copy(fontFamily = ff),
            headlineMedium = MaterialTheme.typography.headlineMedium.copy(fontFamily = ff),
            headlineSmall = MaterialTheme.typography.headlineSmall.copy(fontFamily = ff),
            titleLarge = MaterialTheme.typography.titleLarge.copy(fontFamily = ff),
            titleMedium = MaterialTheme.typography.titleMedium.copy(fontFamily = ff),
            titleSmall = MaterialTheme.typography.titleSmall.copy(fontFamily = ff),
            bodyLarge = MaterialTheme.typography.bodyLarge.copy(fontFamily = ff),
            bodyMedium = MaterialTheme.typography.bodyMedium.copy(fontFamily = ff),
            bodySmall = MaterialTheme.typography.bodySmall.copy(fontFamily = ff),
            labelLarge = MaterialTheme.typography.labelLarge.copy(fontFamily = ff),
            labelMedium = MaterialTheme.typography.labelMedium.copy(fontFamily = ff),
            labelSmall = MaterialTheme.typography.labelSmall.copy(fontFamily = ff),
        )
    }

Also, give 1.7.0-dev1721 a try. It has this change: #4893 which might help a bit. (using kotlin = "2.0.20-Beta2")

Also, using rel=preload for the resources such as fonts might be beneficial: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload

from compose-multiplatform.

GuilhE avatar GuilhE commented on August 24, 2024 2

So, my final report:

Using rel="preload" and storing the fontFamiliy as a val inside the composable function improves significantly the performance. But combining it with 1.7.0-dev1721 removes any font lag.

Tested on the slowest physical device I have:

  • Allwinner T8100 (T8100C045298)
  • Android 10 API 29
  • Chrome 126.0.6478.71
  • Power save mode ON

No optimizations:

no-opt.mp4

Optimizations + 1.6.11:

1.6.11.mp4

Optimizations + 1.7.0-dev1721:

1.7.0-dev1721.mp4

@eymar I think we can close this issue and possible reopen if next Beta or RC breaks this, but I believe it would be very beneficial that this optimisations hints (especially the html one) could be documented.

from compose-multiplatform.

eymar avatar eymar commented on August 24, 2024 1

This is an example from a different app:

<link rel="preload" href="./composeResources/username.composeapp.generated.resources/font/FiraMono-Regular.ttf" as="fetch" type="font/ttf" crossorigin/>

from compose-multiplatform.

eymar avatar eymar commented on August 24, 2024

Anything that involves network requests (including fonts loading) will take some time to complete. Therefore, it can be barely noticeable if the connection is fast and the latency is low. But If the connection is not so fast, the UX is indeed affected.


What are the options to improve the situation:

  • There is an experimental Local Fonts API available only in Chrome: https://developer.mozilla.org/en-US/docs/Web/API/Local_Font_Access_API - so you might avoid the network request. Not all fonts will be available though (depends on the system).
  • Alternatively, it's worth to preload the fonts during the app initialization - before displaying the content.

Could you please point to the place in code where you currently do Fonts loading?

from compose-multiplatform.

GuilhE avatar GuilhE commented on August 24, 2024

The tests were conducted in the same network and the fonts are local resources in the project, therefore I believe the problem remains with hardware and possible code optimisations.

I'm using composeResources so I believe the loading operations are automatic:

from compose-multiplatform.

GuilhE avatar GuilhE commented on August 24, 2024

Nice tips, thanks!

val ff = helveticaNeueFontFamily // call getter only once, so the `fetch` wan't happen every time

✅ - this one shows significant improvements

Also, give 1.7.0-dev1721 a try. It has this change: #4893 which might help a bit. (using kotlin = "2.0.20-Beta2")

Also, using rel=preload for the resources such as fonts might be beneficial: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload

Where should I apply this one? The only <link> I have is <link rel="icon" type="image/x-icon" href="icon.png">

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Who'sNext</title>
        <link rel="icon" type="image/x-icon" href="icon.png">
        <script type="application/javascript" src="skiko.js"></script>
        <script type="application/javascript" src="browserApp.js"></script>
        <meta content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=3, user-scalable=yes" name="viewport">
    </head>
    <body>
        <canvas id="WhosNext"></canvas>
    </body>
</html>

from compose-multiplatform.

okushnikov avatar okushnikov commented on August 24, 2024

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

from compose-multiplatform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.