perf: fix PacketBatcher O(n) length/1 to O(1) counter
Ultraworked with Sisyphus Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
parent
21ab9fb20c
commit
ae1194ebb0
1 changed files with 11 additions and 9 deletions
|
|
@ -10,11 +10,12 @@ defmodule AprsmeWeb.MapLive.PacketBatcher do
|
||||||
# milliseconds
|
# milliseconds
|
||||||
@batch_timeout 100
|
@batch_timeout 100
|
||||||
|
|
||||||
defstruct [:parent_pid, :buffer, :timer_ref]
|
defstruct [:parent_pid, :buffer, :timer_ref, :buffer_size]
|
||||||
|
|
||||||
@type t :: %__MODULE__{
|
@type t :: %__MODULE__{
|
||||||
parent_pid: pid(),
|
parent_pid: pid(),
|
||||||
buffer: [map()],
|
buffer: [map()],
|
||||||
|
buffer_size: non_neg_integer(),
|
||||||
timer_ref: reference() | nil
|
timer_ref: reference() | nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -50,27 +51,28 @@ defmodule AprsmeWeb.MapLive.PacketBatcher do
|
||||||
%__MODULE__{
|
%__MODULE__{
|
||||||
parent_pid: parent_pid,
|
parent_pid: parent_pid,
|
||||||
buffer: [],
|
buffer: [],
|
||||||
|
buffer_size: 0,
|
||||||
timer_ref: nil
|
timer_ref: nil
|
||||||
}}
|
}}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_cast({:add_packet, packet}, state) do
|
def handle_cast({:add_packet, packet}, state) do
|
||||||
# Add packet to buffer
|
|
||||||
new_buffer = [packet | state.buffer]
|
new_buffer = [packet | state.buffer]
|
||||||
|
new_size = state.buffer_size + 1
|
||||||
|
|
||||||
# Cancel existing timer if any
|
# Cancel existing timer if any
|
||||||
state = cancel_timer(state)
|
state = cancel_timer(state)
|
||||||
|
|
||||||
# Check if we should process immediately or wait
|
# Check if we should process immediately or wait
|
||||||
if length(new_buffer) >= @batch_size do
|
if new_size >= @batch_size do
|
||||||
# Process immediately
|
# Process immediately
|
||||||
process_batch(new_buffer, state.parent_pid)
|
process_batch(new_buffer, state.parent_pid)
|
||||||
{:noreply, %{state | buffer: [], timer_ref: nil}}
|
{:noreply, %{state | buffer: [], buffer_size: 0, timer_ref: nil}}
|
||||||
else
|
else
|
||||||
# Set timer for batch timeout
|
# Set timer for batch timeout
|
||||||
timer_ref = Process.send_after(self(), :batch_timeout, @batch_timeout)
|
timer_ref = Process.send_after(self(), :batch_timeout, @batch_timeout)
|
||||||
{:noreply, %{state | buffer: new_buffer, timer_ref: timer_ref}}
|
{:noreply, %{state | buffer: new_buffer, buffer_size: new_size, timer_ref: timer_ref}}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -78,20 +80,20 @@ defmodule AprsmeWeb.MapLive.PacketBatcher do
|
||||||
def handle_cast(:flush, state) do
|
def handle_cast(:flush, state) do
|
||||||
state = cancel_timer(state)
|
state = cancel_timer(state)
|
||||||
|
|
||||||
if state.buffer != [] do
|
if state.buffer_size > 0 do
|
||||||
process_batch(state.buffer, state.parent_pid)
|
process_batch(state.buffer, state.parent_pid)
|
||||||
end
|
end
|
||||||
|
|
||||||
{:noreply, %{state | buffer: [], timer_ref: nil}}
|
{:noreply, %{state | buffer: [], buffer_size: 0, timer_ref: nil}}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_info(:batch_timeout, state) do
|
def handle_info(:batch_timeout, state) do
|
||||||
if state.buffer != [] do
|
if state.buffer_size > 0 do
|
||||||
process_batch(state.buffer, state.parent_pid)
|
process_batch(state.buffer, state.parent_pid)
|
||||||
end
|
end
|
||||||
|
|
||||||
{:noreply, %{state | buffer: [], timer_ref: nil}}
|
{:noreply, %{state | buffer: [], buffer_size: 0, timer_ref: nil}}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue