mirror of
				https://github.com/packwiz/packwiz-installer.git
				synced 2025-11-04 12:34:31 +01:00 
			
		
		
		
	Optimise memory usage while computing Murmur2
This commit is contained in:
		@@ -10,7 +10,7 @@ class Murmur2Hasher : IHasher {
 | 
			
		||||
		val tempBuffer = Buffer()
 | 
			
		||||
 | 
			
		||||
		override val hash: Hash by lazy(LazyThreadSafetyMode.NONE) {
 | 
			
		||||
			val data = computeNormalizedArray(internalBuffer.readByteArray())
 | 
			
		||||
			val data = internalBuffer.readByteArray()
 | 
			
		||||
			Murmur2Hash(Murmur2Lib.hash32(data, data.size, 1))
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -19,27 +19,42 @@ class Murmur2Hasher : IHasher {
 | 
			
		||||
			val out = delegate.read(tempBuffer, byteCount)
 | 
			
		||||
			if (out > -1) {
 | 
			
		||||
				sink.write(tempBuffer.clone(), out)
 | 
			
		||||
				internalBuffer.write(tempBuffer, out)
 | 
			
		||||
				computeNormalizedBufferFaster(tempBuffer, internalBuffer)
 | 
			
		||||
			}
 | 
			
		||||
			return out
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Credit to https://github.com/modmuss50/CAV2/blob/master/murmur.go
 | 
			
		||||
		private fun computeNormalizedArray(input: ByteArray): ByteArray {
 | 
			
		||||
			val output = ByteArray(input.size)
 | 
			
		||||
//		private fun computeNormalizedArray(input: ByteArray): ByteArray {
 | 
			
		||||
//			val output = ByteArray(input.size)
 | 
			
		||||
//			var index = 0
 | 
			
		||||
//			for (b in input) {
 | 
			
		||||
//				when (b) {
 | 
			
		||||
//					9.toByte(), 10.toByte(), 13.toByte(), 32.toByte() -> {}
 | 
			
		||||
//					else -> {
 | 
			
		||||
//						output[index] = b
 | 
			
		||||
//						index++
 | 
			
		||||
//					}
 | 
			
		||||
//				}
 | 
			
		||||
//			}
 | 
			
		||||
//			val outputTrimmed = ByteArray(index)
 | 
			
		||||
//			System.arraycopy(output, 0, outputTrimmed, 0, index)
 | 
			
		||||
//			return outputTrimmed
 | 
			
		||||
//		}
 | 
			
		||||
 | 
			
		||||
		private fun computeNormalizedBufferFaster(input: Buffer, output: Buffer) {
 | 
			
		||||
			var index = 0
 | 
			
		||||
			for (b in input) {
 | 
			
		||||
			val arr = input.readByteArray()
 | 
			
		||||
			for (b in arr) {
 | 
			
		||||
				when (b) {
 | 
			
		||||
					9.toByte(), 10.toByte(), 13.toByte(), 32.toByte() -> {}
 | 
			
		||||
					else -> {
 | 
			
		||||
						output[index] = b
 | 
			
		||||
						arr[index] = b
 | 
			
		||||
						index++
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			val outputTrimmed = ByteArray(index)
 | 
			
		||||
			System.arraycopy(output, 0, outputTrimmed, 0, index)
 | 
			
		||||
			return outputTrimmed
 | 
			
		||||
			output.write(arr, 0, index)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user