Skip to content

Commit

Permalink
Reduce startup time. (TextureGroup#1294)
Browse files Browse the repository at this point in the history
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 authored and nguyenhuy committed Jan 17, 2019
1 parent d73c44d commit d8cc3c9
Showing 1 changed file with 63 additions and 41 deletions.
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

0 comments on commit d8cc3c9

Please sign in to comment.