[dasher: 15/43] iPhone: make progress display more efficient!



commit 66cf77e9ee4ba369c64ef946274922e0afc0d14b
Author: Alan Lawrence <acl33 inf phy cam ac uk>
Date:   Tue Apr 5 15:38:53 2011 +0100

    iPhone: make progress display more efficient!
    
    Main thread polls for message 5 times/sec, train thread updates asynchronously.
     This is significantly faster than the training thread updating the label
     every time, for the >>5 updates/sec we get with 300k training text, and only
     marginally slower than not updating the progress label at all.

 Src/iPhone/Classes/DasherAppDelegate.mm |   37 ++++++++++++++++--------------
 1 files changed, 20 insertions(+), 17 deletions(-)
---
diff --git a/Src/iPhone/Classes/DasherAppDelegate.mm b/Src/iPhone/Classes/DasherAppDelegate.mm
index 66860c9..2d87c6a 100644
--- a/Src/iPhone/Classes/DasherAppDelegate.mm
+++ b/Src/iPhone/Classes/DasherAppDelegate.mm
@@ -29,6 +29,7 @@
 //calls through to [EAGLView makeContextCurrent]
 - (void)selectEAGLContext;
 @property (retain) UILabel *screenLockLabel;
+ property (retain) NSString *lockText;
 @property (nonatomic,retain) NSString *m_wordBoundary;
 @property (nonatomic,retain) NSString *m_sentenceBoundary;
 @property (nonatomic,retain) NSString *m_lineBoundary;
@@ -55,6 +56,7 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
 @implementation DasherAppDelegate
 
 @synthesize screenLockLabel;
+ synthesize lockText;
 @synthesize m_wordBoundary;
 @synthesize m_sentenceBoundary;
 @synthesize m_lineBoundary;
@@ -530,26 +532,15 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
   [glView makeContextCurrent];
 }
 
-#pragma mark TextViewDelegate methods
-
--(void)textViewDidChangeSelection:(UITextView *)textView {
-  DASHER_ASSERT(textView == text);
-  selectedText = text.selectedRange;
-  if (selectedText.location == NSIntegerMax) selectedText.location = text.text.length;
-  _dasherInterface->SetOffset(selectedText.location);
-}
-
-- (void)setLockText:(NSString *)s {
-  if ([NSThread isMainThread]) {
-    UILabel *lbl = self.screenLockLabel;
-    if (!lbl) return;
-    if (s) {
+-(void)updateLockText {
+  NSAssert([NSThread isMainThread],@"Not on main thread?!?!");
+  if (UILabel *lbl=self.screenLockLabel) {
+    if (NSString *s = self.lockText) {
       [lbl setText:s];
       lbl.hidden = NO;
-    }
-    else lbl.hidden = YES;
+    } else lbl.hidden = YES;   
+    [self performSelector:@selector(updateLockText) withObject:nil afterDelay:0.2];
   }
-  else [self performSelectorOnMainThread:@selector(setLockText:) withObject:s waitUntilDone:YES];
 }
 
 + (DasherAppDelegate *)theApp {
@@ -557,6 +548,16 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
 	NSAssert ([app isMemberOfClass:[DasherAppDelegate class]], @"AppDelegate is not DasherAppDelegate!");
 	return (DasherAppDelegate *)app;
 }
+
+#pragma mark TextViewDelegate method
+
+-(void)textViewDidChangeSelection:(UITextView *)textView {
+  DASHER_ASSERT(textView == text);
+  selectedText = text.selectedRange;
+  if (selectedText.location == NSIntegerMax) selectedText.location = text.text.length;
+  _dasherInterface->SetOffset(selectedText.location);
+}
+
 @end
 
 /* interface UIViewController (lockable)
@@ -607,6 +608,7 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
     va_end(argumentList);
   }
   [self performSelectorInBackground:@selector(aSyncMain:) withObject:action];
+  [[DasherAppDelegate theApp] performSelector:@selector(updateLockText) withObject:nil afterDelay:0.2];
 }
 
 - (void)aSyncMain:(NSInvocation *)action {
@@ -616,6 +618,7 @@ static SModuleSettings _miscSettings[] = { //note iStep and string description a
   [[DasherAppDelegate theApp] selectEAGLContext];
   [action invoke];
   [DasherAppDelegate theApp].screenLockLabel = nil;
+  [NSObject cancelPreviousPerformRequestsWithTarget:[DasherAppDelegate theApp] selector:@selector(updateLockText) object:nil];
   //passing 'nil' here, where a BOOL is expected, is a horrendous trick - nil = 0x0 is effectively reinterpret_casted... 
   // however, the 'correct' method of passing [NSNumber numberWithBool:] is erratic, resulting in either inversion, 
   // always true, or always false, on different versions of the iPhone OS/SDK...



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]