Closed Bug 1106137 Opened 10 years ago Closed 10 years ago

Make canvas debugger faster by avoiding a double-copy operation when deserializing image data

Categories

(DevTools Graveyard :: Canvas Debugger, defect)

x86
macOS
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED
Firefox 37

People

(Reporter: vporof, Assigned: vporof)

Details

Attachments

(2 files, 1 obsolete file)

      No description provided.
Attached patch avoid-double-copy.patch (obsolete) — Splinter Review
Assignee: nobody → vporof
Status: NEW → ASSIGNED
Explaining this seemingly weird mess of array buffer constructors:
Previous behavior:
1. make an array buffer (representing pixels) in the actor
2. manipulate its 32-bit view somehow (one pixel at a time)
3. pass that 32-bit view to the protocol's serializer - since array buffer views can't be properly stringified as JSON, manually join the numbers with commas to represent a plain array
4. deserialize it on the client as plain array and create a 32-bit view from it
5. create an ImageData object
6. copy the previously created 32-bit view's buffer into the ImageData's 8-bit view's buffer

A keen observer would realize that this involves one extra redundant copy operation: step 4 vs. 6. The new approach is:

1. make an array buffer (representing pixels) in the actor
2. manipulate its 32-bit view somehow (one pixel at a time)
3. pass an 8-bit view of that array buffer to the protocol's serializer
4. deserialize it on the client as plain array
5. create an ImageData object
6. copy the previously deserialized array representing an 8-bit view into the ImageData's 8-bit view's buffer

This saves us a very costly copy operation, at the expense of serializing 4 times as many numbers. This is a good exchange.

Obviously, it'd still be better if protocol.js could just "hand over" the array buffer without serializing one of its views, but what we have right now is still mostly ok.
Attachment #8530390 - Attachment is obsolete: true
Attachment #8530449 - Flags: review?(pbrosset)
Comment on attachment 8530449 [details] [diff] [review]
avoid-double-copy.patch

Review of attachment 8530449 [details] [diff] [review]:
-----------------------------------------------------------------

::: toolkit/devtools/server/actors/canvas.js
@@ +62,2 @@
>    write: (v) => "[" + Array.join(v, ",") + "]",
> +  read: (v) => JSON.parse(v)

To be more precise, this was the redundant copy. Creating an array buffer view from a regular js array (or another view) involves a copy, as opposed to creating it from a buffer, which does not.
Attached patch v2Splinter Review
With text fixes.
Attachment #8532589 - Flags: review?(pbrosset)
Attachment #8530449 - Flags: review?(pbrosset)
Comment on attachment 8532589 [details] [diff] [review]
v2

Review of attachment 8532589 [details] [diff] [review]:
-----------------------------------------------------------------

This all looks good to me.
I'd like to see a comment (something general at the top of canvas.js) that explains the approach described in comment 2.
Attachment #8532589 - Flags: review?(pbrosset) → review+
Added comment and landed: https://hg.mozilla.org/integration/fx-team/rev/8a6b14d8320e
Whiteboard: [fixed-in-fx-team]
https://hg.mozilla.org/mozilla-central/rev/8a6b14d8320e
Status: ASSIGNED → RESOLVED
Closed: 10 years ago
Flags: in-testsuite+
Resolution: --- → FIXED
Whiteboard: [fixed-in-fx-team]
Target Milestone: --- → Firefox 37
Product: Firefox → DevTools
Product: DevTools → DevTools Graveyard
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: