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 = {}
function Gate.new(objref, definition)
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,
definition = definition,
ports = {},
port_nets = {},
in_queue = false,
logic = definition.logic,
}
return o
end

View File

@ -1,19 +1,27 @@
local ffi = FFI
Group = {}
function Group.new()
local o = {
--state = ffi.new("long long", 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,
update_tick = 0,
wires = {},
out_ports = {},
in_ports = {},
gates_update = {},
state_num = 0,
in_queue = false,
nwires = 0,
nout_ports = 0,
nin_ports = 0,
@ -178,7 +186,10 @@ function Group.setstate(group, state)
group.state = state
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)
end
@ -192,9 +203,11 @@ end
function Group.rebuild_ports(group)
group.gates_update = {}
group.num_gates_update = 0
for k, port in pairs(group.in_ports) do
if port.causeupdate then
array_add(group.gates_update, Port.getgate(port))
group.num_gates_update = group.num_gates_update + 1
end
end
end

View File

@ -157,7 +157,7 @@ while 1 do
local objref = tonumber(data[i+1])
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 rotation = tonumber(data[i+4])
@ -248,7 +248,7 @@ while 1 do
local group = Wire.getgroup(wire)
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 numgatesu = #group.gates_update
local numgatesu = group.num_gates_update
local numportso = 0; local numportson=0;
for k, port in pairs(group.out_ports) do
numportso = numportso+1

View File

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

View File

@ -3,25 +3,26 @@ Simulation = {}
function Simulation.new(sim)
local o = {
groupqueue = {},
num_groupqueue = 0,
gatequeue = {},
num_gatequeue = 0,
groupfxqueue = {},
current_tick = 0,
definitions = {},
wires = {},
gates = {},
nwires = 0,
ngates = 0,
ninports = 0,
noutports = 0,
groupqueue = {},
groupfxqueue = {},
gatequeue = {},
initqueue = nil,
inputqueue = nil,
tickqueue = {},
callbacks = nil,
current_tick = 0,
}
setmetatable(o, sim)
sim.__index = sim
@ -237,9 +238,10 @@ end
-- Logic Critical
function Simulation.queuegate(sim, gate)
if not gate.in_queue then
if gate.in_queue==0 then
table.insert(sim.gatequeue, gate)
gate.in_queue = true
sim.num_gatequeue = sim.num_gatequeue + 1
gate.in_queue = 1
end
end
@ -263,22 +265,28 @@ end
-- Logic Critical
function Simulation.queuegroup(sim, group)
if not group.in_queue then
table.insert(sim.groupqueue, group)
group.in_queue = true
if group.in_queue==0 then
--table.insert(sim.groupqueue, group)
sim.groupqueue[sim.num_groupqueue+1] = group
sim.num_groupqueue = sim.num_groupqueue + 1
group.in_queue = 1
end
end
function Simulation.dequeuegroup(sim, group)
if group.in_queue then
if group.in_queue~=0 then
array_remove(sim.groupqueue, group)
sim.num_groupqueue = sim.num_groupqueue - 1
group.in_queue = 0
end
sim.groupfxqueue[group] = nil
end
function Simulation.dequeuegate(sim, gate)
if gate.in_queue then
if gate.in_queue~=0 then
array_remove(sim.gatequeue, gate)
sim.num_gatequeue = sim.num_gatequeue - 1
gate.in_queue = 0
end
if sim.inputqueue~=nil then sim.inputqueue[gate] = nil end
if sim.initqueue ~=nil then sim.initqueue [gate] = nil end
@ -298,11 +306,15 @@ end
-- Logic Critical
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.in_queue = false
group.in_queue = 0
end
sim.groupqueue = {}
sim.num_groupqueue = 0
if sim.tickqueue[sim.current_tick] ~= nil then
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
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.in_queue = false
gate.in_queue = 0
end
sim.gatequeue = {}
sim.num_gatequeue = 0
sim.current_tick = sim.current_tick + 1
end

View File

@ -20,7 +20,7 @@ function Wire.setlayer(wire, layer)
end
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
function Wire.setgroup(wire, group)