Author: ericwa
Date: Thu Mar 20 00:21:03 2014
New Revision: 10588

URL: http://svn.gna.org/viewcvs/etoile?rev=10588&view=rev
Log:
COAttributedString: rewrite substringItemGraphWithRange: to be faster (avoid 
copying the entire string). Helps with Typewriter slowness

Modified:
    trunk/Etoile/Frameworks/CoreObject/Core/COCopier.h
    trunk/Etoile/Frameworks/CoreObject/Core/COCopier.m
    trunk/Etoile/Frameworks/CoreObject/Extras/Model/COAttributedString.m

Modified: trunk/Etoile/Frameworks/CoreObject/Core/COCopier.h
URL: 
http://svn.gna.org/viewcvs/etoile/trunk/Etoile/Frameworks/CoreObject/Core/COCopier.h?rev=10588&r1=10587&r2=10588&view=diff
==============================================================================
--- trunk/Etoile/Frameworks/CoreObject/Core/COCopier.h  (original)
+++ trunk/Etoile/Frameworks/CoreObject/Core/COCopier.h  Thu Mar 20 00:21:03 2014
@@ -34,4 +34,10 @@
 - (ETUUID*) copyItemWithUUID: (ETUUID*)aUUID
                    fromGraph: (id<COItemGraph>)source
                      toGraph: (id<COItemGraph>)dest;
+
+- (NSArray*) copyItemsWithUUIDs: (NSArray*)uuids
+                                         fromGraph: (id<COItemGraph>)source
+                                               toGraph: (id<COItemGraph>)dest;
+
+
 @end

Modified: trunk/Etoile/Frameworks/CoreObject/Core/COCopier.m
URL: 
http://svn.gna.org/viewcvs/etoile/trunk/Etoile/Frameworks/CoreObject/Core/COCopier.m?rev=10588&r1=10587&r2=10588&view=diff
==============================================================================
--- trunk/Etoile/Frameworks/CoreObject/Core/COCopier.m  (original)
+++ trunk/Etoile/Frameworks/CoreObject/Core/COCopier.m  Thu Mar 20 00:21:03 2014
@@ -99,18 +99,29 @@
     return result;
 }
 
-
 - (ETUUID*) copyItemWithUUID: (ETUUID*)aUUID
                    fromGraph: (id<COItemGraph>)source
                      toGraph: (id<COItemGraph>)dest
 {
        NILARG_EXCEPTION_TEST(aUUID);
+       return [self copyItemsWithUUIDs: @[aUUID] fromGraph: source toGraph: 
dest][0];
+}
+
+- (NSArray*) copyItemsWithUUIDs: (NSArray*)uuids
+                                        fromGraph: (id<COItemGraph>)source
+                                          toGraph: (id<COItemGraph>)dest
+{
+       NILARG_EXCEPTION_TEST(uuids);
        NILARG_EXCEPTION_TEST(source);
        NILARG_EXCEPTION_TEST(dest);
        
-    NSSet *uuidsToCopy = [self itemUUIDsToCopyForItemItemWithUUID: aUUID
-                                                        fromGraph: source
-                                                          toGraph: dest];
+    NSMutableSet *uuidsToCopy = [NSMutableSet new];
+       for (ETUUID *uuid in uuids)
+       {
+               [uuidsToCopy unionSet: [self 
itemUUIDsToCopyForItemItemWithUUID: uuid
+                                                                               
                                          fromGraph: source
+                                                                               
                                                toGraph: dest]];
+       }
     
     NSMutableDictionary *mapping = [NSMutableDictionary dictionary];
     for (ETUUID *oldUUID in uuidsToCopy)
@@ -130,7 +141,8 @@
     
     [dest insertOrUpdateItems: result];
     
-    return [mapping objectForKey: aUUID];
+       return [uuids mappedCollectionWithBlock:
+                       ^(id inputUUID){ return [mapping objectForKey: 
inputUUID]; }];
 }
 
 @end

Modified: trunk/Etoile/Frameworks/CoreObject/Extras/Model/COAttributedString.m
URL: 
http://svn.gna.org/viewcvs/etoile/trunk/Etoile/Frameworks/CoreObject/Extras/Model/COAttributedString.m?rev=10588&r1=10587&r2=10588&view=diff
==============================================================================
--- trunk/Etoile/Frameworks/CoreObject/Extras/Model/COAttributedString.m        
(original)
+++ trunk/Etoile/Frameworks/CoreObject/Extras/Model/COAttributedString.m        
Thu Mar 20 00:21:03 2014
@@ -52,26 +52,68 @@
        return result;
 }
 
