Skip to content

Improve speed of http backend by avoiding data copy amd using '420' subsampling - #274

Merged
almarklein merged 14 commits into
mainfrom
faster-http
Dec 1, 2026
Merged

almarklein merged 14 commits into
mainfrom
faster-http

Conversation

@almarklein

@almarklein almarklein commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

What this PR does

Architectual changes:

  • For bitmap present, introduce internal API for 'encoding params', supporting (for now) array, png and jpeg.
  • Remove references that suggest _rc_present_xx for future present methods. That is solved through 'encodings'.

Practical changes:

  • The http backend uses the new jpeg encoder, so the encoding happens on the mapped data, saving one data copy.
  • The subsampling is changed from '444' to '420'.
  • The http backend uses just jpeg. No trying to use png when no new frames. No fallback to png (which makes it much slower).

Prior and future work

  • The basic plumbing was already in place. This PR improves and documents it.
  • Other canvas backends can make use of this, like the anywidget backend. Will do that in a next pr.
  • We can also introduce more encodings, in particular encoding on the GPU, which will further improve the performance.

Benchmarks

So it looks like avooding a data-copy shaves of a few ms, while moving from 444 to 420 yuv subsampling has the biggest effect. I also looked into reducing the quality, but that does not have such a great performance improvement, while at some point artifacts become visible. From what i found, 75-80 is a good default.

On my laptop screen:

what fps ms
main 50 20
canvas encodes, 444 (like main) 50 20
canvas encodes, 420 70 14
context encodes, 444 52 19
context encodes, 420 71 14

On a 4k screen (canvas size close to 4k):

what fps ms
canvas encodes, 444 27 37
canvas encodes, 420 41 24
context encodes, 444 29 34
context encodes, 420 43 22

Moving from 27 fps to 43 fps also means a big difference in the perceived delay in interactive applications.

@almarklein almarklein changed the title Improve speed of http backend by avoiding data copy Improve speed of http backend by avoiding data copy amd using '420' subsampling Nov 30, 2026
@almarklein
almarklein marked this pull request as ready for review November 30, 2026 11:56
@almarklein
almarklein requested a review from clewis7 November 30, 2026 12:47
@kushalkolar

Copy link
Copy Markdown
Contributor

This is great with the 420 encoding!

self._present_params = {}
# Params that steer the present process (only used for bitmap present). This is
# the "encoding" field of the present info dict, kept by reference, so the canvas
# can update it directly at runtime.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this only is used by the bitmap context. could it be moved to BitmapContext?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not it's only used by the bitmap present method, so by BitmapContextToBitmap and WgpuContextToBitmap. I get the confusion though :)

@almarklein almarklein Dec 1, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So a canvas can add this subdict as part of the result of _rc_get_present_info(), e.g.:

return {
                "method": "bitmap",
                "formats": ["rgba-u8"],
                "encoding": self._encoding_params,
            }

And then it can change the encoding at runtime. Like dynamically lower the quality etc.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My.type checker complainted that this could be None

encoding = encoding_params["codec"]

So I was wondering if I could find such a situation, and if not why it still complained. Maybe the solution would be a ToBitmap mixin but that feels convoluted too

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The encoding params are not None if present_info["method"] == "bitmap", which is always the case here. But the type checker cannot see that I guess. If we want to please mypy we can define the type, maybe?

@Vipitis Vipitis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one question but not an issue.

looks good to me

Comment thread examples/cube_http.py Resolved
@Vipitis

Vipitis commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

as a sorta torture test I tried this with my spectral path tracer and it isn't too bad.

(not quite same angle or same sample count, and the screenshot probably compressed it again)

image

@almarklein
almarklein merged commit 91cd731 into main Dec 1, 2026
29 checks passed
@almarklein
almarklein deleted the faster-http branch December 1, 2026 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants