Giter Club home page Giter Club logo

Comments (9)

joinr avatar joinr commented on May 24, 2024

[comment updated to fix formatting]

from threeagent.

DougHamil avatar DougHamil commented on May 24, 2024

I hadn't tried embedding threeagent in a reagent component like this before, but it seems to work.

I put together a simple example to try to reproduce the memory leak issue:

(ns app
  (:require [threeagent.core :as th]
            [reagent.dom :as rdom]
            [reagent.core :as r]))

(defonce state (r/atom 0))

(defn- tick [delta-time]
  (swap! state + delta-time))

(defn- threeagent-root []
  [:object
   [:ambient-light {:intensity 1.0}]
   [:box {:position [0 0 -5]
          :rotation [0 @state 0]}]])

(defn- threeagent-scene [root-fn]
  (r/create-class
   {:display-name "threeagent"
    :reagent-render (fn [] [:canvas])
    :component-did-mount
    (fn [this]
      (th/render root-fn (rdom/dom-node this) {:on-before-render tick}))
    :component-did-update
    (fn [this]
      (th/render root-fn (rdom/dom-node this) {:on-before-render tick}))}))

(defn root []
  [:div
   [:h1 "Embedded Threeagent Scene"]
   [:p "Tick:" @state]
   [threeagent-scene threeagent-root]])

(defn ^:dev/after-load init []
  (rdom/render [root] (.getElementById js/document "root")))

I didn't see any obvious memory leaks. There is definitely a lot of garbage generated per frame (I'm seeing about 30KB/frame) but it's all being reclaimed during the minor GC (this is on Chrome). I haven't spent much time reducing the garbage generated by threeagent, but there's certainly room for improvement.

So, I don't think using threeagent in this way is causing the memory leak. If you can put together a smaller example that reproduces the issue, that would help a lot.

from threeagent.

joinr avatar joinr commented on May 24, 2024

It could be something I introduced in the utils I wrote. I will strip down my example and see if I can get an isolated case. Thanks.

from threeagent.

joinr avatar joinr commented on May 24, 2024

I traced a memory profile and it went to component-did-update invoking render every frame for some reason. If I elide that from the implementation, memory leak disappears and performance is great. Note: I am using sprites in my stuff too (another degree of freedom) which your minimal example does not. Very curious.

from threeagent.

DougHamil avatar DougHamil commented on May 24, 2024

Oh interesting, when I put together my example it was not running component-did-update on each frame. It might be due to the (fn [dt] (swap! ratom tick-scene)) parameter, as it's being recreated each frame due to the @c-day deref. Reagent is probably sees this new fn as a different parameter, so it forces the three-canvas component to re-render.

You could try passing the ratom into the three-canvas component and have it define and use that function.

from threeagent.

DougHamil avatar DougHamil commented on May 24, 2024

There is likely a memory leak somewhere in threeagent from the multiple threeagent/render calls, as that usually should not be called multiple times (except after a hot-reload).

from threeagent.

joinr avatar joinr commented on May 24, 2024

You could try passing the ratom into the three-canvas component and have it define and use that function.

I ended up just wrapping the app state in a form-2 component where the render function is computed outside the reagent form:

(defn app [ratom]
  (let [render-scene! (fn [dt] (swap! ratom tick-scene))]
      (fn [] 
         ...the-page...)))

I can't tell if this has made a drastic difference though, performance was pretty good after the last fix.

from threeagent.

tytrdev avatar tytrdev commented on May 24, 2024

I'd recommend updating this issue to reflect that both reagent and threeagent are being used

from threeagent.

DougHamil avatar DougHamil commented on May 24, 2024

Sorry for not getting back to this sooner. I improved the docs in the latest release and added an FAQ entry for how to embed threeagent within a Reagent app.

Please re-open or create a new issue if you are running into issues with this approach. Thank you!

from threeagent.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.