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

Đang mở
#719 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
28/100
Loại issue
Tính năng
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust, typescript
Lĩnh vực
build-system, performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
1.6k
Fork
88
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Chúng tôi chưa kiểm tra các tệp thiết lập môi trường của dự án này. Hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pocket-stack/pocketjs

Tất cả issue của pocket-stack/pocketjs

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.