Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[WEEX-648][iOS]native batch for dom operations generating from JavaSc…
Browse files Browse the repository at this point in the history
…ript (#1644)

* [WEEX-648][iOS]native batch for dom operations generating from javaScript

wrap a series of dom operations with beginBatch and endBatch directives, when every V-sync signal comes,
we try to ensure that the operations between beginBatch and endBatch can be performed in current V-sync,
this policy can drop frames maybe, for our policy dropping frames, we only allow 20 frames at most.

Bugs:648

* [WEEX-648][iOS] clear the count for drop frames when clear stashed operation queue

Bug: 648

* [WEEX-648] adjust max collection times to 200

* [WEEX-648][iOS] optimize code batch operations commit

* [WEEX-648][iOS] try to reduce the dispatch count for main thread

feature:648
  • Loading branch information
acton393 authored and cxfeng1 committed Nov 5, 2018
1 parent 817f6ef commit 2187889
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
14 changes: 14 additions & 0 deletions ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,18 @@ void WXPerformBlockSyncOnComponentThread(void (^block)(void));
*/
- (void)enumerateComponentsUsingBlock:(void (^)(WXComponent *, BOOL *stop))block;

#pragma mark batch mark

/**
a start native batch tag for a group of UI operations, company with performBatchEnd
@see performBatchEnd
*/
- (void)performBatchBegin;

/**
an end native batch tag for a group of UI operations, company with performBatchBegin
@see performBatchBegin
*/
- (void)performBatchEnd;

@end
67 changes: 59 additions & 8 deletions ios/sdk/WeexSDK/Sources/Manager/WXComponentManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
static NSThread *WXComponentThread;

#define WXAssertComponentExist(component) WXAssert(component, @"component not exists")

#define MAX_DROP_FRAME_FOR_BATCH 200

@implementation WXComponentManager
{
Expand All @@ -64,6 +64,7 @@ @implementation WXComponentManager

pthread_mutex_t _propertyMutex;
pthread_mutexattr_t _propertMutexAttr;
NSUInteger _syncUITaskCount;
}

+ (instancetype)sharedManager
Expand All @@ -80,7 +81,7 @@ - (instancetype)initWithWeexInstance:(id)weexInstance
{
if (self = [self init]) {
_weexInstance = weexInstance;

_syncUITaskCount = 0;
_indexDict = [NSMapTable strongToWeakObjectsMapTable];
_fixedComponents = [NSMutableArray wx_mutableArrayUsingWeakReferences];
_uiTaskQueue = [NSMutableArray array];
Expand Down Expand Up @@ -991,13 +992,42 @@ - (void) _printFlexComponentFrame:(WXComponent *)component

- (void)_syncUITasks
{
NSArray<dispatch_block_t> *blocks = _uiTaskQueue;
_uiTaskQueue = [NSMutableArray array];
dispatch_async(dispatch_get_main_queue(), ^{
for(dispatch_block_t block in blocks) {
block();
NSInteger mismatchBeginIndex = _uiTaskQueue.count;
for (NSInteger i = _uiTaskQueue.count - 1;i >= 0;i --) {
if (_uiTaskQueue[i] == WXPerformUITaskBatchEndBlock) {
_syncUITaskCount = 0;
// clear when find the matches for end and begin tag
break;
}
});
if (_uiTaskQueue[i] == WXPerformUITaskBatchBeginBlock) {
mismatchBeginIndex = i;
break;
}
}

if (mismatchBeginIndex == _uiTaskQueue.count) {
// here we get end tag or there are not begin and end directives
} else {
_syncUITaskCount ++;
// we only find begin tag but missing end tag,
if (_syncUITaskCount > (MAX_DROP_FRAME_FOR_BATCH)) {
// when the wait times come to MAX_DROP_FRAME_FOR_BATCH, we will pop all the stashed operations for user experience.
mismatchBeginIndex = _uiTaskQueue.count;
_syncUITaskCount = 0;
}
}

if (mismatchBeginIndex > 0) {
NSArray<dispatch_block_t> *blocks = [_uiTaskQueue subarrayWithRange:NSMakeRange(0, mismatchBeginIndex)];
[_uiTaskQueue removeObjectsInRange:NSMakeRange(0, mismatchBeginIndex)];
if (blocks.count) {
dispatch_async(dispatch_get_main_queue(), ^{
for(dispatch_block_t block in blocks) {
block();
}
});
}
}
}

#pragma mark Fixed
Expand Down Expand Up @@ -1051,6 +1081,27 @@ - (void)enumerateComponentsUsingBlock:(void (^)(WXComponent *, BOOL *stop))block
}
}

static void (^WXPerformUITaskBatchBeginBlock)(void) = ^ () {
#if DEBUG
WXLogDebug(@"directive BatchBeginBlock");
#endif
};
static void (^WXPerformUITaskBatchEndBlock)(void) = ^ () {
#if DEBUG
WXLogDebug(@"directive BatchEndBlock");
#endif
};

- (void)performBatchBegin
{
[self _addUITask:WXPerformUITaskBatchBeginBlock];
}

- (void)performBatchEnd
{
[self _addUITask:WXPerformUITaskBatchEndBlock];
}

- (void)handleDisplayLink {
[self _handleDisplayLink];
}
Expand Down
16 changes: 16 additions & 0 deletions ios/sdk/WeexSDK/Sources/Module/WXDomModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ @implementation WXDomModule
WX_EXPORT_METHOD(@selector(addRule:rule:))
WX_EXPORT_METHOD(@selector(getComponentRect:callback:))
WX_EXPORT_METHOD(@selector(updateComponentData:componentData:callback:))
WX_EXPORT_METHOD(@selector(beginBatchMark))
WX_EXPORT_METHOD(@selector(endBatchMark))

- (void)performBlockOnComponentManager:(void(^)(WXComponentManager *))block
{
Expand Down Expand Up @@ -82,6 +84,20 @@ - (void)performSelectorOnRuleManager:(void(^)(void))block{
});
}

- (void)beginBatchMark
{
[self performBlockOnComponentManager:^(WXComponentManager *manager) {
[manager performBatchBegin];
}];
}

- (void)endBatchMark
{
[self performBlockOnComponentManager:^(WXComponentManager * manager) {
[manager performBatchEnd];
}];
}

- (NSThread *)targetExecuteThread
{
return [WXComponentManager componentThread];
Expand Down

0 comments on commit 2187889

Please sign in to comment.