Giter VIP home page Giter VIP logo

Comments (5)

JakeWharton avatar JakeWharton commented on July 26, 2024

I don't think plain objects is the right answer because they still need to undergo JSON encoding. It'll be faster, since it's C code instead of JS-hosted Kotlin code, but it's still JSON encoding.

We can instead teach each Change subtype to JSON "encode" itself directly to a string and build up the JSON in a buildString directly, only calling out to kotlinx.serialization for user types like you said.

I mean look at what we generate:

"""["create",{"id":1,"tag":2}],""" +
"""["add",{"id":1,"tag":2,"childId":3,"index":4}],""" +
"""["move",{"id":1,"tag":2,"fromIndex":3,"toIndex":4,"count":5}],""" +
"""["remove",{"id":4,"tag":3,"index":2,"count":1}],""" +
"""["remove",{"id":1,"tag":2,"index":3,"count":4,"removedIds":[5,6,7,8]}],""" +
"""["modifier",{"id":1,"elements":[[1,{}],[2,3],[3,[]],[4],[5]]}],""" +
"""["property",{"id":1,"tag":2,"value":"hello"}],""" +
"""["property",{"id":1,"tag":2}]""" +

The Create class can get a member

public fun appendJsonTo(builder: StringBuilder) {
  builder.append("""["create",{"id":""")
  builder.append(id.value)
  builder.append(""","tag":""")
  builder.append(tag.value)
  builder.append("}]")
}

and we're done. Repeat for all the other subtypes.

Then with a custom serializer for List<Change> we can do the top-level buildString + comma insertion, and then write that as an unsafe raw string into the JsonEncoder.

An even more efficient version of this bypasses allocating any Change instances and simply builds the raw JSON string directly. Unfortunately this breaks the current API unless we do something crazy like

class RawJsonChange : Change {
  val json = StringBuidler()
}

and only have this single allocation per set of changes.

from redwood.

swankjesse avatar swankjesse commented on July 26, 2024

Doing JSON encoding in JS would be more efficient if we had a good StringBuilder on QuickJS. Sigh.

from redwood.

JakeWharton avatar JakeWharton commented on July 26, 2024

True. We could do a List and write them all sequentially to the underlying stream.

from redwood.

swankjesse avatar swankjesse commented on July 26, 2024

I used a sampling profiler to measure just how much time we’re spending in serialization code. For one sample I took it’s about 19% of the samples:

original

With this optimization implemented that drops down to about 3% of the samples:

optimized

from redwood.

JakeWharton avatar JakeWharton commented on July 26, 2024

Is this done?

from redwood.

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.