-
Notifications
You must be signed in to change notification settings - Fork 568
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
Fix timer requests in update and lifecycle. #964
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2020 The xi-editor Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! Miscellaneous utility functions. | ||
|
||
use std::collections::HashMap; | ||
use std::hash::Hash; | ||
use std::mem; | ||
|
||
/// Fast path for equal type extend + drain. | ||
pub trait ExtendDrain { | ||
/// Extend the collection by draining the entries from `source`. | ||
/// | ||
/// This function may swap the underlying memory locations, | ||
/// so keep that in mind if one of the collections has a large allocation | ||
/// and it should keep that allocation. | ||
fn extend_drain(&mut self, source: &mut Self); | ||
} | ||
|
||
impl<K, V> ExtendDrain for HashMap<K, V> | ||
where | ||
K: Eq + Hash + Copy, | ||
V: Copy, | ||
{ | ||
// Benchmarking this vs just extend+drain with a 10k entry map. | ||
// | ||
// running 2 tests | ||
// test bench_extend ... bench: 1,971 ns/iter (+/- 566) | ||
// test bench_extend_drain ... bench: 0 ns/iter (+/- 0) | ||
fn extend_drain(&mut self, source: &mut Self) { | ||
if !source.is_empty() { | ||
if self.is_empty() { | ||
// If the target is empty we can just swap the pointers. | ||
mem::swap(self, source); | ||
} else { | ||
// Otherwise we need to fall back to regular extend-drain. | ||
self.extend(source.drain()); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,12 +176,15 @@ impl<T, W: Widget<T>> Scroll<T, W> { | |
} | ||
|
||
/// Makes the scrollbars visible, and resets the fade timer. | ||
pub fn reset_scrollbar_fade(&mut self, ctx: &mut EventCtx, env: &Env) { | ||
pub fn reset_scrollbar_fade<F>(&mut self, request_timer: F, env: &Env) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, this is annoying. Something I've been wondering is whether we could have certain traits for behaviour that is common to (most?) contexts, and then for things like requesting a timer or sending a command you could have the argument be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeap, I was thinking of the same thing based on the amount of code duplication we have in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am accidentally working on this right now. |
||
where | ||
F: FnOnce(Duration) -> TimerToken, | ||
{ | ||
// Display scroll bars and schedule their disappearance | ||
self.scrollbars.opacity = env.get(theme::SCROLLBAR_MAX_OPACITY); | ||
let fade_delay = env.get(theme::SCROLLBAR_FADE_DELAY); | ||
let deadline = Duration::from_millis(fade_delay); | ||
self.scrollbars.timer_id = ctx.request_timer(deadline); | ||
self.scrollbars.timer_id = request_timer(deadline); | ||
} | ||
|
||
/// Returns the current scroll offset. | ||
|
@@ -344,7 +347,7 @@ impl<T: Data, W: Widget<T>> Widget<T> for Scroll<T, W> { | |
|
||
if !scrollbar_is_hovered { | ||
self.scrollbars.hovered = BarHoveredState::None; | ||
self.reset_scrollbar_fade(ctx, env); | ||
self.reset_scrollbar_fade(|d| ctx.request_timer(d), env); | ||
} | ||
} | ||
_ => (), // other events are a noop | ||
|
@@ -395,7 +398,7 @@ impl<T: Data, W: Widget<T>> Widget<T> for Scroll<T, W> { | |
// if we have just stopped hovering | ||
if self.scrollbars.hovered.is_hovered() && !scrollbar_is_hovered { | ||
self.scrollbars.hovered = BarHoveredState::None; | ||
self.reset_scrollbar_fade(ctx, env); | ||
self.reset_scrollbar_fade(|d| ctx.request_timer(d), env); | ||
} | ||
} | ||
Event::Timer(id) if *id == self.scrollbars.timer_id => { | ||
|
@@ -412,24 +415,29 @@ impl<T: Data, W: Widget<T>> Widget<T> for Scroll<T, W> { | |
if self.scroll(mouse.wheel_delta, size) { | ||
ctx.request_paint(); | ||
ctx.set_handled(); | ||
self.reset_scrollbar_fade(ctx, env); | ||
self.reset_scrollbar_fade(|d| ctx.request_timer(d), env); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &T, env: &Env) { | ||
// Guard by the timer id being invalid, otherwise the scroll bars would fade | ||
// immediately if some other widgeet started animating. | ||
if let LifeCycle::AnimFrame(interval) = event { | ||
if self.scrollbars.timer_id == TimerToken::INVALID { | ||
// Animate scroll bars opacity | ||
let diff = 2.0 * (*interval as f64) * 1e-9; | ||
self.scrollbars.opacity -= diff; | ||
if self.scrollbars.opacity > 0.0 { | ||
ctx.request_anim_frame(); | ||
match event { | ||
LifeCycle::AnimFrame(interval) => { | ||
// Guard by the timer id being invalid, otherwise the scroll bars would fade | ||
// immediately if some other widgeet started animating. | ||
if self.scrollbars.timer_id == TimerToken::INVALID { | ||
// Animate scroll bars opacity | ||
let diff = 2.0 * (*interval as f64) * 1e-9; | ||
self.scrollbars.opacity -= diff; | ||
if self.scrollbars.opacity > 0.0 { | ||
ctx.request_anim_frame(); | ||
} | ||
} | ||
} | ||
// Show the scrollbars any time our size changes | ||
LifeCycle::Size(_) => self.reset_scrollbar_fade(|d| ctx.request_timer(d), &env), | ||
_ => (), | ||
} | ||
self.child.lifecycle(ctx, event, data, env) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just an observation: this is the kind of thing I'm always curious to profile, or at least take a look at the asm; always room for funny surprises. I certainly suspect its an improvement, but it is a suspicion, it's not impossible the compiler generating code like this as an optimization already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a funny surprise alright. The empty target case is 0ns with this optimized function, which is a win. However unexpectedly even the slow path is faster! The slow path is faster only with
drain
though. If I bench extending with justiter
vsiter
, then the slow paths are equal. Some sort of non-generic code win I guess.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
neat, thanks for measuring!