Skip to content

Commit

Permalink
Shrink _ASCollectionPendingState from 144 to 128 bytes, a 12.5% reduc…
Browse files Browse the repository at this point in the history
…tion (#1485)

* Shrink _ASCollectionPendingState from 144 to 128 bytes, a 12.5% reduction

These objects accumulate in the heap, so reducing their size will allow more to accumulate before memory warnings.

Group the `BOOL`s into a struct. Shrink the various stored `enum`s to fit the size of their contents. Move the ivars around so that the smaller `enum` are near eachother and the bitfield struct.

* address review comments. move enums out. add comment. rename struct to _flags

* fix a missing rename
  • Loading branch information
Greg Bolsinga authored and garrettmoon committed May 7, 2019
1 parent bf82b56 commit c3e608b
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 9 deletions.
141 changes: 134 additions & 7 deletions Source/ASCollectionNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@
@interface _ASCollectionPendingState : NSObject {
@public
std::vector<std::vector<ASRangeTuningParameters>> _tuningParameters;

// Keep these enums by the bitfield struct to save memory.
ASLayoutRangeMode _rangeMode;
ASCellLayoutMode _cellLayoutMode;
struct {
unsigned int allowsSelection:1; // default is YES
unsigned int allowsMultipleSelection:1; // default is NO
unsigned int inverted:1; //default is NO
unsigned int alwaysBounceVertical:1;
unsigned int alwaysBounceHorizontal:1;
unsigned int animatesContentOffset:1;
unsigned int showsVerticalScrollIndicator:1;
unsigned int showsHorizontalScrollIndicator:1;
unsigned int pagingEnabled:1;
} _flags;
}
@property (nonatomic, weak) id <ASCollectionDelegate> delegate;
@property (nonatomic, weak) id <ASCollectionDataSource> dataSource;
Expand Down Expand Up @@ -65,19 +80,131 @@ - (instancetype)init
if (self) {
_rangeMode = ASLayoutRangeModeUnspecified;
_tuningParameters = [ASAbstractLayoutController defaultTuningParameters];
_allowsSelection = YES;
_allowsMultipleSelection = NO;
_inverted = NO;
_flags.allowsSelection = YES;
_flags.allowsMultipleSelection = NO;
_flags.inverted = NO;
_contentInset = UIEdgeInsetsZero;
_contentOffset = CGPointZero;
_animatesContentOffset = NO;
_showsVerticalScrollIndicator = YES;
_showsHorizontalScrollIndicator = YES;
_pagingEnabled = NO;
_flags.animatesContentOffset = NO;
_flags.showsVerticalScrollIndicator = YES;
_flags.showsHorizontalScrollIndicator = YES;
_flags.pagingEnabled = NO;
}
return self;
}

#pragma mark Properties

- (ASLayoutRangeMode)rangeMode
{
return _rangeMode;
}

- (void)setRangeMode:(ASLayoutRangeMode)rangeMode
{
_rangeMode = rangeMode;
}

- (ASCellLayoutMode)cellLayoutMode
{
return _cellLayoutMode;
}

- (void)setCellLayoutMode:(ASCellLayoutMode)cellLayoutMode
{
_cellLayoutMode = cellLayoutMode;
}

- (BOOL)allowsSelection
{
return _flags.allowsSelection;
}

- (void)setAllowsSelection:(BOOL)allowsSelection
{
_flags.allowsSelection = allowsSelection;
}

- (BOOL)allowsMultipleSelection
{
return _flags.allowsMultipleSelection;
}

- (void)setAllowsMultipleSelection:(BOOL)allowsMultipleSelection
{
_flags.allowsMultipleSelection = allowsMultipleSelection;
}

- (BOOL)inverted
{
return _flags.inverted;
}

-(void)setInverted:(BOOL)inverted
{
_flags.inverted = inverted;
}

-(BOOL)alwaysBounceVertical
{
return _flags.alwaysBounceVertical;
}

-(void)setAlwaysBounceVertical:(BOOL)alwaysBounceVertical
{
_flags.alwaysBounceVertical = alwaysBounceVertical;
}

-(BOOL)alwaysBounceHorizontal
{
return _flags.alwaysBounceHorizontal;
}

-(void)setAlwaysBounceHorizontal:(BOOL)alwaysBounceHorizontal
{
_flags.alwaysBounceHorizontal = alwaysBounceHorizontal;
}

- (BOOL)animatesContentOffset
{
return _flags.animatesContentOffset;
}

-(void)setAnimatesContentOffset:(BOOL)animatesContentOffset
{
_flags.animatesContentOffset = animatesContentOffset;
}

- (BOOL)showsVerticalScrollIndicator
{
return _flags.showsVerticalScrollIndicator;
}

- (void)setShowsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator
{
_flags.showsVerticalScrollIndicator = showsVerticalScrollIndicator;
}

-(BOOL)showsHorizontalScrollIndicator
{
return _flags.showsHorizontalScrollIndicator;
}

- (void)setShowsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator
{
_flags.showsHorizontalScrollIndicator = showsHorizontalScrollIndicator;
}

-(BOOL)pagingEnabled
{
return _flags.pagingEnabled;
}

- (void)setPagingEnabled:(BOOL)pagingEnabled
{
_flags.pagingEnabled = pagingEnabled;
}

#pragma mark Tuning Parameters

- (ASRangeTuningParameters)tuningParametersForRangeType:(ASLayoutRangeType)rangeType
Expand Down
2 changes: 1 addition & 1 deletion Source/ASCollectionViewProtocols.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import <UIKit/UIKit.h>
#import <AsyncDisplayKit/ASBaseDefines.h>

typedef NS_OPTIONS(NSUInteger, ASCellLayoutMode) {
typedef NS_OPTIONS(unsigned short, ASCellLayoutMode) {
/**
* No options set. If cell layout mode is set to ASCellLayoutModeNone, the default values for
* each flag listed below is used.
Expand Down
2 changes: 1 addition & 1 deletion Source/Details/ASLayoutRangeType.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ AS_EXTERN BOOL ASRangeTuningParametersEqualToRangeTuningParameters(ASRangeTuning
* Depending on some conditions (including interface state and direction of the scroll view, state of rendering engine, etc),
* a range controller can choose which mode it should use at a given time.
*/
typedef NS_ENUM(NSInteger, ASLayoutRangeMode) {
typedef NS_ENUM(char, ASLayoutRangeMode) {
ASLayoutRangeModeUnspecified = -1,

/**
Expand Down

0 comments on commit c3e608b

Please sign in to comment.