Skip to content

Commit daddda2

Browse files
committed
Performance: Fields are organized for cache efficiency
1 parent eaa27e0 commit daddda2

4 files changed

Lines changed: 33 additions & 24 deletions

File tree

src/ClassLogic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ Logic *ClassNetlist::parse(Logic *node, int depth)
276276
// Copy the list of transistors for which this net is either a source or a drain, but skip over transistors already visited
277277
// While copying, make sure c1 contains our primary net number, and c2 is "the other end" net
278278
node->trans.reserve(net0.c1c2s.size());
279-
for (const Trans *t : net0.c1c2s)
279+
for (Trans *t : net0.c1c2s)
280280
{
281281
if (!visitedTrans.contains(t->id)) // Skip over transistors already visited
282282
{

src/ClassNetlist.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,9 @@ const QStringList ClassNetlist::get(const QVector<net_t> &nets)
407407
const QVector<net_t> ClassNetlist::netsDriving(net_t n)
408408
{
409409
QVector<net_t> nets;
410-
const QVector<Trans *> &gates = m_netlist[n].gates;
410+
const QVector<Trans*> &gates = m_netlist[n].gates;
411411

412-
for (const auto t : gates)
412+
for (Trans *t : gates)
413413
{
414414
if ((t->c1 > 2) && !nets.contains(t->c1)) // c1 is the source
415415
nets.append(t->c1);
@@ -426,9 +426,11 @@ const QVector<net_t> ClassNetlist::netsDriving(net_t n)
426426
const QVector<net_t> ClassNetlist::netsDriven(net_t n)
427427
{
428428
QVector<net_t> nets;
429-
for (auto &t : m_netlist[n].c1c2s)
429+
for (Trans *t : m_netlist[n].c1c2s)
430+
{
430431
if (!nets.contains(t->gate))
431432
nets.append(t->gate);
433+
}
432434
std::sort(nets.begin(), nets.end()); // Sorting numbers only
433435
return nets;
434436
}
@@ -439,8 +441,8 @@ const QVector<net_t> ClassNetlist::netsDriven(net_t n)
439441
inline pin_t ClassNetlist::getNetStateEx(net_t n)
440442
{
441443
// Every transistor in the contributing nets needs to be off for this net to be hi-Z
442-
for (auto &tran : m_netlist[n].c1c2s)
443-
if (tran->on) return !!m_netlist[n].state;
444+
for (Trans *t : m_netlist[n].c1c2s)
445+
if (t->on) return !!m_netlist[n].state;
444446
// If nothing is explicitly driving this net, it will be "1" if it has an internal pullup
445447
if (m_netlist[n].hasPullup)
446448
return 1;
@@ -520,12 +522,12 @@ const QString ClassNetlist::netInfo(net_t net)
520522
{
521523
// Transistor numbers for which this net is either a source or a drain
522524
QStringList c1c2s;
523-
for (auto &t : m_netlist[net].c1c2s)
524-
c1c2s.append(QString::number(t - &m_transdefs[0]));
525+
for (Trans *t : m_netlist[net].c1c2s)
526+
c1c2s.append(QString::number(t->id));
525527
// Transistor numbers for which this net is a gate
526528
QStringList gates;
527-
for (auto &t : m_netlist[net].gates)
528-
gates.append(QString::number(t - &m_transdefs[0]));
529+
for (Trans *t : m_netlist[net].gates)
530+
gates.append(QString::number(t->id));
529531
// Limit printing up to 20 gate nets which is more than a practical limit.
530532
// This prevents large nets like clk to take over the log window
531533
if (gates.count() > 20)

src/ClassNetlist.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
66
#include <QHash>
77

88
// Contains individual transistor definition
9+
// Fields are organized for cache efficiency: hot data (frequently accessed) first
910
struct Trans
1011
{
11-
tran_t id; // Transistor number
12-
net_t gate; // Net connected to its gate
13-
net_t c1, c2; // Connections 1, 2 (source, drain) nets
14-
bool on {false}; // Is the transistor on?
12+
// Hot data - accessed in critical path (recalcNet, addNetToGroup)
13+
bool on {false}; // Is the transistor on? (1 byte)
14+
// Padding for alignment (1 byte) - compiler will add this automatically
15+
net_t c1, c2; // Connections 1, 2 (source, drain) nets (4 bytes total)
16+
17+
// Cold data - rarely accessed in hot path
18+
net_t gate; // Net connected to its gate (2 bytes)
19+
tran_t id; // Transistor number (2 bytes)
1520
};
1621

1722
// Contains netlist net definition: net is a trace with equal potential and it connects a number of transistors
1823
struct Net
1924
{
20-
QVector<Trans *> gates; // The list of transistors for which this net is a gate
21-
QVector<Trans *> c1c2s; // The list of transistors for which this net is either a source or a drain
25+
QVector<Trans*> gates; // Transistors for which this net is a gate
26+
QVector<Trans*> c1c2s; // Transistors for which this net is either a source or a drain
2227
bool state {false}; // The voltage on the net is high (if not floating)
2328
bool floats {false}; // Net can float (used with ab, db, mreq, iorq, rd, wr to read hi-Z state)
2429
bool isHigh {false}; // Net is being pulled high

src/ClassSimZ80.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ inline bool ClassSimZ80::getNetValue()
357357
#if 0
358358
// We just want to pick the larger, or "stronger" net, assuming that one would have more transistor connections
359359
//auto conn = net.gates.count() + net.c1c2s.count();
360-
auto conn = net.c1c2s.count();
360+
//auto conn = net.c1c2s.count();
361361
auto conn = net.gates.count();
362362
if (conn > max_conn)
363363
{
@@ -416,7 +416,7 @@ inline void ClassSimZ80::recalcNet(net_t n)
416416
net.state = newState;
417417

418418
if (net.state)
419-
for (Trans *&t : net.gates)
419+
for (Trans *t : net.gates)
420420
{
421421
if (!t->on)
422422
{
@@ -425,7 +425,7 @@ inline void ClassSimZ80::recalcNet(net_t n)
425425
}
426426
}
427427
else
428-
for (Trans *&t : net.gates)
428+
for (Trans *t : net.gates)
429429
{
430430
if (t->on)
431431
{
@@ -447,12 +447,12 @@ inline void ClassSimZ80::recalcNet(net_t n)
447447
Net &net = m_netlist[i];
448448
if (net.state == newState) continue;
449449
net.state = newState;
450-
for (int i = 0; i < net.gates.count(); i++)
450+
for (Trans *t : net.gates)
451451
{
452452
if (net.state)
453-
setTransOn(net.gates[i]);
453+
setTransOn(t);
454454
else
455-
setTransOff(net.gates[i]);
455+
setTransOff(t);
456456
}
457457
}
458458
}
@@ -544,7 +544,8 @@ inline void ClassSimZ80::addNetToGroup(net_t n)
544544
setBit(m_groupBitset, n);
545545
m_group[m_groupIndex++] = n;
546546
if (Q_UNLIKELY((n == ngnd) || (n == npwr))) return;
547-
for (auto &t : m_netlist[n].c1c2s)
547+
548+
for (Trans *t : m_netlist[n].c1c2s)
548549
{
549550
if (!t->on) continue;
550551
net_t other = 0;
@@ -560,7 +561,8 @@ inline void ClassSimZ80::addNetToGroup(net_t n)
560561
if (group.contains(n)) return;
561562
group.append(n);
562563
if (Q_UNLIKELY((n == ngnd) || (n == npwr))) return;
563-
for (auto &t : m_netlist[n].c1c2s)
564+
565+
for (Trans *t : m_netlist[n].c1c2s)
564566
{
565567
if (!t->on) continue;
566568
net_t other = 0;

0 commit comments

Comments
 (0)