Skip to content

Commit

Permalink
Avoid unnecessary file IO when computing positions
Browse files Browse the repository at this point in the history
The exists call introduced in scala#8883 and scala#8897 lead to a slowdown
noticeable in benchmarks. Replace them by a check on the content length,
since SourceFile caches its content this should avoid any unnecessary
IO operation. This required changing the way SourceFile handles empty
files to have it return an empty string instead of crashing.
  • Loading branch information
smarter committed Jun 1, 2020
1 parent e5e489e commit 99574e2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
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

0 comments on commit 99574e2

Please sign in to comment.