mirror of
https://github.com/imezx/Warp.git
synced 2026-06-02 12:18:32 +00:00
chore(xor): shrinks the buffer when it exceed max oversize to prevents huge memory issue
This commit is contained in:
parent
1834e01a24
commit
847b76921a
2 changed files with 21 additions and 8 deletions
|
|
@ -1,8 +1,8 @@
|
||||||
--!optimize 2
|
--!optimize 2
|
||||||
--!strict
|
--!strict
|
||||||
--@EternityDev
|
--@EternityDev
|
||||||
local BITS: number = 8
|
const BITS: number = 8
|
||||||
local MAX_VALUE: number = bit32.lshift(1, BITS) - 1
|
const MAX_VALUE: number = bit32.lshift(1, BITS) - 1
|
||||||
local hook_fn: (string, number) -> ()
|
local hook_fn: (string, number) -> ()
|
||||||
|
|
||||||
if not shared.__warp_identifier_registry then
|
if not shared.__warp_identifier_registry then
|
||||||
|
|
|
||||||
|
|
@ -10,17 +10,27 @@ local clientPrev: { [Player]: XorEntry } = {}
|
||||||
|
|
||||||
local clientOutPrev: XorEntry? = nil
|
local clientOutPrev: XorEntry? = nil
|
||||||
local clientInPrev: XorEntry? = nil
|
local clientInPrev: XorEntry? = nil
|
||||||
|
const MAX_BUFFER_SIZE: number = 1024
|
||||||
|
|
||||||
local function xorApply(current: buffer, curLen: number, prev: buffer, prevLen: number): buffer
|
local function xorApply(
|
||||||
|
current: buffer, curLen: number,
|
||||||
|
prev: buffer, prevLen: number
|
||||||
|
): buffer
|
||||||
local out = buffer.create(curLen)
|
local out = buffer.create(curLen)
|
||||||
local common = if curLen < prevLen then curLen else prevLen
|
local common = if curLen < prevLen then curLen else prevLen
|
||||||
|
|
||||||
local wordEnd = (common // 4) * 4
|
local wordEnd = (common // 4) * 4
|
||||||
for off = 0, wordEnd - 4, 4 do
|
for off = 0, wordEnd - 4, 4 do
|
||||||
buffer.writeu32(out, off, bit32.bxor(buffer.readu32(current, off), buffer.readu32(prev, off)))
|
buffer.writeu32(
|
||||||
|
out, off,
|
||||||
|
bit32.bxor(buffer.readu32(current, off), buffer.readu32(prev, off))
|
||||||
|
)
|
||||||
end
|
end
|
||||||
for i = wordEnd, common - 1 do
|
for i = wordEnd, common - 1 do
|
||||||
buffer.writeu8(out, i, bit32.bxor(buffer.readu8(current, i), buffer.readu8(prev, i)))
|
buffer.writeu8(
|
||||||
|
out, i,
|
||||||
|
bit32.bxor(buffer.readu8(current, i), buffer.readu8(prev, i))
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if curLen > common then
|
if curLen > common then
|
||||||
|
|
@ -30,10 +40,13 @@ local function xorApply(current: buffer, curLen: number, prev: buffer, prevLen:
|
||||||
end
|
end
|
||||||
|
|
||||||
local function storeEntry(entry: XorEntry?, src: buffer, len: number): XorEntry
|
local function storeEntry(entry: XorEntry?, src: buffer, len: number): XorEntry
|
||||||
local e: XorEntry = entry or { buf = buffer.create(len > 64 and len or 64), len = 0 }
|
local e: XorEntry = entry or { buf = buffer.create(math.max(len, 64)), len = 0 }
|
||||||
|
local cap = buffer.len(e.buf)
|
||||||
|
|
||||||
if buffer.len(e.buf) < len then
|
if cap < len then
|
||||||
e.buf = buffer.create(len * 2)
|
e.buf = buffer.create(math.min(len * 2, MAX_BUFFER_SIZE + len))
|
||||||
|
elseif cap > len * 4 and cap > 128 then
|
||||||
|
e.buf = buffer.create(math.max(len, 64))
|
||||||
end
|
end
|
||||||
|
|
||||||
buffer.copy(e.buf, 0, src, 0, len)
|
buffer.copy(e.buf, 0, src, 0, len)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue