add various optimizations

This commit is contained in:
Redo0 2021-06-05 17:37:52 -05:00
parent bdcf2b384a
commit 5b8a1ea850
6 changed files with 71 additions and 32 deletions

View File

@ -1,14 +1,20 @@
local ffi = FFI
Gate = {} Gate = {}
function Gate.new(objref, definition) function Gate.new(objref, definition)
local o = { local o = {
--in_queue = ffi.new("bool", false),
--in_queue = false,
--in_queue = ffi.new("long long", 0),
in_queue = 0,
port_nets = {},
logic = definition.logic,
ports = {},
objref = objref, objref = objref,
definition = definition, definition = definition,
ports = {},
port_nets = {},
in_queue = false,
logic = definition.logic,
} }
return o return o
end end

View File

@ -1,19 +1,27 @@
local ffi = FFI
Group = {} Group = {}
function Group.new() function Group.new()
local o = { local o = {
--state = ffi.new("long long", 0),
state = 0, state = 0,
--state_num = ffi.new("long long", 0),
state_num = 0,
--in_queue = ffi.new("bool", false),
--in_queue = false,
--in_queue = ffi.new("long long", 0),
in_queue = 0,
gates_update = {},
num_gates_update = 0,
fxstate = 0, fxstate = 0,
update_tick = 0, update_tick = 0,
wires = {}, wires = {},
out_ports = {}, out_ports = {},
in_ports = {}, in_ports = {},
gates_update = {},
state_num = 0,
in_queue = false,
nwires = 0, nwires = 0,
nout_ports = 0, nout_ports = 0,
nin_ports = 0, nin_ports = 0,
@ -178,7 +186,10 @@ function Group.setstate(group, state)
group.state = state group.state = state
group.update_tick = sim.current_tick group.update_tick = sim.current_tick
for k, gate in ipairs(group.gates_update) do --for k, gate in ipairs(group.gates_update) do
local len = group.num_gates_update
for i = 1, len do
local gate = group.gates_update[i]
Simulation.queuegate(sim, gate) Simulation.queuegate(sim, gate)
end end
@ -192,9 +203,11 @@ end
function Group.rebuild_ports(group) function Group.rebuild_ports(group)
group.gates_update = {} group.gates_update = {}
group.num_gates_update = 0
for k, port in pairs(group.in_ports) do for k, port in pairs(group.in_ports) do
if port.causeupdate then if port.causeupdate then
array_add(group.gates_update, Port.getgate(port)) array_add(group.gates_update, Port.getgate(port))
group.num_gates_update = group.num_gates_update + 1
end end
end end
end end

View File

@ -157,7 +157,7 @@ while 1 do
local objref = tonumber(data[i+1]) local objref = tonumber(data[i+1])
local definition = Simulation.getdefinitionbyref(sim, tonumber(data[i+2])) local definition = Simulation.getdefinitionbyref(sim, tonumber(data[i+2]))
assert(definition, "No gate definition for objref "..objref) assert(definition, "No gate definition for objref "..objref.." defref "..tonumber(data[i+1]))
local position = vectotable(data[i+3]) local position = vectotable(data[i+3])
local rotation = tonumber(data[i+4]) local rotation = tonumber(data[i+4])
@ -248,7 +248,7 @@ while 1 do
local group = Wire.getgroup(wire) local group = Wire.getgroup(wire)
local numwires = 0; for k, wire2 in pairs(group.wires ) do numwires = numwires +1 end local numwires = 0; for k, wire2 in pairs(group.wires ) do numwires = numwires +1 end
local numportsi = 0; for k, port in pairs(group.in_ports ) do numportsi = numportsi+1 end local numportsi = 0; for k, port in pairs(group.in_ports ) do numportsi = numportsi+1 end
local numgatesu = #group.gates_update local numgatesu = group.num_gates_update
local numportso = 0; local numportson=0; local numportso = 0; local numportson=0;
for k, port in pairs(group.out_ports) do for k, port in pairs(group.out_ports) do
numportso = numportso+1 numportso = numportso+1

View File

@ -1,4 +1,6 @@
local ffi = FFI
PortTypes = { PortTypes = {
output = 0, output = 0,
input = 1, input = 1,
@ -17,13 +19,15 @@ Port = {}
function Port.new(type, direction, position, causeupdate, idx) function Port.new(type, direction, position, causeupdate, idx)
local o = { local o = {
--state = ffi.new("long long", 0),
state = 0,
group = nil,
type = type, type = type,
direction = direction, direction = direction,
position = position, position = position,
causeupdate = causeupdate, causeupdate = causeupdate,
state = 0,
gate = nil, gate = nil,
group = nil,
idx = idx, idx = idx,
} }
return o return o

View File

@ -3,25 +3,26 @@ Simulation = {}
function Simulation.new(sim) function Simulation.new(sim)
local o = { local o = {
groupqueue = {},
num_groupqueue = 0,
gatequeue = {},
num_gatequeue = 0,
groupfxqueue = {},
current_tick = 0,
definitions = {}, definitions = {},
wires = {}, wires = {},
gates = {}, gates = {},
nwires = 0, nwires = 0,
ngates = 0, ngates = 0,
ninports = 0, ninports = 0,
noutports = 0, noutports = 0,
groupqueue = {},
groupfxqueue = {},
gatequeue = {},
initqueue = nil, initqueue = nil,
inputqueue = nil, inputqueue = nil,
tickqueue = {}, tickqueue = {},
callbacks = nil, callbacks = nil,
current_tick = 0,
} }
setmetatable(o, sim) setmetatable(o, sim)
sim.__index = sim sim.__index = sim
@ -237,9 +238,10 @@ end
-- Logic Critical -- Logic Critical
function Simulation.queuegate(sim, gate) function Simulation.queuegate(sim, gate)
if not gate.in_queue then if gate.in_queue==0 then
table.insert(sim.gatequeue, gate) table.insert(sim.gatequeue, gate)
gate.in_queue = true sim.num_gatequeue = sim.num_gatequeue + 1
gate.in_queue = 1
end end
end end
@ -263,22 +265,28 @@ end
-- Logic Critical -- Logic Critical
function Simulation.queuegroup(sim, group) function Simulation.queuegroup(sim, group)
if not group.in_queue then if group.in_queue==0 then
table.insert(sim.groupqueue, group) --table.insert(sim.groupqueue, group)
group.in_queue = true sim.groupqueue[sim.num_groupqueue+1] = group
sim.num_groupqueue = sim.num_groupqueue + 1
group.in_queue = 1
end end
end end
function Simulation.dequeuegroup(sim, group) function Simulation.dequeuegroup(sim, group)
if group.in_queue then if group.in_queue~=0 then
array_remove(sim.groupqueue, group) array_remove(sim.groupqueue, group)
sim.num_groupqueue = sim.num_groupqueue - 1
group.in_queue = 0
end end
sim.groupfxqueue[group] = nil sim.groupfxqueue[group] = nil
end end
function Simulation.dequeuegate(sim, gate) function Simulation.dequeuegate(sim, gate)
if gate.in_queue then if gate.in_queue~=0 then
array_remove(sim.gatequeue, gate) array_remove(sim.gatequeue, gate)
sim.num_gatequeue = sim.num_gatequeue - 1
gate.in_queue = 0
end end
if sim.inputqueue~=nil then sim.inputqueue[gate] = nil end if sim.inputqueue~=nil then sim.inputqueue[gate] = nil end
if sim.initqueue ~=nil then sim.initqueue [gate] = nil end if sim.initqueue ~=nil then sim.initqueue [gate] = nil end
@ -298,11 +306,15 @@ end
-- Logic Critical -- Logic Critical
function Simulation.ticklogic(sim) function Simulation.ticklogic(sim)
for k, group in ipairs(sim.groupqueue) do --for k, group in ipairs(sim.groupqueue) do
local len = sim.num_groupqueue
for i = 1, len do
local group = sim.groupqueue[i]
Group.update(group) Group.update(group)
group.in_queue = false group.in_queue = 0
end end
sim.groupqueue = {} sim.groupqueue = {}
sim.num_groupqueue = 0
if sim.tickqueue[sim.current_tick] ~= nil then if sim.tickqueue[sim.current_tick] ~= nil then
for i, gate in pairs(sim.tickqueue[sim.current_tick]) do for i, gate in pairs(sim.tickqueue[sim.current_tick]) do
@ -311,11 +323,15 @@ function Simulation.ticklogic(sim)
sim.tickqueue[sim.current_tick] = nil sim.tickqueue[sim.current_tick] = nil
end end
for k, gate in ipairs(sim.gatequeue) do --for k, gate in ipairs(sim.gatequeue) do
local len = sim.num_gatequeue
for i = 1, len do
local gate = sim.gatequeue[i]
gate.logic(gate) gate.logic(gate)
gate.in_queue = false gate.in_queue = 0
end end
sim.gatequeue = {} sim.gatequeue = {}
sim.num_gatequeue = 0
sim.current_tick = sim.current_tick + 1 sim.current_tick = sim.current_tick + 1
end end

View File

@ -20,7 +20,7 @@ function Wire.setlayer(wire, layer)
end end
function Wire.update(wire) function Wire.update(wire)
client:send("WU\t" .. wire.group.state .. "\t" .. wire.objref .. "\n") client:send("WU\t" .. (wire.group.state~=0 and "1" or "0") .. "\t" .. wire.objref .. "\n")
end end
function Wire.setgroup(wire, group) function Wire.setgroup(wire, group)