rails/activesupport
Jonathan Hefner 4c4f4c385a Avoid double JSON parse for metadata-less messages
In #46612, a check was added to only attempt metadata extraction if the
message looks like a JSON object (i.e. starts with "{"), thus avoiding
an unnecessary JSON parse and possible exception.

This commit extends the check to only attempt metadata extraction if the
message looks like a metadata wrapper object (i.e. has the "_rails"
key).  This avoids an unnecessary JSON parse of JSON object messages
that don't have metadata.

**Benchmark**

  ```ruby
  require "benchmark/ips"
  require "active_support/all"

  verifier = ActiveSupport::MessageVerifier.new("secret", serializer: JSON)
  message_100 = verifier.generate({ content: "x" * 100 })
  message_1m = verifier.generate({ content: "x" * 1_000_000 })

  Benchmark.ips do |x|
    x.report("100 chars") do
      verifier.verify(message_100)
    end

    x.report("1m chars") do
      verifier.verify(message_1m)
    end
  end
  ```

**Before**

  ```
  Warming up --------------------------------------
             100 chars     2.803k i/100ms
              1m chars     6.000  i/100ms
  Calculating -------------------------------------
             100 chars     27.762k (± 1.6%) i/s -    140.150k in   5.049649s
              1m chars     83.516  (±16.8%) i/s -    402.000  in   5.037269s
  ```

**After**

  ```
  Warming up --------------------------------------
             100 chars     3.360k i/100ms
              1m chars     9.000  i/100ms
  Calculating -------------------------------------
             100 chars     33.480k (± 1.7%) i/s -    168.000k in   5.019311s
              1m chars    113.373  (±15.0%) i/s -    549.000  in   5.023443s
  ```
2022-12-21 14:12:15 -06:00
..
bin Remove AS::Multibyte's unicode table 2018-02-20 03:58:22 +09:00
lib Avoid double JSON parse for metadata-less messages 2022-12-21 14:12:15 -06:00
test Add Rails.env.local? (#46786) 2022-12-21 15:38:33 +01:00
.gitignore Clean up and consolidate .gitignores 2018-02-17 14:26:19 -08:00
activesupport.gemspec Enable connection pooling by default for MemCacheStore and RedisCacheStore 2022-06-07 11:40:17 +03:00
CHANGELOG.md Add Rails.env.local? (#46786) 2022-12-21 15:38:33 +01:00
MIT-LICENSE Bump license years to 2022 [ci-skip] 2022-01-01 15:22:15 +09:00
Rakefile Stop testing hiredis 2022-08-22 09:01:27 +02:00
README.rdoc Rename master to main in all code references 2021-01-19 20:46:33 +00:00

= Active Support -- Utility classes and Ruby extensions from Rails

Active Support is a collection of utility classes and standard library
extensions that were found useful for the Rails framework. These additions
reside in this package so they can be loaded as needed in Ruby projects
outside of Rails.

You can read more about the extensions in the {Active Support Core Extensions}[https://edgeguides.rubyonrails.org/active_support_core_extensions.html] guide.

== Download and installation

The latest version of Active Support can be installed with RubyGems:

  $ gem install activesupport

Source code can be downloaded as part of the Rails project on GitHub:

* https://github.com/rails/rails/tree/main/activesupport


== License

Active Support is released under the MIT license:

* https://opensource.org/licenses/MIT


== Support

API documentation is at:

* https://api.rubyonrails.org

Bug reports for the Ruby on Rails project can be filed here:

* https://github.com/rails/rails/issues

Feature requests should be discussed on the rails-core mailing list here:

* https://discuss.rubyonrails.org/c/rubyonrails-core