Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Merge performance changes from cindex.py #244

Merged
merged 6 commits into from

2 participants

@tobig
Collaborator

This pull request syncs our cindex.py again with the upstream cindex.py version. The main reason for this sync are three performance related changes that I committed to the core clang repository. For examples/boost.cpp these changes improve formatting time for me from 0.84 to 0.45 seconds.

@tobig tobig referenced this pull request
Closed

Maintenance of cindex.py #241

@Rip-Rip
Owner

Wow this is a great improvement! I'll wait for your answer before merging those.
Thanks!

tobig added some commits
@tobig tobig Merge: [cindex.py]: Speed up lookup of the completion kind
We can directly the number of the kind instead of going through the
completionChunkKindMap.

Formatting time changes from 0.84 to 0.72 seconds.

https://llvm.org/svn/llvm-project/cfe/trunk@17289
96924a7
@tobig tobig Merge: [cindex.py] Replace CachedProperty with our own implementation
This is a very performance critical point for auto completion. The
manual implementation gives a large speedup. As it does not complicate
the code a lot, I figured it is worth the change. If anybody understands
why the CachedProperty is here so much slower, I am very interested in
working on an improvement of CachedProperty.

Formatting time changes from 0.72 to 0.57 seconds.

https://llvm.org/svn/llvm-project/cfe/trunk@172900
b764ade
@tobig tobig Merge: Fix a copypasto bug. Also rename the parameter in question to not
shadow the 'file' builtin, and fix up a docstring a little.

Hat tip to Sebastian Kreft Carreno at Google for noticing the bug.

https://llvm.org/svn/llvm-project/cfe/trunk@169887
14906a4
@tobig tobig Merge: [python bindings] Expose cursor.referenced (clang_getCursorRef…
…erenced).

Patch by Matthew King!

https://llvm.org/svn/llvm-project/cfe/trunk@171423
64491be
@tobig tobig Merge: [cindex.py] Add cache for CompletionChunk spellings
Most of the CompletionChunks represent braces, colons or other one
character spellings. There is no need to call libclang, to figure out
how to write a colon. Instead we use an internal cache to retrieve the
correct spelling. As function calls from python are very expensive and
this is a performance critical part of auto completion this patch makes
formatting of auto completion results a lot faster.

Formatting time changes from 0.57 to 0.45 seconds

https://llvm.org/svn/llvm-project/cfe/trunk@172901
7fa1d1a
@tobig tobig Merge: [cindex.py]: Use spaces instead of tabs
The tabs slipped in accidentally.

https://llvm.org/svn/llvm-project/cfe/trunk@172934
e8d0f36
@tobig
Collaborator
  • I fixed the tabs that accidentally slipped in.
  • I do not initialize the __kindNumber immediately as this would cause an unnecessary call in case the kindNumber is not needed. If we figure out the kindNumber is almost always needed, it may be worth to initialize it immediately. However, in this patch I was trying to model the behavior in terms of c function calls. My only goal was to make it faster.

Thanks for your review.

@Rip-Rip
Owner

Great! Thanks for doing this.

@Rip-Rip Rip-Rip merged commit e8d0f36 into Rip-Rip:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 20, 2013
  1. @tobig

    Merge: [cindex.py]: Speed up lookup of the completion kind

    tobig authored
    We can directly the number of the kind instead of going through the
    completionChunkKindMap.
    
    Formatting time changes from 0.84 to 0.72 seconds.
    
    https://llvm.org/svn/llvm-project/cfe/trunk@17289
  2. @tobig

    Merge: [cindex.py] Replace CachedProperty with our own implementation

    tobig authored
    This is a very performance critical point for auto completion. The
    manual implementation gives a large speedup. As it does not complicate
    the code a lot, I figured it is worth the change. If anybody understands
    why the CachedProperty is here so much slower, I am very interested in
    working on an improvement of CachedProperty.
    
    Formatting time changes from 0.72 to 0.57 seconds.
    
    https://llvm.org/svn/llvm-project/cfe/trunk@172900
  3. @tobig

    Merge: Fix a copypasto bug. Also rename the parameter in question to not

    tobig authored
    shadow the 'file' builtin, and fix up a docstring a little.
    
    Hat tip to Sebastian Kreft Carreno at Google for noticing the bug.
    
    https://llvm.org/svn/llvm-project/cfe/trunk@169887
  4. @tobig
  5. @tobig

    Merge: [cindex.py] Add cache for CompletionChunk spellings

    tobig authored
    Most of the CompletionChunks represent braces, colons or other one
    character spellings. There is no need to call libclang, to figure out
    how to write a colon. Instead we use an internal cache to retrieve the
    correct spelling. As function calls from python are very expensive and
    this is a performance critical part of auto completion this patch makes
    formatting of auto completion results a lot faster.
    
    Formatting time changes from 0.57 to 0.45 seconds
    
    https://llvm.org/svn/llvm-project/cfe/trunk@172901
  6. @tobig
This page is out of date. Refresh to see the latest.
Showing with 60 additions and 10 deletions.
  1. +60 −10 plugin/clang/cindex.py
