Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

State Handling Performance Speedup #206

Open
catmando opened this issue Jan 26, 2017 · 4 comments
Open

State Handling Performance Speedup #206

catmando opened this issue Jan 26, 2017 · 4 comments

Comments

@catmando
Copy link
Contributor

Group all state updates in a given render cycle together

module React
  class State
    def self.set_state(object, name, value, delay=true)
      states[object][name] = value
      if delay || @bulk_update_flag
        @delayed_updates ||= Hash.new { |h, k| h[k] = {} }
        @delayed_updates[object][name] = [value, Set.new]
        @delayed_updater ||= after(0.0) do
          delayed_updates = @delayed_updates
          @delayed_updates = Hash.new { |h, k| h[k] = {} } # could this be nil???
          @delayed_updater = nil
          updates = Hash.new { |hash, key| hash[key] = Array.new }
          delayed_updates.each do |object, name_hash|
            name_hash.each do |name, value_and_set|
              set_state2(object, name, value_and_set[0], updates, value_and_set[1])
            end
          end
          updates.each { |observer, args| observer.update_react_js_state(*args) }
        end
      elsif @rendering_level == 0
        updates = Hash.new { |hash, key| hash[key] = Array.new }
        set_state2(object, name, value, updates)
        updates.each { |observer, args| observer.update_react_js_state(*args) }
      end
      value
    end
  end
end
@zetachang
Copy link
Member

@catmando
Copy link
Contributor Author

Might be! I couldn't find the issue. Note that the default on the second parameter is now true. That makes a huge difference

@zetachang
Copy link
Member

Ok, I see. The change is actually introduced by #178

@janbiedermann
Copy link
Contributor

this doesnt work as expected with react, react seems unable to group setState calls outside of a render cycle. Preact can do this, maybe react 16 will improve this during its dot releases.
However, this can be achieved in React with: ReactDOM.unstable_batchedUpdates(), which, as the name suggests, is kinda unstable. Needs watching and adoption. If this a performance issue for somebody, i recommend to try with Preact.
As of lap20 state handling improved anyway, setting state synchronously where allowed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants