Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fold compound expressions with literal arguments into literals #5279

Merged
merged 5 commits into from
Sep 18, 2017

Conversation

anandthakker
Copy link
Contributor

@anandthakker anandthakker commented Sep 13, 2017

When parsing an expression, if its arguments are all Literals, then evaluate it immediately and yield a Literal instead.

Yields a modest perf improvement in tile_layout_dds:

  • before: 51.37629 - 52.20937 ms / iteration
  • after: 47.81951 - 49.86712 ms / iteration

More importantly, this means we get validation errors at compile time rather than evaluation time for the common case of color curves defined like [curve, [linear], [zoom], 0, "red", 10, "bleu mispelled"]

benchmark master 2200752 expressions-fold-constant 0567a8f
map-load 111 ms 119 ms
style-load 128 ms 168 ms
buffer 1,167 ms 1,120 ms
tile_layout_dds 1,329 ms 1,255 ms
fps 60 fps 60 fps
frame-duration 7.1 ms, 1% > 16ms 6.9 ms, 2% > 16ms
query-point 0.98 ms 1.24 ms
query-box 73.43 ms 75.72 ms
geojson-setdata-small 17 ms 0 ms
geojson-setdata-large 88 ms 175 ms
filter Done Done

@anandthakker
Copy link
Contributor Author

anandthakker commented Sep 13, 2017

Example generated code for a color curve with "red" as the stop output value (excerpt of the curve/linear-color integration test output)

before

function e4() {   return "red" }
var v5 = [e4];
function e6() {   return $this.toColor(v5) }

after

var v4 = (new $this.Color(1, 0, 0, 1));
function e5() {   return v4 }

@mourner
Copy link
Member

mourner commented Sep 13, 2017

Very cool!

@anandthakker anandthakker force-pushed the expressions-fold-constant branch from 31a20a7 to 0567a8f Compare September 15, 2017 15:09
@anandthakker
Copy link
Contributor Author

Rebased, generalized, and updated benchmarks.

@@ -13,7 +13,6 @@ const {
toString,
checkSubtype} = require('./types');
const {Color, typeOf} = require('./values');
const Curve = require('./definitions/curve');
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the reason for these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Breaking a new dependency cycle, which was introduced by parse_expression now depending on evaluation_context. I guess it's better to break it there instead (especially since we're already doing so in that module)

}

if (typeof this.value === 'object' && this.value !== null) {
const v = ctx.addVariable(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: return ctx.addVariable(value);

@@ -68,4 +87,21 @@ function parseExpression(expr: mixed, context: ParsingContext): ?Expression {
}
}

const nonConstantExpressions = [ 'error', 'get', 'has', 'properties', 'id', 'geometry-type', 'zoom' ];
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks similar to isFeatureConstant/isZoomConstant. Can they share more code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 I almost did that, and then stopped myself because I thought I was prematurely abstracting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, actually the code itself is a bit different. isConstant only looks one level deep (because that's sufficient for the way we're using it). We could consolidate the base case code, though

@anandthakker anandthakker force-pushed the expressions-fold-constant branch from 0567a8f to c027323 Compare September 16, 2017 00:21
@anandthakker
Copy link
Contributor Author

@jfirebaugh updated to address your notes/questions

@anandthakker anandthakker merged commit 6ab53dc into master Sep 18, 2017
@anandthakker anandthakker deleted the expressions-fold-constant branch September 18, 2017 18:03
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