Skip to content

Commit 6d09bda

Browse files
committed
Faster memory compare
1 parent 1be7135 commit 6d09bda

File tree

2 files changed

+10
-31
lines changed

2 files changed

+10
-31
lines changed

src/main/kotlin/com/lambda/graphics/buffer/DynamicByteBuffer.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ class DynamicByteBuffer private constructor(initialCapacity: Int) {
150150
val offset = position - pointer
151151

152152
memCopy(pointer, newPointer, offset)
153-
data = newBuffer
154153

154+
data = newBuffer
155155
pointer = newPointer
156156
position = newPointer + offset
157157
capacity = newCapacity
@@ -176,6 +176,13 @@ class DynamicByteBuffer private constructor(initialCapacity: Int) {
176176
capacity = newCapacity
177177
}
178178

179+
/**
180+
* Returns the relative index of the first mismatch between this and the given buffer, otherwise -1 if no mismatch.
181+
*
182+
* @see [ByteBuffer.mismatch]
183+
*/
184+
fun mismatch(other: DynamicByteBuffer) = data.mismatch(other.data)
185+
179186
companion object {
180187
/**
181188
* Creates a new DynamicByteBuffer with specified initial capacity
@@ -184,4 +191,4 @@ class DynamicByteBuffer private constructor(initialCapacity: Int) {
184191
fun dynamicByteBuffer(initialCapacity: Int) =
185192
DynamicByteBuffer(initialCapacity)
186193
}
187-
}
194+
}

src/main/kotlin/com/lambda/graphics/pipeline/PersistentBuffer.kt

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
package com.lambda.graphics.pipeline
1919

2020
import com.lambda.graphics.buffer.Buffer.Companion.createPipelineBuffer
21-
import com.lambda.graphics.buffer.DynamicByteBuffer
2221
import com.lambda.graphics.buffer.DynamicByteBuffer.Companion.dynamicByteBuffer
2322
import com.lambda.graphics.gl.kibibyte
24-
import org.lwjgl.system.MemoryUtil
2523
import org.lwjgl.system.MemoryUtil.memCopy
2624

2725
/**
@@ -64,7 +62,7 @@ class PersistentBuffer(
6462
}
6563

6664
if (snapshotData > 0 && snapshot.capacity >= byteBuffer.bytesPut) {
67-
if (memcmp(snapshot, byteBuffer, uploadOffset, dataCount)) return
65+
if (snapshot.mismatch(byteBuffer) >= 0) return
6866
}
6967

7068
glBuffer.update(uploadOffset, dataCount, dataStart)
@@ -90,30 +88,4 @@ class PersistentBuffer(
9088
}
9189

9290
fun use(block: () -> Unit) = glBuffer.bind { block() }
93-
94-
private fun memcmp(a: DynamicByteBuffer, b: DynamicByteBuffer, position: Long, size: Long): Boolean {
95-
if (a.capacity != b.capacity) return false
96-
97-
val end = position + size
98-
var head = position
99-
100-
// Process the aligned bytes in chunks of 8 until we've reached the end
101-
while (head + 8 <= end) {
102-
val first = MemoryUtil.memGetLong(a.pointer + head)
103-
val second = MemoryUtil.memGetLong(b.pointer + head)
104-
if (first != second) return false
105-
106-
head += 8
107-
}
108-
109-
while (head < end) {
110-
val first = MemoryUtil.memGetByte(a.pointer + head)
111-
val second = MemoryUtil.memGetByte(b.pointer + head)
112-
if (first != second) return false
113-
114-
head++
115-
}
116-
117-
return true
118-
}
11991
}

0 commit comments

Comments
 (0)