Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

feat: Granular tracking of stale references

Aperta
#2,753 1 commento 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Tranquilla
Stack tecnologico
typescript
Ambito
compilers

Direzione di ricerca

Nell’issue non sono identificati file né test. Inizia tracciando la generazione dei riferimenti TypeScript-to-WGSL e il lavoro esistente sulla verifica del consumo menzionato nella proposta. Il lavoro è completato quando i riferimenti diventano inaccessibili dopo che il valore originale potrebbe essere stato mutato, mentre l’uso di argomenti immutabili rimane valido.

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

Descrizione

There has been TypeScript behavior that we have overlooked up until now that has to do with references:

const Boid = d.struct({
  pos: d.vec3f,
  vel: d.vec3f,
});

function main() {
  'use gpu';
  const boid = Boid();
  // getting a reference to the boid's position
  const boidPos = boid.pos;
  // replacing the `pos` property of the boid with a new vector
  boid.pos = d.vec3f(1, 2, 3);
  // returning the `x` coordinate of the vector that was once
  // the boid's position
  return boidPos.x;
}

We'd expect the result of main() to be 0, since we haven't actually updated the x coordinate of the existing boid.pos, rather we replaced that vector with an entirely new one, making boidPos no longer point to the actual value of boid.pos (they drifted out of sync).
We generate the following WGSL:

struct Boid {
  pos: vec3f,
  vel: vec3f,
}

fn main() -> f32 {
  var boid = Boid();
  let boidPos = (&boid.pos);
  boid.pos = vec3f(1, 2, 3);
  return (*boidPos).x;
}

In the generated WGSL, boidPos is a pointer to boid.pos, meaning it always reflects the up-to-date value of boid.pos, making the result of calling main() be 1.

Stale reference tracking

@cieplypolar's insight is that we can use a sort of borrow checker (like we were planning to do for consumption verification) to invalidate stale references. Essentially, as soon as it's determined that boid (the original value) could have been mutated by an instruction, boidPos would become invalidated and unaccessible.

struct Boid {
  pos: vec3f,
  vel: vec3f,
}

fn main() -> f32 {
  var boid = Boid();
  let boidPos = (&boid.pos);
  boid.pos = vec3f(1, 2, 3); // <- accessing the original invalidates the aliases
  return (*boidPos).x; // 🚫 `boidPos` is a stale reference
}

I believe there is a way to do this statically without introducing too much complexity to the system.

[!NOTE]
To be continued...

function main() {
  'use gpu';
  const boid = Boid();
  // boid
  //

  const boidPos = boid.pos;
  // boid
  // |
  // | (.pos)
  // |
  // boidPos

  randomFunction(boid) // <- arguments are immutable, so this is okay

  blackBox(d.ref(boid)); // <- could mutate `boid`
  // boid
  // |
  // | (.pos)
  // |
  // ☠️ boidPos

  return boidPos.x; // 🚫 `boidPos` is a stale reference
}
function main() {
  'use gpu';
  const boid = Boid();
  // boid
  //

  const boidPos = boid.pos;
  // boid
  // |
  // | (.pos)
  // |
  // boidPos

  const boidVel = boid.vel;
  // boid
  // |         \
  // | (.pos)   \ (.vel)
  // |           \
  // boidPos      boidVel

  blackBox(d.ref(boid)); // <- could mutate `boid`
  // boid
  // |         \
  // | (.pos)   \ (.vel)
  // |           \
  // ☠️ boidPos   ☠️ boidVel

  return boidPos.x; // 🚫 `boidPos` is a stale reference
}
Lingua principale
TypeScript
Stelle
3.3k
Fork
140
Merge medio
3g 8h
PR unite (30g)
72

Preparare l'ambiente

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 software-mansion/TypeGPU

Tutte le issue di software-mansion/TypeGPU

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.