[whatwg/streams] Fix queue total size tracking logic (#661)

Previously, we believed that the spec's inefficient-but-clear method, of adding up all the chunk sizes whenever it wanted to get the total queue size, was equivalent to keeping a running count. This was discussed in #582; note also #491 where we changed the reference implementation to the running-count strategy for speed reasons. As discovered in https://github.com/whatwg/streams/issues/582#issuecomment-273016992, and further elaborated on in #657, these two methods are not in fact equivalent, due to the vagaries of floating-point arithmetic. (That is, they are equivalent if you assume the addition is floating-point addition, but that is less than clear from the spec.)

As such, this commit switches the spec to the more efficient running-count method, as that's realistically the only implementable version. It also adds tests to ensure floating point arithmetic is being used, exposing such cases.

This commit also includes a few unobservable cleanups:

- It introduces the ResetQueue abstract operation, to ensure that the queue total size gets reset to zero when the queue is cleared. This should not matter because no code paths check the queue's total size after it has been cleared, but keeping the two slots in sync seems virtuous.

- It updates the internal slot name for ReadableByteStreamController instances from [[totalQueuedBytes]] to [[queueTotalSize]], to be consistent with those for the other queue-containers. We do not yet use the queue-with-sizes abstract operations (except ResetQueue) on ReadableByteStreamController instances as the queue management is significantly more complicated there. But aligning makes the spec easier to read.

Closes #582. Closes #657.

---

Things to contemplate:

- I realized after making this whole change that we probably could have gotten away with simply reminding everyone that all arithmetic is floating point arithmetic. Unless I am very confused, I think the running-count methods and the sum-as-needed methods are equivalent as long as both use the same type of arithmetic. So really the only necessary part of this pull request is 

  ```diff
   +It's also worth noting that, as in [[!ECMASCRIPT]], all numbers are represented as double-precision floating point
   +values, and all arithmetic operations performed on them must be done in the usual way for such values.
   ```

- I would really like to unify the readable byte stream queue so that it uses the queue with sizes operations, instead of its own bespoke stuff. But I'll leave that for future work. There's no obvious way to do so that isn't at least a bit awkward for someone.
You can view, comment on, or merge this pull request online at:

  https://github.com/whatwg/streams/pull/661

-- Commit Summary --

  * Fix queue total size tracking logic

-- File Changes --

    M index.bs (143)
    M reference-implementation/lib/queue-with-sizes.js (46)
    M reference-implementation/lib/readable-stream.js (53)
    M reference-implementation/lib/writable-stream.js (26)
    A reference-implementation/to-upstream-wpts/readable-streams/floating-point-total-queue-size.https.html (14)
    A reference-implementation/to-upstream-wpts/readable-streams/floating-point-total-queue-size.js (86)
    A reference-implementation/to-upstream-wpts/writable-streams/floating-point-total-queue-size.https.html (14)
    A reference-implementation/to-upstream-wpts/writable-streams/floating-point-total-queue-size.js (69)

-- Patch Links --

https://github.com/whatwg/streams/pull/661.patch
https://github.com/whatwg/streams/pull/661.diff

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/streams/pull/661

Received on Wednesday, 18 January 2017 20:36:44 UTC