allowing performance over safety?

Chiusa
#5,257 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Una pull request collegata è già stata integrata.

  • #5264 di @MangelMaxime — integrata

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
18/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
fsharp, javascript, typescript

Direzione di ricerca

The issue asks whether Fable should offer a switch, such as an attribute or fsproj property, that drops array bounds checks in generated JavaScript. The reporter links a benchmark showing that the item(i, arr) helper defeats V8 optimisation. Starting points are the array indexing code path in the Fable compiler and the benchmark in goswinr/Euclid.Kontur. Done means a maintainer decision on the design, not a patch.

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

Descrizione

Using an invalid index on an Array, ResizeArray or String throws the same Out-Of-Bound Exception in Fable as in .NET.
This is safe and good. I actually argued for this in an older issue.
In JS, using an invalid out-of-bounds index would just return undefined.

However, Claude measured up to 8x slowdown on array indexing.
It seems item(i, arr) gets used on several types of arrays, and then V8 can't optimize it.

Do you think there could or should be a Fable attribute or fsproj property to disable bound checks for some modules or projects? So that the rest of the app still compiles with bound checks.
UndefinedInsteadOfIndexOutOfBoundsException ?

The workaround is easy; just use this on your hot path:

module internal Arr =

    /// arr.[i] without a bounds check under Fable.
    let inline get (arr: 'T[]) (i: int) : 'T =
        #if RELEASE && (FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT)
        Fable.Core.JsInterop.emitJsExpr (arr, i) "$0[$1]"
        #else
        arr.[i]
        #endif

    /// arr.[i] <- v without a bounds check under Fable.
    let inline set (arr: 'T[]) (i: int) (v: 'T) : unit =
        #if RELEASE && (FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT)
        Fable.Core.JsInterop.emitJsStatement (arr, i, v) "$0[$1] = $2"
        #else
        arr.[i] <- v
        #endif
Lingua principale
F#
Stelle
3.2k
Fork
335
Merge medio
18h 57m
PR unite (30g)
154

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

  • Nessun Dockerfile né 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 fable-compiler/Fable

Tutte le issue di fable-compiler/Fable

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.