microts: generated load_fonts and load_images copy include_bytes! data into the heap

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

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
28/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust, typescript

Direzione di ricerca

Start at the AOT emitter in tools/pocket.ts that writes include.rs with load_fonts/load_images. Then read the in-place core APIs load_font_atlas_shared (engine/core/src/lib.rs:1684) and upload_texture_from (lib.rs:1230) already used by the guest package path (#642, #643). Done means the generated loaders read the 'static bytes in place (or an opt-in second pair), shown by the counting-allocator harness dropping ~186KB to ~3.4KB; update esp-idf.md 'Memory and stack' if a compile option is added.

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

Descrizione

enhancement

Version

PocketJS v0.17.0 (83c6cac9, current main). macOS 27.2 arm64, Bun 1.4.2, Rust 1.98.1.

Gap

pocket compile --aot writes include.rs with load_fonts, which calls load_font_atlas for each atlas, and load_images, which calls upload_texture for each image. Both copy the include_bytes! data into the heap: Atlas::parse copies the coverage cells (engine/core/src/text.rs:177) and upload_texture_flags copies the pixels (engine/core/src/lib.rs:1145–1225). The data stays in flash as well.

The core has in-place loaders for both. load_font_atlas_shared (engine/core/src/lib.rs:1684) reads coverage cells from the caller's storage, and upload_texture_from (lib.rs:1230) decodes a texture when it is first drawn and releases it under set_texture_budget. The guest package path uses them (#642, #643). esp-idf.md ("Memory and stack") documents the copy and says a library that needs the atlases in place writes its own loader.

On an ESP32-S3 without PSRAM the heap is the internal RAM, a few hundred KiB, so the copies decide whether a small AOT application fits.

Repro

An application with three text slots and one 128×128 image (icon.png, any 128×128 RGBA PNG), measured on the host with a counting allocator.

pocket.host.json, pocket.host.config.ts, main.tsx, app.ts
{
  "format": "pocket-host/2",
  "id": "repro-esp32s3",
  "platform": "esp-idf",
  "form": "takeover",
  "cadences": [30],
  "display": {
    "logicalViewports": [[240, 240]],
    "presentations": ["native"],
    "rasterDensity": 1,
    "animation": "repaint"
  },
  "panel": { "primary": [240, 240] },
  "capabilities": ["input.buttons", "text.glyphs.baked"]
}
// pocket.host.config.ts
import { defineHostConfig } from "@pocketjs/framework/host-config";

export default defineHostConfig({
  host: "./pocket.host.json",
  app: {
    id: "dev.example.repro",
    title: "Repro",
    version: "0.1.0",
    entry: "main.tsx",
    output: "repro",
    framework: "solid",
    viewport: { logical: [240, 240], presentation: "native" },
    features: ["input.buttons", "text.glyphs.baked"],
    aot: true,
    model: "compiled",
  },
});
// main.tsx
import { mount } from "@pocketjs/framework/solid";
import App from "./app.tsx";
mount(() => <App />);
// app.ts
import { createSignal } from "solid-js";
import type { i32 } from "@pocketjs/framework/solid/std";
export const [count, setCount] = createSignal<i32>(0);
// app.tsx
import { Image, Text, View } from "@pocketjs/framework/solid/components";
import { count } from "./app";

export default function App() {
  return (
    <View class="w-full h-full flex-col p-2 bg-slate-100">
      <Image class="w-[128] h-[128]" src="icon.png" />
      <Text class="text-xs text-slate-900">Status {count()}</Text>
      <Text class="text-base text-slate-900">Settings</Text>
      <Text class="text-2xl font-bold text-slate-900">12:34</Text>
    </View>
  );
}
# harness/Cargo.toml
[package]
name = "aot-heap"
version = "0.0.0"
edition = "2021"

[dependencies]
microts = { path = "<pocketjs>/engine/microts-runtime" }

[workspace]
// harness/src/lib.rs
#![no_std]
extern crate alloc;
use alloc::rc::Rc;
use microts::pocketjs_core::spec::psm::PSM_8888;
include!("../../gen/include.rs");

/// The generated loaders.
pub fn mount_generated(ui: &mut microts::Ui) {
    load_fonts(ui);
    load_images(ui);
}

static FONTS: [&[u8]; 3] = [
    include_bytes!("../../gen/font-0.bin"),
    include_bytes!("../../gen/font-2.bin"),
    include_bytes!("../../gen/font-12.bin"),
];
static IMAGE: &[u8] = include_bytes!("../../gen/images/image-0.rgba");

/// The same data through the in-place core APIs.
pub fn mount_in_place(ui: &mut microts::Ui) {
    for bytes in FONTS {
        let storage: Rc<dyn AsRef<[u8]>> = Rc::new(bytes);
        assert!(ui.core_mut().load_font_atlas_shared(storage, 0..bytes.len()));
    }
    let storage: Rc<dyn AsRef<[u8]>> = Rc::new(IMAGE);
    let id = ui.core_mut().upload_texture_from(storage, 0..IMAGE.len(), 128, 128, PSM_8888, 0);
    assert!(id >= 0);
    ui.register_image("icon.png", id);
}
// harness/tests/heap.rs
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicIsize, Ordering};