View
70 plugin/clang/cindex.py
@@ -1271,6 +1271,17 @@ def translation_unit(self):
# created.
return self._tu
+ @property
+ def referenced(self):
+ """
+ For a cursor that is a reference, returns a cursor
+ representing the entity that it references.
+ """
+ if not hasattr(self, '_referenced'):
+ self._referenced = conf.lib.clang_getCursorReferenced(self)
+
+ return self._referenced
+
def get_arguments(self):
"""Return an iterator for accessing the arguments of this cursor."""
num_args = conf.lib.clang_Cursor_getNumArguments(self)
@@ -1634,6 +1645,33 @@ class _CXUnsavedFile(Structure):
"""Helper for passing unsaved file arguments."""
_fields_ = [("name", c_char_p), ("contents", c_char_p), ('length', c_ulong)]
+# Functions calls through the python interface are rather slow. Fortunately,
+# for most symboles, we do not need to perform a function call. Their spelling
+# never changes and is consequently provided by this spelling cache.
+SpellingCache = {
+ # 0: CompletionChunk.Kind("Optional"),
+ # 1: CompletionChunk.Kind("TypedText"),
+ # 2: CompletionChunk.Kind("Text"),
+ # 3: CompletionChunk.Kind("Placeholder"),
+ # 4: CompletionChunk.Kind("Informative"),
+ # 5 : CompletionChunk.Kind("CurrentParameter"),
+ 6: '(', # CompletionChunk.Kind("LeftParen"),
+ 7: ')', # CompletionChunk.Kind("RightParen"),
+ 8: ']', # CompletionChunk.Kind("LeftBracket"),
+ 9: ']', # CompletionChunk.Kind("RightBracket"),
+ 10: '{', # CompletionChunk.Kind("LeftBrace"),
+ 11: '}', # CompletionChunk.Kind("RightBrace"),
+ 12: '<', # CompletionChunk.Kind("LeftAngle"),
+ 13: '>', # CompletionChunk.Kind("RightAngle"),
+ 14: ', ', # CompletionChunk.Kind("Comma"),
+ # 15: CompletionChunk.Kind("ResultType"),
+ 16: ':', # CompletionChunk.Kind("Colon"),
+ 17: ';', # CompletionChunk.Kind("SemiColon"),
+ 18: '=', # CompletionChunk.Kind("Equal"),
+ 19: ' ', # CompletionChunk.Kind("HorizontalSpace"),
+ # 20: CompletionChunk.Kind("VerticalSpace")
+}
+
class CompletionChunk:
class Kind:
def __init__(self, name):
@@ -1648,18 +1686,30 @@ def __repr__(self):
def __init__(self, completionString, key):
self.cs = completionString
self.key = key
+ self.__kindNumberCache = -1
def __repr__(self):
return "{'" + self.spelling + "', " + str(self.kind) + "}"
@CachedProperty
def spelling(self):
+ if self.__kindNumber in SpellingCache:
+ return SpellingCache[self.__kindNumber]
return conf.lib.clang_getCompletionChunkText(self.cs, self.key).spelling
+ # We do not use @CachedProperty here, as the manual implementation is
+ # apparently still significantly faster. Please profile carefully if you
+ # would like to add CachedProperty back.
+ @property
+ def __kindNumber(self):
+ if self.__kindNumberCache == -1:
+ self.__kindNumberCache = \
+ conf.lib.clang_getCompletionChunkKind(self.cs, self.key)
+ return self.__kindNumberCache
+
@CachedProperty
def kind(self):
- res = conf.lib.clang_getCompletionChunkKind(self.cs, self.key)
- return completionChunkKindMap[res]
+ return completionChunkKindMap[self.__kindNumber]
@CachedProperty
def string(self):
@@ -1672,19 +1722,19 @@ def string(self):
None
def isKindOptional(self):
- return self.kind == completionChunkKindMap[0]
+ return self.__kindNumber == 0
def isKindTypedText(self):
- return self.kind == completionChunkKindMap[1]
+ return self.__kindNumber == 1
def isKindPlaceHolder(self):
- return self.kind == completionChunkKindMap[3]
+ return self.__kindNumber == 3
def isKindInformative(self):
- return self.kind == completionChunkKindMap[4]
+ return self.__kindNumber == 4
def isKindResultType(self):
- return self.kind == completionChunkKindMap[15]
+ return self.__kindNumber == 15
completionChunkKindMap = {
0: CompletionChunk.Kind("Optional"),
@@ -3046,13 +3096,13 @@ def set_library_path(path):
Config.library_path = path
@staticmethod
- def set_library_file(file):
- """Set the exact location of libclang from"""
+ def set_library_file(filename):
+ """Set the exact location of libclang"""
if Config.loaded:
raise Exception("library file must be set before before using " \
"any other functionalities in libclang.")
- Config.library_file = path
+ Config.library_file = filename
@staticmethod
def set_compatibility_check(check_status):
Something went wrong with that request. Please try again.