Skip to content

Fix tasty hash #4865

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 10 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from 7 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
23 changes: 23 additions & 0 deletions compiler/src/dotty/tools/dotc/core/tasty/TastyHash.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dotty.tools.dotc.core.tasty

object TastyHash {

/** Returns a non-cryptographic 64-bit hash of the array.
*
* from https://en.wikipedia.org/wiki/PJW_hash_function#Algorithm
*/
def pjwHash64(data: Array[Byte]): Long = {
var h = 0L
var i = 0
while (i < data.length) {
val d = data(i) & 0xFFL // Interpret byte as unsigned byte
h = (h << 8) + d
val high = h & 0xFF00000000000000L
h ^= high >> 48L
h &= ~high
i += 1
}
h
}

}
33 changes: 11 additions & 22 deletions compiler/src/dotty/tools/dotc/core/tasty/TastyPickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ class TastyPickler(val rootCls: ClassSymbol) {
sections += ((nameBuffer.nameIndex(name.toTermName), buf))

def assembleParts(): Array[Byte] = {
def lengthWithLength(buf: TastyBuffer) = {
buf.assemble()
def lengthWithLength(buf: TastyBuffer) =
buf.length + natSize(buf.length)
}

val uuidLow: Long = pjwHash64(nameBuffer.bytes)
val uuidHi: Long = sections.iterator.map(x => pjwHash64(x._2.bytes)).fold(0L)(_ ^ _)
nameBuffer.assemble()
sections.foreach(_._2.assemble())

val nameBufferHash = TastyHash.pjwHash64(nameBuffer.bytes)
val treeSectionHash +: otherSectionHashes = sections.map(x => TastyHash.pjwHash64(x._2.bytes))

// Hash of name table and tree
val uuidLow: Long = nameBufferHash ^ treeSectionHash
// Hash of positions, comments and any additional section
val uuidHi: Long = otherSectionHashes.fold(0L)(_ ^ _)

val headerBuffer = {
val buf = new TastyBuffer(header.length + 24)
Expand Down Expand Up @@ -72,21 +78,4 @@ class TastyPickler(val rootCls: ClassSymbol) {

val treePkl = new TreePickler(this)

/** Returns a non-cryptographic 64-bit hash of the array.
*
* from https://en.wikipedia.org/wiki/PJW_hash_function#Implementation
*/
private def pjwHash64(data: Array[Byte]): Long = {
var h = 0L
var high = 0L
var i = 0
while (i < data.length) {
h = (h << 4) + data(i)
high = h & 0xF0000000L
h ^= high >> 24
h &= ~high
i += 1
}
h
}
}
25 changes: 25 additions & 0 deletions tests/run-with-compiler/tasty-hash.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import dotty.tools.dotc.core.tasty.TastyHash.pjwHash64

object Test {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write a junit test instead, they're faster to run.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

def main(args: Array[String]): Unit = {
testHash(0L, Array.empty)
testHash(0L, Array(0))
testHash(1L, Array(1))
testHash(0x7fL, Array(Byte.MaxValue))
testHash(0x80L, Array(Byte.MinValue))
testHash(0x101L, Array(1, 1))
testHash(0X10101L, Array(1, 1, 1))
testHash(0X1010101L, Array(1, 1, 1, 1))
testHash(0X101010101L, Array(1, 1, 1, 1, 1))
testHash(0X202020202L, Array(2, 2, 2, 2, 2))
testHash(0X1010101L, Array.fill(1024)(1))
testHash(0X55aa01fe55ab54ffL, Array.tabulate(1024)(_.toByte))
testHash(0x34545c16020230L, "abcdefghijklmnopqrstuvwxyz1234567890".getBytes)
}

def testHash(expected: Long, arr: Array[Byte]): Unit = {
val res = pjwHash64(arr)
assert(res == expected, s"Exprected 0x${expected.toHexString}L but got 0X${res.toHexString}L")
}

}