Skip to content

Commit 6fad7b6

Browse files
authored
Merge pull request sbt#520 from shuttie/fix-dedup-heap-usage
do not buffer entry contents in memory while doing MergeStrategy.deduplicate
2 parents f0014e7 + 181a648 commit 6fad7b6

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/main/scala/sbtassembly/Assembly.scala

+4-3
Original file line numberDiff line numberDiff line change
@@ -699,21 +699,22 @@ object Assembly {
699699

700700
private[sbtassembly] def sha1 = MessageDigest.getInstance("SHA-1")
701701

702-
private[sbtassembly] def sha1Content(b: Array[Byte]): String =
703-
byteArrayInputStreamResource(b) { in =>
702+
private[sbtassembly] def sha1Content(stream: InputStream): String =
703+
{
704704
val messageDigest = sha1
705705
val buffer = new Array[Byte](8192)
706706

707707
@tailrec
708708
def read(): Unit = {
709-
val byteCount = in.read(buffer)
709+
val byteCount = stream.read(buffer)
710710
if (byteCount >= 0) {
711711
messageDigest.update(buffer, 0, byteCount)
712712
read()
713713
}
714714
}
715715

716716
read()
717+
stream.close()
717718
bytesToString(messageDigest.digest())
718719
}
719720

src/main/scala/sbtassembly/MergeStrategy.scala

+4-3
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ object MergeStrategy {
122122
* Verifies if all the conflicts have the same content, otherwise error out
123123
*/
124124
val deduplicate: MergeStrategy = MergeStrategy("Deduplicate") { conflicts =>
125-
val conflictContents = conflicts.map(_.stream()).map(Streamable.bytes(_))
126-
val fingerprints = Set() ++ conflictContents.map(sbtassembly.Assembly.sha1Content)
125+
val fingerprints = conflicts.map(dep =>
126+
sbtassembly.Assembly.sha1Content(dep.stream())
127+
).toSet
127128
if (fingerprints.size == 1)
128-
Right(Vector(JarEntry(conflicts.head.target, () => new ByteArrayInputStream(conflictContents.head))))
129+
Right(Vector(JarEntry(conflicts.head.target, conflicts.head.stream)))
129130
else
130131
Left(
131132
s"Deduplicate found different file contents in the following:$newLineIndented${conflicts.mkString(newLineIndented)}"

0 commit comments

Comments
 (0)