dart-lang / macros

A Dart mono-repo for macro development.
BSD 3-Clause "New" or "Revised" License
20 stars 2 forks source link

Add LazyMap, JsonBuffer and tests. #4

Closed davidmorgan closed 1 month ago

davidmorgan commented 1 month ago

We don't actually need to use this yet, so the implementation details are not super important.

But it is important that we leave room to build the JSON data in an efficient way, I figured having this merged would help.

The LazyMap is the important piece: if you put data in a LazyMap it can be written efficiently to the buffer, because the number of keys is known so the map can be written eagerly meaning the buffer is ready to receive the actual keys+values as they are computed.

When I was working on the tree diffing code I realized that unfortunately a tree diff can't be created as a LazyMap. The diff algorithm has no way to compute the keys near the root before it finishes computing some leaves. So I think for quickly serializing tree diffs we'll need something smarter: a different type of LazyMap that can emit an Iterable<(String, Object?)> as it discovers keys and values. Fortunately, I don't think that complexity will leak into the rest of the data model so it should not be needed for code that is just building models.