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

Optimize vsync #2451

Merged
merged 1 commit into from
Mar 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/service/fixed-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class FixedLayer {
}
});
},
}).catch(error => {
}, {}).catch(error => {
// Fail silently.
setTimeout(() => {throw error;});
});
Expand Down
2 changes: 1 addition & 1 deletion src/service/resources-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ export class Resources {
(newScrollHeight - state./*OK*/scrollHeight));
}
},
});
}, {});
}
}
}
Expand Down
77 changes: 55 additions & 22 deletions src/service/vsync-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,40 @@ export class Vsync {
*/
this.tasks_ = [];

/**
* Double buffer for tasks.
* @private {!Array<!VsyncTaskSpecDef>}
*/
this.nextTasks_ = [];

/**
* States for tasks in the next frame in the same order.
* @private {!Array<!VsyncStateDef>}
*/
this.states_ = [];

/**
* Double buffer for states.
* @private {!Array<!VsyncStateDef>}
*/
this.nextStates_ = [];

/**
* Whether a new animation frame has been scheduled.
* @private {boolean}
*/
this.scheduled_ = false;

/**
* @private {?Promise}
*/
this.nextFramePromise_ = null;

/**
* @private {?function()}
*/
this.nextFrameResolver_ = null;

/** @const {!Function} */
this.boundRunScheduledTasks_ = this.runScheduledTasks_.bind(this);

Expand All @@ -103,11 +125,11 @@ export class Vsync {
* will be undefined.
*
* @param {!VsyncTaskSpecDef} task
* @param {!VsyncStateDef=} opt_state
* @param {VsyncStateDef=} opt_state
*/
run(task, opt_state) {
this.tasks_.push(task);
this.states_.push(opt_state || {});
this.states_.push(opt_state);
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a sizable change to the API. I'd expect something would break. I definitely see some places where default opt_state is used.

Copy link
Member Author

Choose a reason for hiding this comment

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

The documentation states otherwise. No tests fail. Do you have a pointer?

Copy link
Member Author

Choose a reason for hiding this comment

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

I found 2 places. Fixed them up.

This API sucks. Should have just let measure return an object.

this.schedule_();
}

Expand All @@ -123,16 +145,12 @@ export class Vsync {
* @return {!Promise}
*/
runPromise(task, opt_state) {
return new Promise(resolve => {
this.run({
measure: state => {
task.measure(state);
},
mutate: state => {
task.mutate(state);
resolve();
},
}, opt_state);
this.run(task, opt_state);
if (this.nextFramePromise_) {
return this.nextFramePromise_;
}
return this.nextFramePromise_ = new Promise(resolve => {
this.nextFrameResolver_ = resolve;
});
}

Expand All @@ -152,7 +170,10 @@ export class Vsync {
* @param {function()} mutator
*/
mutate(mutator) {
this.run({mutate: mutator});
this.run({
measure: undefined, // For uniform hidden class.
mutate: mutator,
});
}

/**
Expand All @@ -161,11 +182,9 @@ export class Vsync {
* @return {!Promise}
*/
mutatePromise(mutator) {
return new Promise(resolve => {
this.mutate(() => {
mutator();
resolve();
});
return this.runPromise({
measure: undefined,
mutate: mutator,
});
}

Expand All @@ -174,7 +193,10 @@ export class Vsync {
* @param {function()} measurer
*/
measure(measurer) {
this.run({measure: measurer});
this.run({
measure: measurer,
mutate: undefined, // For uniform hidden class.
});
}

/**
Expand Down Expand Up @@ -294,11 +316,14 @@ export class Vsync {
*/
runScheduledTasks_() {
this.scheduled_ = false;
// TODO(malteubl) Avoid array allocation with a double buffer.
const tasks = this.tasks_;
const states = this.states_;
this.tasks_ = [];
this.states_ = [];
const resolver = this.nextFrameResolver_;
this.nextFrameResolver_ = null;
this.nextFramePromise_ = null;
// Double buffering
this.tasks_ = this.nextTasks_;
this.states_ = this.nextStates_;
for (let i = 0; i < tasks.length; i++) {
if (tasks[i].measure) {
tasks[i].measure(states[i]);
Expand All @@ -309,6 +334,14 @@ export class Vsync {
tasks[i].mutate(states[i]);
}
}
// Swap last arrays into double buffer.
this.nextTasks_ = tasks;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand the point of having nextTasks_ if we just reassign to tasks and truncate.

Copy link
Member Author

Choose a reason for hiding this comment

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

this.tasks_ can be modified during the loop above, so unless you want want to use a weird splice magic, you need 2 arrays.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ha, I totally read this backwards (as in you're truncating the old nextTasks_, not the new). Never mind.

this.nextStates_ = states;
this.nextTasks_.length = 0;
this.nextStates_.length = 0;
if (resolver) {
resolver();
}
}

/**
Expand Down