Improvement to getCustomOptions to prevent premature loading of elementInfo #4779
+18
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ICompilationUnit.getCustomOptions() has the following javadoc:
/**
setOptionscalled*/
If the IElementInfo has not yet been loaded or does not yet exist, then nobody could have possibly called setOptions on it. Options on a per-cu basis are not persisted in any way, so the only way to set options on a unit is if it's element info has been loaded.
One of the side effects of loading the element info is that source indexer gets called or an attempt to build the structure of the file gets initiated, which may parse the file in order to do that. In many cases, the entire purpose of calling getCustomOptions is to discover what options should be used to parse the file. If the act of calling getCustomOptions to use when parsing the file has the side effect of parsing the file beforehand, then the work of parsing is done twice to no benefit.
For example:
In this example, the purpose of calling getOptions() is to discover what options to use when creating the AST. But the act of calling getOptions(true) loads the IElementInfo, which then calls openWhenClosed(), and then calls buildStructure(). Almost all calls to buildStructure will, in fact, parse the file in one way or another.
Upon returning back up the stack, the parser in the snippet above will, also, parse the file.
The end result is the file gets parsed twice, and the CU gets opened. But the javadoc does not specify that as the result. The javadoc says it should "Returns the table of the current custom options for this ICompilationUnit" and if setOptions() hasn't been called, it should just return empty.
I think checking if the element info has been loaded should be sufficient. If it hasn't been created, nobody could have called setOptions() on it, and the empty map should be returned. This would avoid the first unnecessary parse as a side effect of loading the options.
I suspect, but am unable to prove conclusively, that this is a safe patch.
What it does
How to test
Author checklist