Skip to content

Commit baeefbe

Browse files
committed
Read full message body at once when larger than 4MB
When a message (much) larger than 4MB is read from an rdq file in `rabbit_msg_store:scan/6)` adjust the read size to the full message size instead of appending it together from 4MB chunks, which leaves a lot of garbage and memory fragmentation behind.
1 parent 50548d3 commit baeefbe

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

deps/rabbit/src/rabbit_msg_store.erl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ scan_file_for_valid_messages(Path) ->
13961396
{ok, Fd} ->
13971397
{ok, FileSize} = file:position(Fd, eof),
13981398
{ok, _} = file:position(Fd, bof),
1399-
Messages = scan(<<>>, Fd, 0, FileSize, #{}, []),
1399+
Messages = scan(<<>>, Fd, 0, FileSize, #{}, [], ?SCAN_BLOCK_SIZE),
14001400
ok = file:close(Fd),
14011401
case Messages of
14021402
[] ->
@@ -1412,8 +1412,8 @@ scan_file_for_valid_messages(Path) ->
14121412
Reason}}
14131413
end.
14141414

1415-
scan(Buffer, Fd, Offset, FileSize, MsgIdsFound, Acc) ->
1416-
case file:read(Fd, ?SCAN_BLOCK_SIZE) of
1415+
scan(Buffer, Fd, Offset, FileSize, MsgIdsFound, Acc, ScanSize) ->
1416+
case file:read(Fd, ScanSize) of
14171417
eof ->
14181418
Acc;
14191419
{ok, Data0} ->
@@ -1448,10 +1448,12 @@ scan_data(<<Size:64, MsgIdAndMsg:Size/binary, 255, Rest/bits>> = Data,
14481448
%% This might be the start of a message.
14491449
scan_data(<<Size:64, Rest/bits>> = Data, Fd, Offset, FileSize, MsgIdsFound, Acc)
14501450
when byte_size(Rest) < Size + 1, Size < FileSize - Offset ->
1451-
scan(Data, Fd, Offset, FileSize, MsgIdsFound, Acc);
1451+
RemainingMsgSize = Size - byte_size(Rest),
1452+
ScanSize = max(?SCAN_BLOCK_SIZE, RemainingMsgSize + 1),
1453+
scan(Data, Fd, Offset, FileSize, MsgIdsFound, Acc, ScanSize);
14521454
scan_data(Data, Fd, Offset, FileSize, MsgIdsFound, Acc)
14531455
when byte_size(Data) < 8 ->
1454-
scan(Data, Fd, Offset, FileSize, MsgIdsFound, Acc);
1456+
scan(Data, Fd, Offset, FileSize, MsgIdsFound, Acc, ?SCAN_BLOCK_SIZE);
14551457
%% This is definitely not a message. Try the next byte.
14561458
scan_data(<<_, Rest/bits>>, Fd, Offset, FileSize, MsgIdsFound, Acc) ->
14571459
scan_data(Rest, Fd, Offset + 1, FileSize, MsgIdsFound, Acc).

0 commit comments

Comments
 (0)