Code Monkey home page Code Monkey logo

Comments (2)

romainguy avatar romainguy commented on May 13, 2024

bundleOf() does not make Bundle().apply{} irrelevant. The extra allocations created by the Pair should not have a meaningful impact on apps unless they store thousands of items in bundles (which would indicate other issues). Short-lived allocations like this are also fairly well optimized out in N and O.

from android-ktx.

JakeWharton avatar JakeWharton commented on May 13, 2024

(Whoops I had a reply to this in a tab and forgot to hit enter.)

The full transformation with all syntactic niceties becomes:

bundleOf(
    KEY_ANSWER_ONE to answerOne?.text?.toString(),
    KEY_ANSWER_TWO to answerTwo?.text?.toString())

This syntax matches that of the various *Of factories in the stdlib which take pairs (e.g., mapOf, mutableMapOf, and our provided arrayMapOf).

One advantage to the allocation here is that the underlying collection can be pre-sized to exactly fit. This avoids it always starting empty, going through an allocation to 4 slots, and then having to double (based on how many elements you add).

Allocations like this wind up in many APIs. Hopefully someday the compiler will be able to see through the varargs and pair allocations someday. Or we could try getting the optimizations into D8, R8, or even ART.

The runtime check is the most unfortunate part, but as of yet there's no way to optimize it. We can bundle lint checks for it, though.

from android-ktx.

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.