liveBook · Manning

Structural diff algorithm

As explained in Data-Oriented Programming: Reduce software complexity

An algorithm that recursively compares structured maps and collections to represent differences while ignoring map field order.

A structural diff algorithm recursively compares two nested maps and records how the second differs from the first without depending on map-field order. It supports replacement and addition, while deletion remains deliberately unsupported. For arrays, it compares elements by position and returns the changed second value. section 5.4 section 5.6

The algorithm reduces reconciliation to a small set of structural cases:

  • Recurse through corresponding map fields, taking the union of keys from both maps. section 5.4
  • Return only changed branches: replacements contain the second value, additions contain newly present fields, and equal branches disappear from the result. section 5.4 section 5.6
  • Compare arrays in order; unchanged arrays produce no diff, while changed positions carry values from the second array. section 5.4

Finding concurrent state changes

Suppose a library state changes a book's publication year, then changes its title and an author's name. Comparing the original state with the first update yields only the nested publication-year path; comparing it with the later state yields the title and author-name paths. Those paths can reveal whether two mutations overlap before applying a patch. section 5.4 section 5.6

Table 5.3 Kinds of structural differences between maps without nested fields (view table figure)

Kind

First map

Second map

Diff

Replacement

{"a": 1}

{"a": 2}

{"a": 2}

Addition

{"a": 1}

{"a": 1, "b": 2}

{"b": 2}

Deletion

{"a": 1, "b": 2}

{"a": 1}

Not supported

Use the difference table to compare replacement, addition, and unsupported deletion for flat maps before following the recursive nested-map implementation. section 5.4

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Descends from flat-map cases through nested recursion and array handling, but stops short of implementing deletions.
Examples
Uses flat and nested map tables, positional arrays, a library state, changed information paths, and applied patches.
What it assumes
Assumes hash maps, arrays, recursion, immutable state, and structural sharing, while explaining the comparison cases from flat maps upward.
What you can do after
Build it from scratch to compare nested JavaScript objects, extract changed paths, detect overlap, and apply supported patches.
How it connects
Connects structural sharing, immutable data, reconciliation, concurrent mutations, information paths, and patches.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.