Hi! I just benchmarked each version, both in native and non-native.
Write non-native: My implementation is around 1.5x faster.
Write native: My implementations is still around 1.5x faster.
local COUNT = 1000000
local function writei24KII( b: buffer, offset: number, value: number ): ()
buffer.writei16( b, offset, value//2^8 )
buffer.writeu8( b, offset+2, value )
end
local start = os.clock()
for i = 1, COUNT do
local new = buffer.create( 3 )
local number = math.random( -2^31, -2^31-1 )
writei24KII( new, 0, number )
end
warn( os.clock() - start ) -- ~0.06s nn, ~0.04s n
local COUNT = 1000000
local TempBuffer = buffer.create( 4 )
local function writei24TNA( b: buffer, o: number, value: number)
buffer.writei32(TempBuffer, 0, value)
buffer.copy(b, o, TempBuffer, 0, 3)
end
local start = os.clock()
for i = 1, COUNT do
local new = buffer.create( 3 )
local number = math.random( -2^31, -2^31-1 )
writei24TNA( new, 0, number )
end
warn( os.clock() - start ) -- ~0.09s nn, ~0.06s n
Read non-native: Mine is around 3x faster.
Read: Yours is around 1.5x faster. I didn’t expect this difference!
local COUNT = 1000000
local function writei24KII( b: buffer, offset: number, value: number ): ()
buffer.writei16( b, offset, value//2^8 )
buffer.writeu8( b, offset+2, value )
end
local buffers: { buffer } = table.create( COUNT )
for i = 1, COUNT do
local new = buffer.create( 3 )
writei24KII( new, 0, math.random( -2^31, -2^31-1 ) )
buffers[ i ] = new
end
--
local function readi24KII( b: buffer, offset: number ): number
return 2^8*buffer.readi16( b, offset ) + buffer.readu8( b, offset + 2 )
end
local start = os.clock()
for _, b in buffers do
readi24KII( b, 0 )
end
warn( os.clock() - start ) -- ~0.02s nn, ~0.004s n
local COUNT = 1000000
local function writei24KII( b: buffer, offset: number, value: number ): ()
buffer.writei16( b, offset, value//2^8 )
buffer.writeu8( b, offset+2, value )
end
local buffers: { buffer } = table.create( COUNT )
for i = 1, COUNT do
local new = buffer.create( 3 )
writei24KII( new, 0, math.random( -2^31, -2^31-1 ) )
buffers[ i ] = new
end
--
local TempBuffer = buffer.create(4)
local function readi24TNA(b: buffer, o: number): number
buffer.copy(TempBuffer, 0, b, o, 3)
-- Sign extend
buffer.writeu8(TempBuffer, 3, if buffer.readu8(TempBuffer, 2) >= 128 then 0xFF else 0)
return buffer.readi32(TempBuffer, 0)
end
local start = os.clock()
for _, b in buffers do
readi24TNA( b, 0 )
end
warn( os.clock() - start ) -- ~0.06s nn, ~0.0025s n
But… I don’t think this conversation would’ve needed to happen if readi24/writei24 (along with the unsigned variants) were native to Luau, and would honestly probably be faster than both of our implementations. Hint hint. Wink wink. Please.