+- (NSArray *) chunkUUIDsOverlappingRange: (NSRange)aRange
+                                excessCharactersAtStart: (NSUInteger 
*)excessAtStart
+                                  excessCharactersAtEnd: (NSUInteger 
*)excessAtEnd
+{
+       NSMutableArray *result = [NSMutableArray new];
+       
+       NSUInteger chunkIndex = 0, chunkStart = 0;
+       COAttributedStringChunk *chunk = [self chunkContainingIndex: 
aRange.location chunkStart: &chunkStart chunkIndex: &chunkIndex];
+       
+       *excessAtStart = (aRange.location - chunkStart);
+       
+       [result addObject: chunk.UUID];
+       
+       const NSUInteger maxRange = NSMaxRange(aRange);
+       
+       while (chunkStart + chunk.length < maxRange)
+       {
+               chunkStart += chunk.length;
+               chunkIndex++;
+               chunk = self.chunks[chunkIndex];
+               
+               [result addObject: chunk.UUID];
+       }
+       
+       *excessAtEnd = ((chunkStart + chunk.length) - maxRange);
+       
+       return result;
+}
+
 - (COItemGraph *) substringItemGraphWithRange: (NSRange)aRange
 {
-       // Copy the receiver into a temporary context
-       COObjectGraphContext *tempCtx = [COObjectGraphContext new];
+       ETAssert(aRange.length > 0);
+       
+       COItemGraph *result = [[COItemGraph alloc] init];
+       
+       NSUInteger excessAtStart = 0;
+       NSUInteger excessAtEnd = 0;
+       NSArray *chunkUUIDS = [self chunkUUIDsOverlappingRange: aRange 
excessCharactersAtStart: &excessAtStart excessCharactersAtEnd: &excessAtEnd];
        
        COCopier *copier = [COCopier new];
-       ETUUID *copyUUID = [copier copyItemWithUUID: [self UUID] fromGraph: 
self.objectGraphContext toGraph: tempCtx];
-       COAttributedString *tempCopy = [tempCtx loadedObjectForUUID: copyUUID];
-       [tempCtx setRootObject: tempCopy];
-       
-       // Split the copy with the given range
-       NSUInteger start = [tempCopy splitChunkAtIndex: aRange.location];
-       NSUInteger end = [tempCopy splitChunkAtIndex: aRange.location + 
aRange.length];
-       
-       // Remove all chunks outside the requested range.
-       tempCopy.chunks = [tempCopy.chunks subarrayWithRange: 
NSMakeRange(start, end-start)];
-       
-       [tempCtx removeUnreachableObjects];
-       
-       COItemGraph *result = [[COItemGraph alloc] initWithItemGraph: tempCtx];
+       NSArray *copiedUUIDs = [copier copyItemsWithUUIDs: chunkUUIDS 
fromGraph: self.objectGraphContext toGraph: result];
+       
+       // Trim off excess characters
+       
+       COMutableItem *firstChunk = [result itemForUUID: copiedUUIDs[0]];
+       [firstChunk setValue: [[firstChunk valueForAttribute: @"text"] 
substringFromIndex: excessAtStart]
+                       forAttribute: @"text"
+                                       type: kCOTypeString];
+       
+       COMutableItem *lastChunk = [result itemForUUID: [copiedUUIDs 
lastObject]];
+       [lastChunk setValue: [[lastChunk valueForAttribute: @"text"] 
substringToIndex: ([[lastChunk valueForAttribute: @"text"] length] - 
excessAtEnd)]
+                       forAttribute: @"text"
+                                       type: kCOTypeString];
+       
+       // Insert a root COAttributedString item
+       
+       COMutableItem *rootItem = [COMutableItem item];
+       [rootItem setValue: @"COAttributedString" forAttribute: 
kCOObjectEntityNameProperty type: kCOTypeString];
+       [rootItem setValue: copiedUUIDs forAttribute: @"chunks" type: 
COTypeMakeArrayOf(kCOTypeCompositeReference)];
+       [result insertOrUpdateItems: @[rootItem]];
+       [result setRootItemUUID: [rootItem UUID]];
+       
        return result;
 }
 


_______________________________________________
Etoile-cvs mailing list
[email protected]
https://mail.gna.org/listinfo/etoile-cvs

Reply via email to