Skip to content

Commit

Permalink
much faster caching mechanism
Browse files Browse the repository at this point in the history
which generates 100x smaller cache keys, making program lookups much
faster
  • Loading branch information
mourner committed Nov 5, 2016
1 parent c04f3e7 commit 957c467
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
3 changes: 1 addition & 2 deletions js/data/array_group.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ class ArrayGroup {
for (const layer of layers) {
const programConfiguration = ProgramConfiguration.createDynamic(
programInterface.paintAttributes || [], layer, zoom);
const PaintVertexArrayType = programConfiguration.paintVertexArrayType();
this.layerData[layer.id] = {
layer: layer,
programConfiguration: programConfiguration,
paintVertexArray: new PaintVertexArrayType()
paintVertexArray: new programConfiguration.PaintVertexArray()
};
}

Expand Down
16 changes: 8 additions & 8 deletions js/data/program_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ProgramConfiguration {
this.interpolationUniforms = [];
this.vertexPragmas = {};
this.fragmentPragmas = {};
this.cacheKey = '';
}

static createDynamic(attributes, layer, zoom) {
Expand All @@ -44,8 +45,7 @@ class ProgramConfiguration {
self.addDataAndZoomDrivenAttribute(name, attribute, layer, zoom);
}
}

self.cacheKey = JSON.stringify([self.vertexPragmas, self.fragmentPragmas]);
self.PaintVertexArray = createVertexArrayType(self.attributes);

return self;
}
Expand All @@ -56,8 +56,6 @@ class ProgramConfiguration {
for (const name of uniformNames) {
self.addUniform(name, `u_${name}`);
}
self.cacheKey = JSON.stringify(self.fragmentPragmas);

return self;
}

Expand All @@ -70,6 +68,8 @@ class ProgramConfiguration {

frag.initialize.push(`{precision} {type} ${name} = ${inputName};`);
vert.initialize.push(`{precision} {type} ${name} = ${inputName};`);

this.cacheKey += `/u_${name}`;
}

addZoomDrivenAttribute(name, attribute) {
Expand All @@ -88,6 +88,8 @@ class ProgramConfiguration {

vert.define.push(`attribute {precision} {type} ${attribute.name};`);
vert.initialize.push(`${name} = ${attribute.name} / ${attribute.multiplier}.0;`);

this.cacheKey += `/a_${name}`;
}

addDataAndZoomDrivenAttribute(name, attribute, layer, zoom) {
Expand Down Expand Up @@ -143,6 +145,8 @@ class ProgramConfiguration {
}
vert.initialize.push(`${name} = evaluate_zoom_function_${attribute.components}(\
${componentNames.join(', ')}, ${tName}) / ${attribute.multiplier}.0;`);

this.cacheKey += `/z_${name}`;
}

getFragmentPragmas(name) {
Expand Down Expand Up @@ -182,10 +186,6 @@ class ProgramConfiguration {
}
}

paintVertexArrayType() {
return createVertexArrayType(this.attributes);
}

setUniforms(gl, program, layer, globalProperties) {
for (const uniform of this.uniforms) {
const value = layer.getPaintValue(uniform.property, globalProperties);
Expand Down
2 changes: 1 addition & 1 deletion js/render/painter.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ class Painter {

_createProgramCached(name, programConfiguration) {
this.cache = this.cache || {};
const key = `${name}${programConfiguration.cacheKey}${!!this._showOverdrawInspector}`;
const key = `${name}${programConfiguration.cacheKey || ''}/${!!this._showOverdrawInspector}`;
if (!this.cache[key]) {
this.cache[key] = this.createProgram(name, this._showOverdrawInspector, this.gl, programConfiguration);
}
Expand Down

0 comments on commit 957c467

Please sign in to comment.