GithubHelp home page GithubHelp logo

Comments (13)

andreasklinger avatar andreasklinger commented on July 23, 2024

We add the hash of the webpack main.js and the hash of the serializers to the cache checksum

    def fetch_component(name, args, options, &block)
      Rails.cache.fetch [webpack_checksum, name, Digest::SHA1.hexdigest(args.to_s), options], &block
    end

    def serializer_checksum
      return @serializer_checksum if @serializer_checksum.present? && !Rails.env.development?

      digest = Digest::SHA1.new
      serializer_files.each { |f| digest.file(f) }
      @serializer_checksum = digest.hexdigest
    end

    private

    def serializer_files
      Dir.glob(Rails.root.join('app/serializers/**/*.rb'))
    end

    def webpack_checksum
      return @webpack_checksum if @webpack_checksum.present? && !Rails.env.development?

      @webpack_checksum = Digest::SHA1.file(Webpack.local_asset_path('main.js')).hexdigest
    end

better ideas welcome

from react_on_rails.

justin808 avatar justin808 commented on July 23, 2024

@andreasklinger Looking at the code, I'm guessing that you include the serializers because your "args" are not the JSON props (Hash or String) being passed to the component. Any chance that you could elaborate more on your inclusion on the serializers and if there were any performance hot spots in computing the digests.

from react_on_rails.

justin808 avatar justin808 commented on July 23, 2024

Hash Speed

Interesting discussion here:

  1. http://stackoverflow.com/questions/3665247/fastest-hash-for-non-cryptographic-uses
  2. http://programmers.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed
 Name            Speed       Q.Score   Author
 xxHash          5.4 GB/s     10
 MumurHash 3a    2.7 GB/s     10       Austin Appleby
 SpookyHash      2.0 GB/s     10       Bob Jenkins
 SBox            1.4 GB/s      9       Bret Mulvey
 Lookup3         1.2 GB/s      9       Bob Jenkins
 CityHash64      1.05 GB/s    10       Pike & Alakuijala
 FNV             0.55 GB/s     5       Fowler, Noll, Vo
 CRC32           0.43 GB/s     9
 MD5-32          0.33 GB/s    10       Ronald L. Rivest
 SHA1-32         0.28 GB/s    10

xxHash

Possibly xxhash could help?

Background articles

Rails Source Code References

@andreasklinger

  1. Did the hashing come up as any performance bottleneck? Rails source uses md5, google search reveals that fastest might now be xxhash.
  2. Look at your code again, I'm guessing that the "args" are whatever makes sense for your caching and not necessarily what's used for computing the props.
  3. It's clever how you preserve the cache if your main.js stays the same along with all your serializers.

from react_on_rails.

justin808 avatar justin808 commented on July 23, 2024

We'll add a view helper method for creating a cache key in the near future. This will be a nice addition for this gem.

from react_on_rails.

lfittl avatar lfittl commented on July 23, 2024

@justin808 You are correct that we choose to use the args vs the serializer output because of the slowness of computing the hash (I'm the one who wrote that code, its been a few months though).

This was especially obvious when you assume that both the cache output and the pre-render output are cached (based on the same initial serializer input args), in this case you'd want to minimize overhead.

However I think it'd be fine to re-evaluate the use of a hash method here - I think we tried sha1sum of the JSON serializer output at one point, but using the original arguments was the easier choice.

from react_on_rails.

justin808 avatar justin808 commented on July 23, 2024

@robwise @alexfedoseev @dylangrafmyre This "issue" is relevant for our upcoming incorporation of fragment caching. We should aim to create a generic helper function, possibly part of our upcoming "React on Rails Pro" premium subscription.

from react_on_rails.

justin808 avatar justin808 commented on July 23, 2024

This will be solved by the hashes of the files from webpacker. The key is that the JS bundles will have to be separated, and the cache call will requiring specifying the dependent bundles.

from react_on_rails.

corsonknowles avatar corsonknowles commented on July 23, 2024

What's the status on this? I am investigating memory issue (~leak) in our app coming from gems and have narrowed it to either this or Delayed Jobs

Reference: https://github.com/ASoftCo/leaky-gems/blob/master/README.md

from react_on_rails.

justin808 avatar justin808 commented on July 23, 2024

@corsonknowles A helper for caching is definitely doable. We haven't had enough users like you asking for it.

I'd be happy to work with you on this if you like.

I don't have any comment on the memory leak other than tons of apps are live on React on Rails and nobody is blaming it.

from react_on_rails.

corsonknowles avatar corsonknowles commented on July 23, 2024

Thanks! Our memory issue spontaneously resolved during refactoring to move to Rails 5.1.4, but it was almost certainly related to Delayed Jobs and not server rendering.

from react_on_rails.

mapreal19 avatar mapreal19 commented on July 23, 2024

what about having the react component helper to support caching (passing cache: true)?

cc @robwise @justin808

from react_on_rails.

robwise avatar robwise commented on July 23, 2024

I think it's a good idea, that way checking the server bundle can be done automatically under the hood if cache: true and prerender: true are both passed.

from react_on_rails.

justin808 avatar justin808 commented on July 23, 2024

Caching is a part of React on Rails Pro. Contact [email protected] for more info.

from react_on_rails.

Related Issues (20)

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.