-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[Performance] Convert ASLayoutElementSize to atomic #trivial #331
Merged
nguyenhuy
merged 3 commits into
TextureGroup:master
from
hannahmbanana:HTAtomicLayoutElementSize
Jun 13, 2017
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,9 +124,9 @@ void ASLayoutElementClearCurrentContext() | |
|
||
@implementation ASLayoutElementStyle { | ||
ASDN::RecursiveMutex __instanceLock__; | ||
ASLayoutElementSize _size; | ||
ASLayoutElementStyleExtensions _extensions; | ||
|
||
std::atomic<ASLayoutElementSize> _size; | ||
std::atomic<CGFloat> _spacingBefore; | ||
std::atomic<CGFloat> _spacingAfter; | ||
std::atomic<CGFloat> _flexGrow; | ||
|
@@ -180,93 +180,91 @@ - (instancetype)init | |
|
||
- (ASLayoutElementSize)size | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
return _size; | ||
return _size.load(); | ||
} | ||
|
||
- (void)setSize:(ASLayoutElementSize)size | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size = size; | ||
_size.store(size); | ||
} | ||
|
||
#pragma mark - ASLayoutElementStyleSizeForwarding | ||
|
||
- (ASDimension)width | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
return _size.width; | ||
return _size.load().width; | ||
} | ||
|
||
- (void)setWidth:(ASDimension)width | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.width = width; | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.width = width; | ||
_size.store(newSize); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleWidthProperty); | ||
} | ||
|
||
- (ASDimension)height | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
return _size.height; | ||
return _size.load().height; | ||
} | ||
|
||
- (void)setHeight:(ASDimension)height | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.height = height; | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.height = height; | ||
_size.store(newSize); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleHeightProperty); | ||
} | ||
|
||
- (ASDimension)minWidth | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
return _size.minWidth; | ||
return _size.load().minWidth; | ||
} | ||
|
||
- (void)setMinWidth:(ASDimension)minWidth | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.minWidth = minWidth; | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.minWidth = minWidth; | ||
_size.store(newSize); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMinWidthProperty); | ||
} | ||
|
||
- (ASDimension)maxWidth | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
return _size.maxWidth; | ||
return _size.load().maxWidth; | ||
} | ||
|
||
- (void)setMaxWidth:(ASDimension)maxWidth | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.maxWidth = maxWidth; | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.maxWidth = maxWidth; | ||
_size.store(newSize); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMaxWidthProperty); | ||
} | ||
|
||
- (ASDimension)minHeight | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
return _size.minHeight; | ||
return _size.load().minHeight; | ||
} | ||
|
||
- (void)setMinHeight:(ASDimension)minHeight | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.minHeight = minHeight; | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.minHeight = minHeight; | ||
_size.store(newSize); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMinHeightProperty); | ||
} | ||
|
||
- (ASDimension)maxHeight | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
return _size.maxHeight; | ||
return _size.load().maxHeight; | ||
} | ||
|
||
- (void)setMaxHeight:(ASDimension)maxHeight | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.maxHeight = maxHeight; | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.maxHeight = maxHeight; | ||
_size.store(newSize); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMaxHeightProperty); | ||
} | ||
|
||
|
@@ -277,88 +275,94 @@ - (void)setMaxHeight:(ASDimension)maxHeight | |
|
||
- (void)setPreferredSize:(CGSize)preferredSize | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.width = ASDimensionMakeWithPoints(preferredSize.width); | ||
_size.height = ASDimensionMakeWithPoints(preferredSize.height); | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.width = ASDimensionMakeWithPoints(preferredSize.width); | ||
newSize.height = ASDimensionMakeWithPoints(preferredSize.height); | ||
_size.store(newSize); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleWidthProperty); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleHeightProperty); | ||
} | ||
|
||
- (CGSize)preferredSize | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
if (_size.width.unit == ASDimensionUnitFraction) { | ||
NSCAssert(NO, @"Cannot get preferredSize of element with fractional width. Width: %@.", NSStringFromASDimension(_size.width)); | ||
ASLayoutElementSize size = _size.load(); | ||
if (size.width.unit == ASDimensionUnitFraction) { | ||
NSCAssert(NO, @"Cannot get preferredSize of element with fractional width. Width: %@.", NSStringFromASDimension(size.width)); | ||
return CGSizeZero; | ||
} | ||
|
||
if (_size.height.unit == ASDimensionUnitFraction) { | ||
NSCAssert(NO, @"Cannot get preferredSize of element with fractional height. Height: %@.", NSStringFromASDimension(_size.height)); | ||
if (size.height.unit == ASDimensionUnitFraction) { | ||
NSCAssert(NO, @"Cannot get preferredSize of element with fractional height. Height: %@.", NSStringFromASDimension(size.height)); | ||
return CGSizeZero; | ||
} | ||
|
||
return CGSizeMake(_size.width.value, _size.height.value); | ||
return CGSizeMake(size.width.value, size.height.value); | ||
} | ||
|
||
- (void)setMinSize:(CGSize)minSize | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.minWidth = ASDimensionMakeWithPoints(minSize.width); | ||
_size.minHeight = ASDimensionMakeWithPoints(minSize.height); | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.minWidth = ASDimensionMakeWithPoints(minSize.width); | ||
newSize.minHeight = ASDimensionMakeWithPoints(minSize.height); | ||
_size.store(newSize); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMinWidthProperty); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMinHeightProperty); | ||
} | ||
|
||
- (void)setMaxSize:(CGSize)maxSize | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.maxWidth = ASDimensionMakeWithPoints(maxSize.width); | ||
_size.maxHeight = ASDimensionMakeWithPoints(maxSize.height); | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.maxWidth = ASDimensionMakeWithPoints(maxSize.width); | ||
newSize.maxHeight = ASDimensionMakeWithPoints(maxSize.height); | ||
_size.store(newSize); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMaxWidthProperty); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMaxHeightProperty); | ||
} | ||
|
||
- (ASLayoutSize)preferredLayoutSize | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
return ASLayoutSizeMake(_size.width, _size.height); | ||
ASLayoutElementSize size = _size.load(); | ||
return ASLayoutSizeMake(size.width, size.height); | ||
} | ||
|
||
- (void)setPreferredLayoutSize:(ASLayoutSize)preferredLayoutSize | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.width = preferredLayoutSize.width; | ||
_size.height = preferredLayoutSize.height; | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.width = preferredLayoutSize.width; | ||
newSize.height = preferredLayoutSize.height; | ||
_size.store(newSize); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleWidthProperty); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleHeightProperty); | ||
} | ||
|
||
- (ASLayoutSize)minLayoutSize | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
return ASLayoutSizeMake(_size.minWidth, _size.minHeight); | ||
ASLayoutElementSize size = _size.load(); | ||
return ASLayoutSizeMake(size.minWidth, size.minHeight); | ||
} | ||
|
||
- (void)setMinLayoutSize:(ASLayoutSize)minLayoutSize | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.minWidth = minLayoutSize.width; | ||
_size.minHeight = minLayoutSize.height; | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.minWidth = minLayoutSize.width; | ||
newSize.minHeight = minLayoutSize.height; | ||
_size.store(newSize); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMinWidthProperty); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMinHeightProperty); | ||
} | ||
|
||
- (ASLayoutSize)maxLayoutSize | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
return ASLayoutSizeMake(_size.maxWidth, _size.maxHeight); | ||
ASLayoutElementSize size = _size.load(); | ||
return ASLayoutSizeMake(size.maxWidth, size.maxHeight); | ||
} | ||
|
||
- (void)setMaxLayoutSize:(ASLayoutSize)maxLayoutSize | ||
{ | ||
ASDN::MutexLocker l(__instanceLock__); | ||
_size.maxWidth = maxLayoutSize.width; | ||
_size.maxHeight = maxLayoutSize.height; | ||
ASLayoutElementSize newSize = _size.load(); | ||
newSize.maxWidth = maxLayoutSize.width; | ||
newSize.maxHeight = maxLayoutSize.height; | ||
_size.store(newSize); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if |
||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMaxWidthProperty); | ||
ASLayoutElementStyleCallDelegate(ASLayoutElementStyleMaxHeightProperty); | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Unfortunately this breaks atomicity =/ Imagine:
Thread 1: read size A
Thread 2: read size A
Thread 1: write size { A, height = x }
Thread 2: write size { A, width = y }
So we end up with
size { A, width = y }
, thread 2 overwrote thread 1's change. We should have ended up withsize { A, height = x, width = y }
.We have to do the read-update-write all in one operation.