Skip to content

Avoid unnecessary file IO when computing positions #8935

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

Merged
merged 1 commit into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/util/SourceFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends

def maybeIncomplete: Boolean = _maybeInComplete

def this(file: AbstractFile, codec: Codec) = this(file, new String(file.toByteArray, codec.charSet).toCharArray)
def this(file: AbstractFile, codec: Codec) =
// It would be cleaner to check if the file exists instead of catching
// an exception, but it turns out that Files.exists is remarkably slow,
// at least on Java 8 (https://rules.sonarsource.com/java/tag/performance/RSPEC-3725),
// this is significant enough to show up in our benchmarks.
this(file,
try new String(file.toByteArray, codec.charSet).toCharArray
catch case _: java.nio.file.NoSuchFileException => Array[Char]())

/** Tab increment; can be overridden */
def tabInc: Int = 8
Expand Down
10 changes: 5 additions & 5 deletions compiler/src/dotty/tools/dotc/util/SourcePosition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ extends interfaces.SourcePosition with Showable {
def beforeAndAfterPoint: (List[Int], List[Int]) =
lineOffsets.partition(_ <= point)

def column: Int = if (source.file.exists) source.column(point) else -1
def column: Int = if (source.content().length != 0) source.column(point) else -1

def start: Int = span.start
def startLine: Int = if (source.file.exists) source.offsetToLine(start) else -1
def startColumn: Int = if (source.file.exists) source.column(start) else -1
def startLine: Int = if (source.content().length != 0) source.offsetToLine(start) else -1
def startColumn: Int = if (source.content().length != 0) source.column(start) else -1
def startColumnPadding: String = source.startColumnPadding(start)

def end: Int = span.end
def endLine: Int = if (source.file.exists) source.offsetToLine(end) else -1
def endColumn: Int = if (source.file.exists) source.column(end) else -1
def endLine: Int = if (source.content().length != 0) source.offsetToLine(end) else -1
def endColumn: Int = if (source.content().length != 0) source.column(end) else -1

def withOuter(outer: SourcePosition): SourcePosition = SourcePosition(source, span, outer)
def withSpan(range: Span) = SourcePosition(source, range, outer)
Expand Down
8 changes: 4 additions & 4 deletions sbt-bridge/src/xsbt/DelegatingReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public Optional<java.io.File> sourceFile() {
return Optional.ofNullable(src.file().file());
}
public Optional<Integer> line() {
if (!src.file().exists())
if (src.content().length == 0)
return Optional.empty();

int line = pos.line() + 1;
Expand All @@ -101,7 +101,7 @@ public Optional<Integer> line() {
return Optional.of(line);
}
public String lineContent() {
if (!src.file().exists())
if (src.content().length == 0)
return "";

String line = pos.lineContent();
Expand All @@ -116,13 +116,13 @@ public Optional<Integer> offset() {
return Optional.of(pos.point());
}
public Optional<Integer> pointer() {
if (!src.file().exists())
if (src.content().length == 0)
return Optional.empty();

return Optional.of(pos.point() - src.startOfLine(pos.point()));
}
public Optional<String> pointerSpace() {
if (!src.file().exists())
if (src.content().length == 0)
return Optional.empty();

String lineContent = this.lineContent();
Expand Down