Skip to content
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

[ListMonitor] Fix performance of subscript(safeSectionIndex:safeItemIndex:). #287

Conversation

ruslanskorb
Copy link
Contributor

There is a performance problem in Swift when getting an object from the Objective-C array with a large number of objects using subscript. It requires casting the array between Objective-C and Swift, that is pretty slow.

…ript(indexPath:)` after the validation of `sectionIndex` and `itemIndex` to get an object.

There is a performance problem in Swift when getting an object from the Objective-C array with a large number of objects using subscript. It requires casting the array between Objective-C and Swift, that is pretty slow.
@JohnEstropia
Copy link
Owner

Do you have benchmarks for this? I am under the impression that ObjC arrays are toll-free bridged, especially since we are just accessing the indexer which should be calling NSArray's objectAtIndexedSubscript: method

@ruslanskorb
Copy link
Contributor Author

I guess this is because of the type of the elements of the array. This is what I have.

With the current implementation:

screenshot 2018-12-04 at 22 05 16

screenshot 2018-12-04 at 22 07 15

With the proposed implementation:

screenshot 2018-12-04 at 22 21 07

@JohnEstropia
Copy link
Owner

JohnEstropia commented Dec 5, 2018

I see, that's interesting. Can you try if casting it to NSArray first would work?

        return ObjectType.cs_fromRaw(
            object: (section.objects! as NSArray).object(at: itemIndex) as! NSManagedObject
        )

If this works then I'm pretty sure it would be faster than using IndexPath as that would search the internal sections again.

@ruslanskorb
Copy link
Contributor Author

This is faster than the current implementation, but still quite slow.

screenshot 2018-12-05 at 09 23 37

screenshot 2018-12-05 at 09 24 27

@JohnEstropia
Copy link
Owner

Wow, that's actually counter-intuitive. Thanks for the tests!

@JohnEstropia JohnEstropia merged commit 42caee2 into JohnEstropia:develop Dec 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants