Skip to content

Analyzing spec algorithms #663

Open
Open
@tidoust

Description

@tidoust

Context: algorithms extraction

Reffy now extracts algorithms from specs, see algorithms extracts in Webref. Extraction is far from perfect (a number of limitations are noted as TODO comments in the extraction logic) but the results are still good enough to start analyzing potential problems in algorithms, see study-algorithms.js.

Also see @dontcallmedom's initial algorithms explorer based on the extracts.

Analysis: parallel steps that should call "queue a task"

First thing I analyzed are steps that run in parallel because I always forget the need to queue a task within parallel steps before resolving/rejecting a Promise or firing an event. @jyasskin noted the same problem back in 2022 in whatwg/html#8569. What's new here is that we can now compute that information automatically, giving us more weapons to play whack-a-mole.

The results show that about 50% of the specs (56/113) that define algorithms with "in parallel" steps need fixing:

Specs with steps in parallel that miss a call to "queue a task" (56 specs)

To run the analysis and refresh the above results, retrieve Webref's latest crawl results data locally and run:

npx strudy inspect [pathto]/webref --what algorithms --structure type/spec --sort default/title > res.md

On top of these problems (Edit: both examples have been fixed):

We could start reporting these problems to spec authors on a semi-automated basis (after review) as we do for broken links. Now, as noted by @jyasskin in whatwg/html#8569, "queue a task" means selecting a task source, which is also something that people get confused about (looking into it, I realize that I raised w3ctag/design-principles#38 on this topic back in 2016), and often choose to ignore.

Given the number of specs that get it wrong, two questions that I'm wondering about:

  1. Would it be better to consider less error prone ways to write such steps first?
  2. Do these problems materialize in interoperability bugs? Should we rather keep that in the back burner if it does not have practical implications?

Further analyses

Additional analyses that could be done:

  • Look into task sources as well. Whether a task source is defined is a bit harder to evaluate automatically because various specs have a blanket "use the foo task source for all tasks in this specification" statement; but the name of the task source is usually wrapped in a <dfn>, which should be easy to detect.
  • Report terms in steps that should link to their formal definition (e.g., "in parallel", "queue a task") and that don't.
  • Look into phrasing conventions when defining an algorithm and calling an algorithm, to progressively converge on a similar pseudo-language for algorithms. See how to define an algorithm in Infra for the definition part.
  • Start tracking algorithm variables, inputs and outputs. That probably requires settling down on stricter conventions first.
  • Analyze the list of step operations. In the extraction logic, the list is used to assess that a list item is indeed part of an algorithm. It reveals the many verbs that specs use to tell implementers what needs to be done. Some have formal definitions. The meaning of others is more fuzzy. Ideally, the meaning of all operations would be normatively defined somewhere.

Any other interesting or important analyses that could be worth looking into?

How to write/flag algorithms

More broadly speaking, algorithms are currently written with semi-formal structures. There are opportunities to converge on a more formal structure if that seems useful. Examples:

  • Infra describes how to define an algorithm. Specs use other variants here and there, such as "When the foo() method is invoked, run the following steps...", or algorithms sections where the sub-heading's title is the name of the algorithm.
  • Specs and spec authoring tools follow different conventions with regards to flagging these algorithms, e.g., with a class="algorithm" attribute. This makes it harder to extract the relevant prose, algorithm name, and actual steps.
  • Algorithm steps may be more or less atomic. Sometimes, a single step performs multiple operations (e.g., if/then/else all in one sentence). Related discussions about "inlining" steps in HTML in "in parallel" in algorithm headers can be confusing whatwg/html#10049

On top of us raising issues afterwards, spec authoring tools could perhaps better guide spec authors at the authoring step. Additional classes could also help make algorithms visually more readable, as attempted with flowchart symbols in the algorithms explorer.

In any case, convergence would make it easier to extract the algorithms and run further analyses.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions