<commit_before>module Protobuf
  class Encoder

    def self.encode(stream, message)
      new(stream, message).encode
    end

    private

    attr_writer :message, :stream

    public

    attr_reader :message, :stream

    def initialize(message, stream)
      unless message.respond_to?(:each_field_for_serialization)
        fail ArgumentError, "Message instance must respond to :each_field_for_serialization"
      end

      self.message = message
      self.stream = stream
    end

    def encode
      message.each_field_for_serialization do |field, value|
        encode_field(field, value)
      end

      stream
    end

    private

    def encode_field(field, value)
      if field.repeated?
        encode_repeated_field(field, value)
      else
        write_pair(field, value)
      end
    end

    def encode_packed_field(field, value)
      key = (field.tag << 3) | ::Protobuf::WireType::LENGTH_DELIMITED
      packed_value = value.map { |val| field.encode(val) }.join
      stream << ::Protobuf::Field::VarintField.encode(key)
      stream << ::Protobuf::Field::VarintField.encode(packed_value.size)
      stream << packed_value
    end

    def encode_repeated_field(field, value)
      if field.packed?
        encode_packed_field(field, value)
      else
        value.each { |val| write_pair(field, val) }
      end
    end

    # Encode key and value, and write to +stream+.
    def write_pair(field, value)
      key = (field.tag << 3) | field.wire_type
      stream << ::Protobuf::Field::VarintField.encode(key)
      stream << field.encode(value)
    end

  end
end<commit_msg>reduce encoder to class method as object instantiation here (#new) was problem of creating new and collecting objects for a small function<commit_after>module Protobuf
  class Encoder
    def self.encode(message, stream)
      message.each_field_for_serialization do |field, value|
        if field.repeated?
          if field.packed?
            key = (field.tag << 3) | ::Protobuf::WireType::LENGTH_DELIMITED
            packed_value = value.map { |val| field.encode(val) }.join
            stream << ::Protobuf::Field::VarintField.encode(key)
            stream << ::Protobuf::Field::VarintField.encode(packed_value.size)
            stream << packed_value
          else
            value.each do |val|
              key = (field.tag << 3) | field.wire_type
              stream << "#{::Protobuf::Field::VarintField.encode(key)}#{field.encode(value)}"
            end
          end
        else
          key = (field.tag << 3) | field.wire_type
          stream << "#{::Protobuf::Field::VarintField.encode(key)}#{field.encode(value)}"
        end
      end

      stream
    end
  end
end
{'fraction_non_alphanumeric': 0.10321864594894561, 'fraction_numerical': 0.001849796522382538, 'mean_word_length': 2.5485564304461943, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7355761', 'n_tokens_mistral': 812, 'n_tokens_neox': 785, 'n_words': 191}