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

Reduce startup time. #1294

Merged
merged 1 commit into from
Jan 17, 2019
Merged
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
Reduce startup time.
Initializing the LUT arrays at file level scope creates a large chunk of code retaining and releasing all of the NSStrings in the tables. Moving them to function level moves the initialization to being lazy.
  • Loading branch information
dmaclach committed Dec 26, 2018
commit bd9fa2a71873149ca311dec8acd0cfe0b31bfefe
104 changes: 63 additions & 41 deletions Source/Private/_ASCoreAnimationExtras.mm
Original file line number Diff line number Diff line change
Expand Up @@ -62,62 +62,82 @@ void ASDisplayNodeSetResizableContents(id<ASResizableContents> obj, UIImage *ima
NSString *const string;
};

static const struct _UIContentModeStringLUTEntry UIContentModeCAGravityLUT[] = {
{UIViewContentModeScaleToFill, kCAGravityResize},
{UIViewContentModeScaleAspectFit, kCAGravityResizeAspect},
{UIViewContentModeScaleAspectFill, kCAGravityResizeAspectFill},
{UIViewContentModeCenter, kCAGravityCenter},
{UIViewContentModeTop, kCAGravityBottom},
{UIViewContentModeBottom, kCAGravityTop},
{UIViewContentModeLeft, kCAGravityLeft},
{UIViewContentModeRight, kCAGravityRight},
{UIViewContentModeTopLeft, kCAGravityBottomLeft},
{UIViewContentModeTopRight, kCAGravityBottomRight},
{UIViewContentModeBottomLeft, kCAGravityTopLeft},
{UIViewContentModeBottomRight, kCAGravityTopRight},
};
static const _UIContentModeStringLUTEntry *UIContentModeCAGravityLUT(size_t *count)
{
// Initialize this in a function (instead of at file level) to avoid
// startup initialization time.
static const _UIContentModeStringLUTEntry sUIContentModeCAGravityLUT[] = {
{UIViewContentModeScaleToFill, kCAGravityResize},
{UIViewContentModeScaleAspectFit, kCAGravityResizeAspect},
{UIViewContentModeScaleAspectFill, kCAGravityResizeAspectFill},
{UIViewContentModeCenter, kCAGravityCenter},
{UIViewContentModeTop, kCAGravityBottom},
{UIViewContentModeBottom, kCAGravityTop},
{UIViewContentModeLeft, kCAGravityLeft},
{UIViewContentModeRight, kCAGravityRight},
{UIViewContentModeTopLeft, kCAGravityBottomLeft},
{UIViewContentModeTopRight, kCAGravityBottomRight},
{UIViewContentModeBottomLeft, kCAGravityTopLeft},
{UIViewContentModeBottomRight, kCAGravityTopRight},
};
*count = sizeof(sUIContentModeCAGravityLUT) / sizeof(sUIContentModeCAGravityLUT[0]);
return sUIContentModeCAGravityLUT;
}

static const struct _UIContentModeStringLUTEntry UIContentModeDescriptionLUT[] = {
{UIViewContentModeScaleToFill, @"scaleToFill"},
{UIViewContentModeScaleAspectFit, @"aspectFit"},
{UIViewContentModeScaleAspectFill, @"aspectFill"},
{UIViewContentModeRedraw, @"redraw"},
{UIViewContentModeCenter, @"center"},
{UIViewContentModeTop, @"top"},
{UIViewContentModeBottom, @"bottom"},
{UIViewContentModeLeft, @"left"},
{UIViewContentModeRight, @"right"},
{UIViewContentModeTopLeft, @"topLeft"},
{UIViewContentModeTopRight, @"topRight"},
{UIViewContentModeBottomLeft, @"bottomLeft"},
{UIViewContentModeBottomRight, @"bottomRight"},
};
static const _UIContentModeStringLUTEntry *UIContentModeDescriptionLUT(size_t *count)
{
// Initialize this in a function (instead of at file level) to avoid
// startup initialization time.
static const _UIContentModeStringLUTEntry sUIContentModeDescriptionLUT[] = {
{UIViewContentModeScaleToFill, @"scaleToFill"},
{UIViewContentModeScaleAspectFit, @"aspectFit"},
{UIViewContentModeScaleAspectFill, @"aspectFill"},
{UIViewContentModeRedraw, @"redraw"},
{UIViewContentModeCenter, @"center"},
{UIViewContentModeTop, @"top"},
{UIViewContentModeBottom, @"bottom"},
{UIViewContentModeLeft, @"left"},
{UIViewContentModeRight, @"right"},
{UIViewContentModeTopLeft, @"topLeft"},
{UIViewContentModeTopRight, @"topRight"},
{UIViewContentModeBottomLeft, @"bottomLeft"},
{UIViewContentModeBottomRight, @"bottomRight"},
};
*count = sizeof(sUIContentModeDescriptionLUT) / sizeof(sUIContentModeDescriptionLUT[0]);
return sUIContentModeDescriptionLUT;
}

NSString *ASDisplayNodeNSStringFromUIContentMode(UIViewContentMode contentMode)
{
for (let &e : UIContentModeDescriptionLUT) {
if (e.contentMode == contentMode) {
return e.string;
size_t lutSize;
const _UIContentModeStringLUTEntry *lut = UIContentModeDescriptionLUT(&lutSize);
for (size_t i = 0; i < lutSize; ++i) {
if (lut[i].contentMode == contentMode) {
return lut[i].string;
}
}
return [NSString stringWithFormat:@"%d", (int)contentMode];
}

UIViewContentMode ASDisplayNodeUIContentModeFromNSString(NSString *string)
{
for (let &e : UIContentModeDescriptionLUT) {
if (ASObjectIsEqual(e.string, string)) {
return e.contentMode;
size_t lutSize;
const _UIContentModeStringLUTEntry *lut = UIContentModeDescriptionLUT(&lutSize);
for (size_t i = 0; i < lutSize; ++i) {
if (ASObjectIsEqual(lut[i].string, string)) {
return lut[i].contentMode;
}
}
return UIViewContentModeScaleToFill;
}

NSString *const ASDisplayNodeCAContentsGravityFromUIContentMode(UIViewContentMode contentMode)
{
for (let &e : UIContentModeCAGravityLUT) {
if (e.contentMode == contentMode) {
return e.string;
size_t lutSize;
const _UIContentModeStringLUTEntry *lut = UIContentModeCAGravityLUT(&lutSize);
for (size_t i = 0; i < lutSize; ++i) {
if (lut[i].contentMode == contentMode) {
return lut[i].string;
}
}
ASDisplayNodeCAssert(contentMode == UIViewContentModeRedraw, @"Encountered an unknown contentMode %ld. Is this a new version of iOS?", (long)contentMode);
Expand All @@ -137,9 +157,11 @@ UIViewContentMode ASDisplayNodeUIContentModeFromCAContentsGravity(NSString *cons
return cachedModes[foundCacheIndex];
}

for (let &e : UIContentModeCAGravityLUT) {
if (ASObjectIsEqual(e.string, contentsGravity)) {
UIViewContentMode foundContentMode = e.contentMode;
size_t lutSize;
const _UIContentModeStringLUTEntry *lut = UIContentModeCAGravityLUT(&lutSize);
for (size_t i = 0; i < lutSize; ++i) {
if (ASObjectIsEqual(lut[i].string, contentsGravity)) {
UIViewContentMode foundContentMode = lut[i].contentMode;

if (currentCacheIndex < ContentModeCacheSize) {
// Cache the input value. This is almost always a different pointer than in our LUT and will frequently
Expand Down