mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2026-12-02 06:46:47 +00:00
chore: reuse queues during DomainSet build
Build the succinct domain index one level at a time and alternate two reusable queues instead of retaining every processed node until construction completes. Keep explicit node IDs to preserve terminal ordering, Foreach output, and binary serialization. Query logic and the serialized layout remain unchanged. This reduces peak build memory and improves construction time for DomainMap. In isolated measurements, the mixed 100k-rule workload used about 53% less peak memory and completed about 11% faster, while deep 100k-rule input used about 72% less peak memory and completed about 24% faster.
This commit is contained in:
1 parent
f6a038ce0f
commit
ebf82e2962
1 file changed
+25
-21
@@ -25,7 +25,7 @@ type DomainSet struct {
|
||||
ranks, selects []int32
|
||||
}
|
||||
|
||||
type qElt struct{ s, e, col int }
|
||||
type qElt struct{ s, e int }
|
||||
|
||||
// DomainSetBuilder incrementally collects domain patterns for a DomainSet.
|
||||
// Its zero value is ready to use.
|
||||
@@ -107,31 +107,35 @@ func buildDomainSet(keys []string, onTerminal func(string)) *DomainSet {
|
||||
ss := &DomainSet{}
|
||||
lIdx := 0
|
||||
|
||||
queue := []qElt{{0, len(keys), 0}}
|
||||
for i := 0; i < len(queue); i++ {
|
||||
elt := queue[i]
|
||||
if elt.col == len(keys[elt.s]) {
|
||||
if onTerminal != nil {
|
||||
onTerminal(keys[elt.s])
|
||||
nodeID := 0
|
||||
queue := make([]qElt, 1, len(keys))
|
||||
queue[0] = qElt{0, len(keys)}
|
||||
next := make([]qElt, 0, len(keys))
|
||||
for col := 0; len(queue) > 0; col++ {
|
||||
for _, elt := range queue {
|
||||
if col == len(keys[elt.s]) {
|
||||
if onTerminal != nil {
|
||||
onTerminal(keys[elt.s])
|
||||
}
|
||||
elt.s++
|
||||
// a leaf node
|
||||
setBit(&ss.leaves, nodeID, 1)
|
||||
}
|
||||
elt.s++
|
||||
// a leaf node
|
||||
setBit(&ss.leaves, i, 1)
|
||||
}
|
||||
|
||||
for j := elt.s; j < elt.e; {
|
||||
|
||||
frm := j
|
||||
|
||||
for ; j < elt.e && keys[j][elt.col] == keys[frm][elt.col]; j++ {
|
||||
for j := elt.s; j < elt.e; {
|
||||
frm := j
|
||||
for ; j < elt.e && keys[j][col] == keys[frm][col]; j++ {
|
||||
}
|
||||
next = append(next, qElt{frm, j})
|
||||
ss.labels = append(ss.labels, keys[frm][col])
|
||||
setBit(&ss.labelBitmap, lIdx, 0)
|
||||
lIdx++
|
||||
}
|
||||
queue = append(queue, qElt{frm, j, elt.col + 1})
|
||||
ss.labels = append(ss.labels, keys[frm][elt.col])
|
||||
setBit(&ss.labelBitmap, lIdx, 0)
|
||||
setBit(&ss.labelBitmap, lIdx, 1)
|
||||
lIdx++
|
||||
nodeID++
|
||||
}
|
||||
setBit(&ss.labelBitmap, lIdx, 1)
|
||||
lIdx++
|
||||
queue, next = next, queue[:0]
|
||||
}
|
||||
|
||||
ss.init()
|
||||
|
||||
Reference in new issue
Block a user