Skip to content

Accessing strokeStyle and fillStyle of drawing context slow #2011

@wxs

Description

@wxs

Nature of issue?

  • Found a bug
  • Existing feature enhancement
  • New feature request

Most appropriate sub-area of p5.js?

  • Color
  • Core
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL

Which platform were you using when you encountered this?

  • Mobile/Tablet (touch devices)
  • Desktop/Laptop
  • Others (specify if possible)

Details about the bug:

  • p5.js version: 0.5.11
  • Web browser and it's version: Chrome Version 61.0.3128.0 (Official Build) dev (64-bit)
  • Operating System: OS X 10.11.6
  • Steps to reproduce this: Repeatedly call "vertex"

In profiling a sketch I'm working on that makes heavy use of vertex, I found that calls were quite slow, and specifically due to the calls internally to _getStroke and _getFill (see screenshot).

screen shot 2017-06-20 at 13 33 54

It seems that its slow for it to access the fillStyle and strokeStyle of the drawing context. Although I don't know why this would be the case, I found that by locally caching these values, and then updating the cache in every place where they are set, performance improves significantly:

p5.Renderer2D.prototype._getFill = function(){
  return this.cachedFillStyle;
};

p5.Renderer2D.prototype._setFill = function(fillStyle){
    this.drawingContext.fillStyle = fillStyle
    this.cachedFillStyle = this.drawingContext.fillStyle;
};

p5.Renderer2D.prototype._getStroke = function(){
  return this.cachedStrokeStyle
};

p5.Renderer2D.prototype._setStroke = function(strokeStyle){
    this.drawingContext.strokeStyle = strokeStyle
    this.cachedStrokeStyle = this.drawingContext.strokeStyle;
}

this will, of course, fail if someone tries to modify the stroke or fill style outside of these helper functions. There may be another solution by modifying the part of the vertex that calls this.

    vert[5] = this._renderer._getFill();
    vert[6] = this._renderer._getStroke();

If the helper function-based approach looks good, I can probably package up my changes as a PR. Let me know!

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