-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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 StyleBoxFlat.draw()
#94240
Optimize StyleBoxFlat.draw()
#94240
Conversation
5c16722
to
aa89d1f
Compare
cc @davthedev |
aa89d1f
to
29767ff
Compare
I'd suggest using |
29767ff
to
f2dc57c
Compare
Not too familiar with pointers, but I think I got it? I got slightly better measures, but I don't think they are statistically-significant |
2cf9bec
to
13902ca
Compare
Changed the title as I discovered a notable speedup in drawing styleboxes without rounded corners too. |
I expected that some recent optimizations, like the Vector.insert() optimization, might change the benchmark. But I still measure the same 1.9x speedup for rounded styleboxes and 1.2x for all others. |
13902ca
to
71ac6b7
Compare
real_t quarter_arc_rad = Math_PI / 2.0; | ||
Point2 style_rect_center = style_rect.get_center(); | ||
|
||
int colors_size = colors.size(); | ||
int verts_size = verts.size(); | ||
int new_verts_amount = (adapted_corner_detail + 1) * (draw_border ? 8 : 4); |
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.
real_t quarter_arc_rad = Math_PI / 2.0; | |
Point2 style_rect_center = style_rect.get_center(); | |
int colors_size = colors.size(); | |
int verts_size = verts.size(); | |
int new_verts_amount = (adapted_corner_detail + 1) * (draw_border ? 8 : 4); | |
const real_t quarter_arc_rad = Math_PI / 2.0; | |
const Point2 style_rect_center = style_rect.get_center(); | |
const int colors_size = colors.size(); | |
const int verts_size = verts.size(); | |
const int new_verts_amount = (adapted_corner_detail + 1) * (draw_border ? 8 : 4); |
For consistency.
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.
Haven't benchmarked myself, but I believe you :) Great work!
While some aspects slightly impact readability, the performance boost is significant enough to justify the trade-off I think. I've played around with StyleBoxFlat
s for quite a bit and haven't observed any regressions.
verts.push_back(Vector2(x + x_skew, y + y_skew)); | ||
colors.push_back(color); | ||
const real_t pt_angle = (corner_idx + detail / (double)adapted_corner_detail) * quarter_arc_rad + Math_PI; | ||
const real_t angle_cosine = cos(pt_angle); |
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.
const real_t angle_cosine = cos(pt_angle); | |
const real_t angle_cosine = Math::cos(pt_angle); |
colors.push_back(color); | ||
const real_t pt_angle = (corner_idx + detail / (double)adapted_corner_detail) * quarter_arc_rad + Math_PI; | ||
const real_t angle_cosine = cos(pt_angle); | ||
const real_t angle_sine = sin(pt_angle); |
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.
const real_t angle_sine = sin(pt_angle); | |
const real_t angle_sine = Math::sin(pt_angle); |
Belated thanks! And oops I merged by mistake, I intended to wait for style fixes first. Can be done in a follow-up :) |
I'm not great at optimizing, but I needed this... I don't know which changes contributed most to the speedup, but resizing beforehand and the unfolding of the last nested loop seem like the primary two.
Benchmarks:
On production builds, on 4.3.beta2 I get 58ms on average, on this artifact I got 39ms on average.
This speedup should make stylebox drawing faster, for example if you draw hundreds of styleboxes in the same frame. It won't affect stylebox rendering after they have been drawn.