Skip to content

RCTBorderDrawing: Cache results to save resources - #15281

Closed
Adlai-Holler wants to merge 3 commits into
react:masterfrom
Adlai-Holler:AHBorderImageCache
Closed

Adlai-Holler wants to merge 3 commits into
react:masterfrom
Adlai-Holler:AHBorderImageCache

Conversation

@Adlai-Holler

Copy link
Copy Markdown
Contributor

Hi folks. When profiling RNTester on master (688c74) on my iPhone 6 10.3.3 scrolling up and down the grid view example for 10 seconds, average of 3 runs, this patch reduced RCTGetBorderImage from 34ms to 4ms. I didn't look at memory consumption but presumably it lowered that as well.

This is a pretty strong effect but the grid view example is pretty border-heavy so you may get less on other screens. Your call. Love your work!

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Jul 31, 2017
CGContextSetLineWidth(ctx, lineWidth);
CGContextSetLineDash(ctx, 0, dashLengths, sizeof(dashLengths) / sizeof(*dashLengths));

CGContextSetStrokeColorWithColor(ctx, [UIColor yellowColor].CGColor);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I took this out because it looks like it was there by mistake. The stroke color gets overwritten two lines down, before the next draw operation.

RCTGetRGBAColorComponents(borderColors.right, borderParams.rightColors);
RCTGetRGBAColorComponents(backgroundColor, borderParams.backgroundColors);

return [NSValue valueWithBytes:&borderParams objCType:@encode(typeof(borderParams))];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You could use NSData here, and it would actually be slightly faster since the isEqual: wouldn't have to check the underlying type. But it also means another heap allocation – NSValue stores the data on the object itself instead of pointing into the heap. Plus it just feels more purpose-built.

@pull-bot

pull-bot commented Jul 31, 2017 •

Copy link
Copy Markdown
Warnings
⚠️

📋 Test Plan - This PR appears to be missing a Test Plan.

Attention: @shergin, @javache

Generated by 🚫 dangerJS

Comment thread React/Views/RCTBorderDrawing.m Outdated
BOOL drawToEdge)
{
// Important that this struct is aligned on all architectures, else
// the compiler can insert garbage into the padding and corrupt our hash.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we enforce this using a compiler pragma somehow?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep we can, I initially chose not to for the sake of visual cleanliness but thanks for the feedback, safety is king. Ill drop it in later today.

Comment thread React/Base/RCTUtils.m
}

static void RCTGetRGBAColorComponents(CGColorRef color, CGFloat rgba[4])
void RCTGetRGBAColorComponents(CGColorRef color, CGFloat rgba[4])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this an expensive operation? Should we try to normalize the UIColor to this and then use it across the whole border rendering operation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's inexpensive, almost never showed up at 1ms sampling on my iPhone 6 so I'd stick with the CGColor.

@javache

javache commented Aug 2, 2017

Copy link
Copy Markdown
Contributor

Looks good to me. I'll have @shergin take a look as well once he's back from vacation.

@shergin shergin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is fantastic! Just couple of nits. 😄

BOOL drawToEdge)
{
// Important that this struct is not padded on any architecture, else
// there can be garbage in the padding that corrupts our hash.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is not technically hash, so we should not call it hash.

borderImageCache = [NSCache new];
});

if (borderStyle == RCTBorderStyleUnset) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would split this function to two: RCTGetBorderImage and RCTGetCachedBorderImage.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@Adlai-Holler I tried to find reviewers for this pull request and wanted to ping them to take another look. However, based on the blame information for the files in this pull request I couldn't find any reviewers. This sometimes happens when the files in the pull request are new or don't exist on master anymore. Is this pull request still relevant? If yes could you please rebase? In case you know who has context on this code feel free to mention them in a comment (one person is fine). Thanks for reading and hope you will continue contributing to the project.

@stale

stale Bot commented Dec 1, 2017

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

@stale stale Bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 1, 2017
@chirag04

chirag04 commented Dec 1, 2017

Copy link
Copy Markdown
Contributor

seems like something we want to ship. better not let this go stale. @Adlai-Holler can you update as per @shergin's review?

@stale stale Bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 1, 2017
@stale

stale Bot commented Jan 30, 2018

Copy link
Copy Markdown

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

@stale stale Bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 30, 2018
@stale stale Bot closed this Feb 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Stale There has been a lack of activity on this issue and it may be closed soon.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants