Repository: rails/rails
File: activesupport/lib/active_support/cache/serializer_with_fallback.rb (Ruby)

Code before:

      private
        BARE_STRING_SIGNATURES = {
          255 => Encoding::UTF_8,
          254 => Encoding::BINARY,
          253 => Encoding::US_ASCII,
        }
        BARE_STRING_TEMPLATE = "CEl<"
        BARE_STRING_VERSION_INDEX = [0, 0.0, 0].pack(BARE_STRING_TEMPLATE).bytesize

        def try_dump_bare_string(entry)
          if entry.value.instance_of?(String) && signature = BARE_STRING_SIGNATURES.key(entry.value.encoding)
            packed = [
              signature,
              entry.expires_at || -1.0,
              entry.version&.bytesize || -1,
            ].pack(BARE_STRING_TEMPLATE)

            packed << entry.version if entry.version
            packed << entry.value
          end
        end

        def try_load_bare_string(dumped)
          if encoding = BARE_STRING_SIGNATURES[dumped.getbyte(0)]
            _, expires_at, version_length = dumped.unpack(BARE_STRING_TEMPLATE)
            value_index = BARE_STRING_VERSION_INDEX + [version_length, 0].max

            Cache::Entry.new(
              dumped.byteslice(value_index..-1).force_encoding(encoding),
              version: dumped.byteslice(BARE_STRING_VERSION_INDEX, version_length),
              expires_at: (expires_at unless expires_at < 0),
            )
          end
        end

        ZLIB_HEADER = "\x78".b.freeze

        def compressed?(dumped)
          dumped.start_with?(ZLIB_HEADER)
        end

        def compress(dumped)
          Zlib::Deflate.deflate(dumped)
        end

        def try_compress(dumped, threshold)
          if dumped.bytesize >= threshold
            compressed = compress(dumped)
            compressed unless compressed.bytesize >= dumped.bytesize
          end

Review comment (suggestion):
Not sure if worth it, but you could save that array allocation by using `unpack1` with `@` directives, e.g. 

```ruby
>> [255, 23.45, 10].pack("CEl<")
=> "\xFF33333s7@\n\x00\x00\x00"
>> [255, 23.45, 10].pack("CEl<").unpack1("@1E")
=> 23.45
>> [255, 23.45, 10].pack("CEl<").unpack1("@9l<")
=> 10
```

Code after:

      private
        BARE_STRING_SIGNATURES = {
          255 => Encoding::UTF_8,
          254 => Encoding::BINARY,
          253 => Encoding::US_ASCII,
        }
        BARE_STRING_TEMPLATE = "CEl<"
        BARE_STRING_EXPIRES_AT_TEMPLATE = "@1E"
        BARE_STRING_VERSION_LENGTH_TEMPLATE = "@#{[0].pack(BARE_STRING_EXPIRES_AT_TEMPLATE).bytesize}l<"
        BARE_STRING_VERSION_INDEX = [0].pack(BARE_STRING_VERSION_LENGTH_TEMPLATE).bytesize

        def try_dump_bare_string(entry)
          value = entry.value
          return if !value.instance_of?(String)

          version = entry.version
          return if version && version.encoding != Encoding::UTF_8

          signature = BARE_STRING_SIGNATURES.key(value.encoding)
          return if !signature

          packed = [signature, entry.expires_at || -1.0, version&.bytesize || -1].pack(BARE_STRING_TEMPLATE)
          packed << version if version
          packed << value
        end

        def try_load_bare_string(dumped)
          encoding = BARE_STRING_SIGNATURES[dumped.getbyte(0)]
          return if !encoding
          expires_at = dumped.unpack1(BARE_STRING_EXPIRES_AT_TEMPLATE)
          version_length = dumped.unpack1(BARE_STRING_VERSION_LENGTH_TEMPLATE)
          value_index = BARE_STRING_VERSION_INDEX + [version_length, 0].max

          Cache::Entry.new(
            dumped.byteslice(value_index..-1).force_encoding(encoding),
            version: dumped.byteslice(BARE_STRING_VERSION_INDEX, version_length)&.force_encoding(Encoding::UTF_8),
            expires_at: (expires_at unless expires_at < 0),
          )
        end

        ZLIB_HEADER = "\x78".b.freeze

        def compressed?(dumped)
          dumped.start_with?(ZLIB_HEADER)
        end

        def compress(dumped)
          Zlib::Deflate.deflate(dumped)
        end