struct Counting;
static LIVE: AtomicIsize = AtomicIsize::new(0);
unsafe impl GlobalAlloc for Counting {
    unsafe fn alloc(&self, l: Layout) -> *mut u8 { LIVE.fetch_add(l.size() as isize, Ordering::SeqCst); unsafe { System.alloc(l) } }
    unsafe fn dealloc(&self, p: *mut u8, l: Layout) { LIVE.fetch_sub(l.size() as isize, Ordering::SeqCst); unsafe { System.dealloc(p, l) } }
    unsafe fn realloc(&self, p: *mut u8, l: Layout, n: usize) -> *mut u8 { LIVE.fetch_add(n as isize - l.size() as isize, Ordering::SeqCst); unsafe { System.realloc(p, l, n) } }
}
#[global_allocator]
static A: Counting = Counting;

fn measure(f: fn(&mut microts::Ui)) -> isize {
    let mut ui = microts::Ui::new();
    let before = LIVE.load(Ordering::SeqCst);
    f(&mut ui);
    let after = LIVE.load(Ordering::SeqCst);
    drop(ui);
    after - before
}

#[test]
fn heap_bytes_of_the_loaders() {
    let generated = measure(aot_heap::mount_generated);
    let in_place = measure(aot_heap::mount_in_place);
    println!("generated load_fonts + load_images: {generated} bytes live");
    println!("load_font_atlas_shared + upload_texture_from: {in_place} bytes live");
}
bun $POCKETJS/tools/pocket.ts compile --config pocket.host.config.ts --aot --out gen
(cd harness && cargo test --test heap -- --nocapture)

Actual

The generated data is 119,952 bytes of atlases (font-0.bin 18,064, font-2.bin 31,504, font-12.bin 70,384) and 65,536 bytes of RGBA.

generated load_fonts + load_images: 186408 bytes live
load_font_atlas_shared + upload_texture_from: 3400 bytes live

The in-place figure is before the first frame. The texture's 65,536 bytes are allocated when it is first drawn, and the texture budget can release them.

Expected

The generated loaders read the 'static data in place: load_fonts over load_font_atlas_shared and load_images over upload_texture_from. Reading from flash costs more per access than reading internal RAM, so a compile option or a second pair of generated functions would leave the choice to the product.

Workaround

Write the loaders by hand as in mount_in_place above. The hand-written loader has to list the generated file names and image sizes, and it changes whenever the application's fonts or images change.

Lingua principale
TypeScript
Stelle
1.6k
Fork
88
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Non abbiamo ancora controllato i file di configurazione di questo progetto. 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 pocket-stack/pocketjs

Tutte le issue di pocket-stack/pocketjs

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.