From e69d61eaf47955a416321502145c25a4ec0729f6 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 12 Aug 2025 11:27:13 +0200 Subject: [PATCH] :sparkles: Add facilities for work with dataview with common alases --- common/src/app/common/buffer.cljc | 12 ++------ frontend/src/app/render_wasm/api.cljs | 30 ++++++++++--------- frontend/src/app/render_wasm/mem.cljs | 16 ++++++++++ frontend/src/app/render_wasm/serializers.cljs | 1 - 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/common/src/app/common/buffer.cljc b/common/src/app/common/buffer.cljc index 226e547b45..55ec4f83c8 100644 --- a/common/src/app/common/buffer.cljc +++ b/common/src/app/common/buffer.cljc @@ -129,15 +129,9 @@ (defn wrap [data] - #?(:clj (let [buffer (ByteBuffer/wrap ^bytes data)] - (.order buffer ByteOrder/LITTLE_ENDIAN)) - :cljs - (cond - (instance? js/Uint8Array data) - (new js/DataView (.-buffer ^js data)) - - :else - (throw (js/Error. "unexpected type"))))) + #?(:clj (let [buffer (ByteBuffer/wrap ^bytes data)] + (.order buffer ByteOrder/LITTLE_ENDIAN)) + :cljs (new js/DataView (.-buffer ^js data)))) (defn allocate [size] diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index 95e29e612f..4c4008bed7 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -8,7 +8,6 @@ "A WASM based render API" (:require ["react-dom/server" :as rds] - [app.common.buffer :as buf] [app.common.data :as d :refer [not-empty?]] [app.common.data.macros :as dm] [app.common.types.fills :as types.fills] @@ -455,8 +454,7 @@ [entries] (let [size (mem/get-alloc-size entries GRID-LAYOUT-ROW-U8-SIZE) offset (mem/alloc size) - heapu8 (mem/get-heap-u8) - buffer (buf/wrap heapu8)] + dview (mem/get-data-view)] (reduce (fn [offset {:keys [type value]}] ;; NOTE: because of the nature of the grid row data @@ -464,8 +462,8 @@ ;; alligned writes, so for heteregeneus writes we use ;; the buffer abstraction (DataView) for perform ;; surgical writes. - (buf/write-byte buffer (+ offset 0) (sr/translate-grid-track-type type)) - (buf/write-float buffer (+ offset 1) value) + (mem/write-u8 dview (+ offset 0) (sr/translate-grid-track-type type)) + (mem/write-f32 dview (+ offset 1) value) (+ offset GRID-LAYOUT-ROW-U8-SIZE)) offset entries) @@ -476,16 +474,20 @@ [entries] (let [size (mem/get-alloc-size entries GRID-LAYOUT-COLUMN-U8-SIZE) offset (mem/alloc size) - heap (-> (mem/get-heap-u8) - (mem/view offset size))] + dview (mem/get-data-view)] + + (reduce (fn [offset {:keys [type value]}] + ;; NOTE: because of the nature of the grid column data + ;; structure memory layout we can't use fully 32 bits + ;; alligned writes, so for heteregeneus writes we use + ;; the buffer abstraction (DataView) for perform + ;; surgical writes. + (mem/write-u8 dview (+ offset 0) (sr/translate-grid-track-type type)) + (mem/write-f32 dview (+ offset 1) value) + (+ offset GRID-LAYOUT-COLUMN-U8-SIZE)) + offset + entries) - (loop [entries (seq entries) - current-offset 0] - (when-not (empty? entries) - (let [{:keys [type value]} (first entries)] - (.set heap (sr/u8 (sr/translate-grid-track-type type)) (+ current-offset 0)) - (.set heap (sr/f32->u8 value) (+ current-offset 1)) - (recur (rest entries) (+ current-offset GRID-LAYOUT-COLUMN-U8-SIZE))))) (h/call wasm/internal-module "_set_grid_columns"))) (defn set-grid-layout-cells diff --git a/frontend/src/app/render_wasm/mem.cljs b/frontend/src/app/render_wasm/mem.cljs index e56ec4a01f..59562329c8 100644 --- a/frontend/src/app/render_wasm/mem.cljs +++ b/frontend/src/app/render_wasm/mem.cljs @@ -6,6 +6,7 @@ (ns app.render-wasm.mem (:require + [app.common.buffer :as buf] [app.render-wasm.helpers :as h] [app.render-wasm.wasm :as wasm])) @@ -71,3 +72,18 @@ same element types as for this typed array." [heap offset size] (.subarray ^js heap offset (+ offset size))) + +(defn get-data-view + "Returns a heap wrapped in a DataView for surgical write operations" + [] + (buf/wrap (get-heap-u8))) + +(defn write-u8 + "Write unsigned int8. Expects a DataView instance" + [target offset value] + (buf/write-byte target offset value)) + +(defn write-f32 + "Write float32. Expects a DataView instance" + [target offset value] + (buf/write-float target offset value)) diff --git a/frontend/src/app/render_wasm/serializers.cljs b/frontend/src/app/render_wasm/serializers.cljs index bf9dfb45cb..5efd965b6d 100644 --- a/frontend/src/app/render_wasm/serializers.cljs +++ b/frontend/src/app/render_wasm/serializers.cljs @@ -6,7 +6,6 @@ (ns app.render-wasm.serializers (:require - [app.common.data.macros :as dm] [app.common.uuid :as uuid] [cuerdas.core :as str]))