more optimizations: move port states into gates, inline net queue checks

This commit is contained in:
Redo0 2021-06-05 18:41:50 -05:00
parent 14c61e35d1
commit 19d2e36fd6
6 changed files with 38 additions and 44 deletions

View File

@ -5,13 +5,11 @@ 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 = {},
port_nets = {},
port_states = {},
objref = objref,
definition = definition,
@ -19,30 +17,33 @@ function Gate.new(objref, definition)
return o
end
function Gate.addport(gate, port)
gate.ports[#gate.ports+1] = port
Port.setgate(port, gate)
end
-- Logic Critical
function Gate.getportstate(gate, index)
--return gate[index*2].state
return gate.port_nets[index].state
end
-- Logic Critical
function Gate.setportstate(gate, index, state)
local port = gate.ports[index]
if state ~= port.state then
local group = port.group
group.state_num = group.state_num - port.state + state
port.state = state
--if state ~= gate[index*2+1] then
if state ~= gate.port_states[index] then
--local group = gate[index*2]
local group = gate.port_nets[index]
--group.state_num = group.state_num - gate[index*2+1] + state
group.state_num = group.state_num - gate.port_states[index] + state
--gate[index*2+1] = state
gate.port_states[index] = state
if (group.state_num>0) ~= (group.state==1) then
if (group.state_num>0) ~= (group.state==1) and (group.in_queue==0) then
Simulation.queuegroup(GSim, group)
end
end
end
function Gate.preinit(gate)
end
function Gate.initdata(gate)
gate.data = {}
end

View File

@ -82,7 +82,13 @@ function GateDefinition.constructgate(def, objref, position, rotation)
pos[2] = x
end
Gate.addport(gate, Port.new(type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, i))
local port = Port.new(type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, i, gate)
gate.ports[port.idx] = port
--gate[port.idx*2] = nil
--gate[port.idx*2+1] = 0
gate.port_nets[port.idx] = nil
gate.port_states[port.idx] = 0
end
return gate

View File

@ -5,13 +5,8 @@ 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,
@ -44,10 +39,12 @@ function Group.addwire(group, wire)
Wire.setgroup(wire, group)
Wire.update(wire)
if group.in_queue==0 then
Simulation.queuegroup(GSim, group)
end
end
end
end
function Group.removewire(group, wire)
Wire.setgroup(wire, nil)
@ -186,11 +183,9 @@ function Group.setstate(group, state)
group.state = state
group.update_tick = sim.current_tick
--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, group.gates_update[i])
end
Simulation.queuegroupfx(sim, group)

View File

@ -205,8 +205,6 @@ function network_update()
i = i + 1
end
elseif err == "closed" then
--sim = Simulation.new(Simulation)
--acceptclient()
print("Connection closed")
error()
end

View File

@ -17,17 +17,15 @@ PortDirections = {
Port = {}
function Port.new(type, direction, position, causeupdate, idx)
function Port.new(type, direction, position, causeupdate, idx, gate)
local o = {
--state = ffi.new("long long", 0),
state = 0,
group = nil,
type = type,
direction = direction,
position = position,
causeupdate = causeupdate,
gate = nil,
gate = gate,
idx = idx,
}
return o
@ -50,12 +48,9 @@ function Port.getgate(port)
return port.gate
end
function Port.setgate(port, gate)
port.gate = gate
end
function Port.setgroup(port, group)
port.group = group
--Port.getgate(port)[port.idx*2] = group
Port.getgate(port).port_nets[port.idx] = group
end
@ -68,5 +63,5 @@ function Port.gettype(port)
end
function Port.getstate(port)
return port.state
return Port.getgate(port).port_states[port.idx]
end

View File

@ -116,6 +116,7 @@ function Simulation.addgate(sim, gate)
sim.ngates = sim.ngates + 1
Gate.preinit(gate)
Simulation.queuegateinit(sim, gate)
Simulation.queuegate(sim, gate)
end
@ -239,7 +240,8 @@ end
-- Logic Critical
function Simulation.queuegate(sim, gate)
if gate.in_queue==0 then
table.insert(sim.gatequeue, gate)
--table.insert(sim.gatequeue, gate)
sim.gatequeue[sim.num_gatequeue+1] = gate
sim.num_gatequeue = sim.num_gatequeue + 1
gate.in_queue = 1
end
@ -265,12 +267,11 @@ end
-- Logic Critical
function Simulation.queuegroup(sim, group)
if group.in_queue==0 then
--table.insert(sim.groupqueue, group)
--if group.in_queue==0 then
sim.groupqueue[sim.num_groupqueue+1] = group
sim.num_groupqueue = sim.num_groupqueue + 1
group.in_queue = 1
end
--end
end
function Simulation.dequeuegroup(sim, group)
@ -306,7 +307,6 @@ end
-- Logic Critical
function Simulation.ticklogic(sim)
--for k, group in ipairs(sim.groupqueue) do
local len = sim.num_groupqueue
for i = 1, len do
local group = sim.groupqueue[i]
@ -323,7 +323,6 @@ function Simulation.ticklogic(sim)
sim.tickqueue[sim.current_tick] = nil
end
--for k, gate in ipairs(sim.gatequeue) do
local len = sim.num_gatequeue
for i = 1, len do
local gate = sim.gatequeue[i]