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

Eliminate unnecessary CPU usage #31

Merged
merged 1 commit into from
Oct 24, 2013
Merged

Eliminate unnecessary CPU usage #31

merged 1 commit into from
Oct 24, 2013

Conversation

chendo
Copy link
Contributor

@chendo chendo commented Oct 24, 2013

KSImageNamed normally hooks into textViewShouldChangeTextInRange:replacementString:, which causes a lot of KSImageNamed methods to be hit every time the text view changes.

Here is a paste of Obj-C message sends matching KSImageNamed when I press Return only once on an empty line: https://gist.github.com/chendo/7130668

After poking around in DVTTextCompletionController+KSImageNamed and looking at the trace, it seems that replacementString is never nil (at least in Xcode 5.0.1), which is causing imageCompletions to be enumerated every single time text changes.

This would be particularly bad in a project with many image assets.

This PR removes the textView swizzle and hooks into DVTTextCompletionListWindow's showInfoPaneForCompletionItem: and _hideWindow methods, so KSImageNamed is only run when necessary.

Below is the remaining Obj-C message sends matching KSImageNamed when pressing Return once on an empty line after applying this PR:

-> -[DVTTextCompletionController(KSImageNamed) swizzle_acceptCurrentCompletion]
<- -[DVTTextCompletionController(KSImageNamed) swizzle_acceptCurrentCompletion]
-> -[DVTSourceTextView(KSImageNamedSwizzle) swizzle_shouldAutoCompleteAtLocation:]
    -> +[KSImageNamed sharedPlugin]
    <- +[KSImageNamed sharedPlugin]
    -> -[KSImageNamed completionStringsForType:]
    <- -[KSImageNamed completionStringsForType:]
<- -[DVTSourceTextView(KSImageNamedSwizzle) swizzle_shouldAutoCompleteAtLocation:]
-> -[DVTSourceTextView(KSImageNamedSwizzle) swizzle_shouldAutoCompleteAtLocation:]
    -> +[KSImageNamed sharedPlugin]
    <- +[KSImageNamed sharedPlugin]
    -> -[KSImageNamed completionStringsForType:]
    <- -[KSImageNamed completionStringsForType:]
<- -[DVTSourceTextView(KSImageNamedSwizzle) swizzle_shouldAutoCompleteAtLocation:]

I've only tested this on Xcode 5.0.1.

@chendo
Copy link
Contributor Author

chendo commented Oct 24, 2013

A side effect of this PR is it also fixes compatibility issues with chendo/FuzzyAutocomplete as FuzzyAutocomplete disables the replacementString option as it does not make sense when using fuzzy matching.

@ksuther
Copy link
Owner

ksuther commented Oct 24, 2013

Thanks, this looks good! I'll run it for a couple of days and merge it in, assuming it continues to work as it seems to be right now.

@chendo
Copy link
Contributor Author

chendo commented Oct 24, 2013

Sounds good to me! I don't work with imageNamed: that much so I haven't really put it through its paces (I've got a grand total of 6 images in a personal iOS app I'm working on).

ksuther added a commit that referenced this pull request Oct 24, 2013
Eliminate unnecessary CPU usage
@ksuther ksuther merged commit b3efd92 into ksuther:master Oct 24, 2013
@ksuther
Copy link
Owner

ksuther commented Oct 24, 2013

Well I fixed another bug and beat on this a bit, so I'm merging it now. Thanks again!

@chendo
Copy link
Contributor Author

chendo commented Oct 24, 2013

Awesome :D Thanks for the plugin!

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