Skip to content

Instantly share code, notes, and snippets.

@eugeneia
Created December 17, 2018 11:36
Show Gist options
  • Save eugeneia/8cb8951f109ad3a613d96dbd945c861f to your computer and use it in GitHub Desktop.
Save eugeneia/8cb8951f109ad3a613d96dbd945c861f to your computer and use it in GitHub Desktop.
-- Observation: the natural push loop is slow at dropping packets. When a
-- packet doesn’t follow the happy path (either because it doesn’t meet the
-- test condition, or because the output link is full), the loop head for the
-- happy path is “replayed” for every “non-happy” packet because of the
-- side-traces created for error situations.
function A:push ()
local input, output = self.input.input, self.output.output
while not link.empty(input) do
local p = link.receive(input)
if test(p) then
-- do stuff with p
link.transmit(output, p)
else
-- error, discard p
packet.free(p)
end
end
end
-- Below are some alternative formulations of the loop that intend to force
-- different, more efficient traces.
function B:push ()
local input, output = self.input.input, self.output.output
repeat
-- Trace for happy path
while test(link.front(input)) do
local p = link.receive(input)
-- do stuff with p
link.transmit(output, p)
end
-- Trace for error path
while not test(link.front(input)) do
local p = link.receive(input)
-- error, discard p
packet.free(p)
end
until link.empty(input)
end
function C:push ()
local input, output = self.input.input, self.output.output
-- Trace for happy path
while not link.empty(input) do
if test(link.front(input)) then
local p = link.receive(input)
-- do stuff with p
link.transmit(output, p)
else
-- Trace for error path
repeat -- error, discard p
local p = link.receive(input)
packet.free(p)
until test(link.front(input))
end
end
end
function D:push ()
local input, output = self.input.input, self.output.output
while not link.empty(input) and not link.full(output) do
-- Trace for happy path
if test(link.front(input)) then
local p = link.receive(input)
-- do stuff with p
link.transmit(output, p)
else
-- Trace for error path
repeat -- error, discard p
local p = link.receive(input)
packet.free(p)
until test(link.front(input))
end
end
-- Trace for congestion path
while not link.empty(input) do
link.transmit(output, link.receive(input))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment