weixin_39547158
2020-12-08 19:19 阅读 0

Reuse contexts on `ImageData` objects

Reuse the existing canvas on ImageData objects instead of creating a new one each for each operation.

For operations that only require a canvas temporarily, use have a static canvas that we reuse over and over again rather than create one per operation.

This definitely reduces the number of times we create canvases but I have to do some memory profiling to see if it actually reduces the amount of memory we're using.

该提问来源于开源项目:mozilla/pluotsorbet

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • weixin_39845039 weixin_39845039 2020-12-08 19:19

    Probably memory will be allocated when you set the width/height properties, deallocated when you set them to 0. If this is true, we'll use the same amount of memory as before.

    About perf, do you think using the same canvas and setting its width/height is going to be faster than creating new canvases?

    点赞 评论 复制链接分享
  • weixin_39547158 weixin_39547158 2020-12-08 19:19

    Probably memory will be allocated when you set the width/height properties, deallocated when you set them to 0. If this is true, we'll use the same amount of memory as before.

    Yeah; if the old canvases are garbage-collected right away after the ImageData no longer holds a reference to them, then this patch won't save us memory. Intuitively it seems like reusing contexts will reduce the potential for leaking canvases and contexts, but I don't know how much of an issue this is in practice.

    About perf, do you think using the same canvas and setting its width/height is going to be faster than creating new canvases?

    It'll definitely create less work for the garbage collector, and we get to avoid function calls in the ImageDataFactory methods, but this PR also converts some ImageData methods to hand-written JS, which I'm told is slower than compiled Java. With the patch applied there's no noticeable performance difference so I'll have to profile to determine whether perf has been meaningfully affected

    点赞 评论 复制链接分享
  • weixin_39845039 weixin_39845039 2020-12-08 19:19

    Yeah; if the old canvases are garbage-collected right away after the ImageData no longer holds a reference to them, then this patch won't save us memory. Intuitively it seems like reusing contexts will reduce the potential for leaking canvases and contexts, but I don't know how much of an issue this is in practice.

    I suspect we're going to release the memory eventually anyway, but this PR will most probably release it earlier (as soon as we set width/height to 0).

    点赞 评论 复制链接分享
  • weixin_39845039 weixin_39845039 2020-12-08 19:19

    It'll definitely create less work for the garbage collector, and we get to avoid function calls in the ImageDataFactory methods, but this PR also converts some ImageData methods to hand-written JS, which I'm told is slower than compiled Java. With the patch applied there's no noticeable performance difference so I'll have to profile to determine whether perf has been meaningfully affected

    Yeah, I think we could obtain the same effect (reuse contexts) without creating a custom ImageData.

    点赞 评论 复制链接分享

相关推荐