State.close leaves closed upvalues in the open list, so calls get slower without bound

Aperta
#153 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

@efegokdemir ci sta già lavorando.

Dal 7/11/2026.

  • #155 di @efegokdemir — aperta

Valutazione

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

Direzione di ricerca

Read stack.go and trace State.close alongside findUpValue to understand how the open upvalue list is maintained. The issue includes a reproduction and a proposed fix; verify the list no longer retains closed entries and run the relevant Go tests. A pull request is already open for this issue.

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

Descrizione

State.close (stack.go) unlinks closed upvalues from l.upValues, but the loop advances p onto the node it has just unlinked:

for e := l.upValues; e != nil; e, p = e.next, e {
	if e.upValue.isInStackAbove(level) {
		e.upValue.close()
		if p != nil {
			p.next = e.next
		} else {
			l.upValues = e.next
		}
	}
}

When two neighbouring upvalues are closed in the same call, the second unlink writes to the removed node instead of the list. The second upvalue is closed correctly (its value is right), but it stays in the open list forever. Every later findUpValue (on each closure creation) and close walks the longer list, so code that creates closures gets slower the longer a State lives.

Reproduction:

L := lua.NewState()
lua.OpenLibraries(L)
lua.DoString(L, `
local function f()
  local a, b, c = 1, 2, 3
  local g = function() return a + b + c end
  return g()
end
for _ = 1, 100 do f() end`)
// l.upValues now holds 100 dead entries (one per call); it should hold none.

In our use (a planner called a few thousand times per State), the cost per call grew from about 155 µs to about 1.6 ms after 4,000 calls, and profiles were dominated by findUpValue / isInStackAt. With the fix below, the open list stays empty and a long simulation ran 2.2 times faster, with identical results.

Fix: only advance p past nodes that stay in the list.

 	var p *openUpValue
-	for e := l.upValues; e != nil; e, p = e.next, e {
+	for e := l.upValues; e != nil; e = e.next {
 		if e.upValue.isInStackAbove(level) {
 			e.upValue.close()
 			if p != nil {
 				p.next = e.next
 			} else {
 				l.upValues = e.next
 			}
+		} else {
+			p = e
 		}
 	}
Lingua principale
Go
Stelle
3.5k
Fork
217
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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 Shopify/go-lua

Tutte le issue di Shopify/go-lua

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.