`check` and startup are quadratic in outbound group size since 1.14.4 (duplicate tag check)

Aperta Adatta ai principianti
#4,745 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

@Tidehyc ci sta già lavorando.

Dal 28/11/2026.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
84/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go

Direzione di ricerca

Leggi i cicli di controllo dei tag duplicati in protocol/group/selector.go e protocol/group/urltest.go, indicati nel report. Sostituisci le scansioni ripetute con un controllo basato su un insieme, quindi confronta i tempi di sing-box check usando la riproduzione fornita con 50.000 outbound e 20 selettori. Il lavoro è completato quando i tag duplicati vengono ancora rifiutati e la convalida scala linearmente con la dimensione del gruppo.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Operating system

Linux

System version

Alpine Linux, official image ghcr.io/sagernet/sing-box:v1.14.5, linux/arm64

Installation type

Original sing-box Command Line

If you are using a graphical client, please provide the version of the client.

No response

Version
sing-box version 1.14.5

Environment: go1.27.4 linux/arm64
Tags: with_gvisor,with_quic,with_dhcp,with_wireguard,with_utls,with_acme,with_clash_api,with_tailscale,with_ccm,with_ocm,with_cloudflared,with_naive_outbound,with_usbip,with_openvpn,with_openconnect,badlinkname,tfogo_checklinkname0,with_musl
Revision: a6844a780ba173e189a018d0ac62df67cd50b8e9
CGO: enabled
Description

Since 1.14.4, creating a selector or urltest takes time quadratic in its number of members. A config with many outbounds and several groups over them now takes tens of seconds to validate and to start, where 1.14.3 takes about 2 seconds.

With 50,000 direct outbounds and 20 selectors that each list all of them:

Version sing-box check sing-box run until "sing-box started"
1.14.3 1.8 s 3 s
1.14.4 33.2 s not measured
1.14.5 44.2 s 34 s
1.15.0-alpha.14 38.0 s not measured

A SIGHUP reload pays it twice, since it runs the check and then starts the new instance.

The cause is the duplicate check added in c90ccd7535 ("Reject duplicate tags in outbound groups"). For every tag it scans all the tags before it, in protocol/group/selector.go (lines 63-67 at v1.14.5) and with the same loop in protocol/group/urltest.go:

for i, tag := range outbound.tags {
	if slices.Contains(outbound.tags[:i], tag) {
		return nil, E.New("duplicate tag: ", tag)
	}
}

That is n²/2 string comparisons per group: 1.25 billion for 50,000 members. The time is linear in the number of groups (one selector of 50,000 members: 2.5 s; twenty: 44 s) and quadratic in the members (20 selectors on 1.14.5, wall time of docker run: 10,000 members 4.1 s, 20,000 members 5.4 s, 40,000 members 21.9 s).

A set keeps the check and makes it linear:

seen := make(map[string]struct{}, len(outbound.tags))
for _, tag := range outbound.tags {
	if _, loaded := seen[tag]; loaded {
		return nil, E.New("duplicate tag: ", tag)
	}
	seen[tag] = struct{}{}
}
Reproduction

Generate the config (needs jq; the file is about 22 MB):

jq -n --argjson n 50000 --argjson m 20 '{
  log: {level: "error"},
  outbounds: (
    [range($n) | {type: "direct", tag: "p\(.)"}]
    + [range($m) | {type: "selector", tag: "s\(.)", outbounds: [range($n) | "p\(.)"]}]
  )
}' > config.json

That is the whole configuration: 50,000 outbounds {"type": "direct", "tag": "p0"} … "p49999", then 20 outbounds {"type": "selector", "tag": "s0", "outbounds": ["p0", …, "p49999"]} … "s19".

Then:

time sing-box check -c config.json

It exits 0 on every version; only the time differs.

Logs
check prints nothing. Timings, each in the official image of that version on the same machine:

v1.14.3           real 0m 1.76s
v1.14.4           real 0m 33.18s
v1.14.5           real 0m 44.24s
v1.15.0-alpha.14  real 0m 38.02s

v1.14.5, the same config with 1 selector instead of 20:
                  real 0m 2.50s
Supporter
Integrity requirements
  • I confirm that I have read the documentation, understand the meaning of all the configuration items I wrote, and did not pile up seemingly useful options or default values.
  • I confirm that I have provided the server and client configuration files and process that can be reproduced locally, instead of a complicated client configuration file that has been stripped of sensitive data.
  • I confirm that I have provided the simplest configuration that can be used to reproduce the error I reported, instead of depending on remote servers, TUN, graphical interface clients, or other closed-source software.
  • I confirm that I have provided the complete configuration files and logs, rather than just providing parts I think are useful out of confidence in my own intelligence.
Lingua principale
Go
Stelle
40.4k
Fork
5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di SagerNet/sing-box

Tutte le issue di SagerNet/sing-box

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.