<commit_before>module Celluloid
  module IO
    # Common implementations of methods originall from the IO class
    module CommonMethods
      # Are we inside of a Celluloid::IO actor?
      def evented?
        actor = Thread.current[:actor]
        actor && actor.mailbox.is_a?(Celluloid::IO::Mailbox)
      end

      # Wait until the current object is readable
      def wait_readable
        if evented?
          Celluloid.current_actor.wait_readable(self.to_io)
        else
          Kernel.select([self.to_io])
        end
      end

      # Wait until the current object is writable
      def wait_writable
        actor = Thread.current[:actor]

        if evented?
          Celluloid.current_actor.wait_writable(self.to_io)
        else
          Kernel.select([], [self.to_io])
        end
      end

      def read(length, buffer = nil)
        buffer ||= ''
        remaining = length

        until remaining.zero?
          begin
            str = readpartial(remaining)
          rescue EOFError
            return if length == remaining
            return buffer
          end

          buffer << str
          remaining -= str.length
        end

        buffer
      end

      def readpartial(length, buffer = nil)
        buffer ||= ''

        begin
          read_nonblock(length, buffer)
        rescue ::IO::WaitReadable
          wait_readable
          retry
        end

        buffer
      end

      def write(string)
        length = string.length
        total_written = 0

        while total_written < length
          begin
            written = write_nonblock(string)
          rescue ::IO::WaitWritable
            wait_writable
            retry
          rescue EOFError
            return total_written
          end

          total_written += written
          if written < string.length
            # Probably not the most efficient way to do this
            string = string[written..-1]
          end
        end

        total_written
      end
      alias_method :<<, :write
    end
  end
end<commit_msg>Avoid successively copying the input string when writing large buffers<commit_after>module Celluloid
  module IO
    # Common implementations of methods originall from the IO class
    module CommonMethods
      # Are we inside of a Celluloid::IO actor?
      def evented?
        actor = Thread.current[:actor]
        actor && actor.mailbox.is_a?(Celluloid::IO::Mailbox)
      end

      # Wait until the current object is readable
      def wait_readable
        if evented?
          Celluloid.current_actor.wait_readable(self.to_io)
        else
          Kernel.select([self.to_io])
        end
      end

      # Wait until the current object is writable
      def wait_writable
        actor = Thread.current[:actor]

        if evented?
          Celluloid.current_actor.wait_writable(self.to_io)
        else
          Kernel.select([], [self.to_io])
        end
      end

      def read(length, buffer = nil)
        buffer ||= ''
        remaining = length

        until remaining.zero?
          begin
            str = readpartial(remaining)
          rescue EOFError
            return if length == remaining
            return buffer
          end

          buffer << str
          remaining -= str.length
        end

        buffer
      end

      def readpartial(length, buffer = nil)
        buffer ||= ''

        begin
          read_nonblock(length, buffer)
        rescue ::IO::WaitReadable
          wait_readable
          retry
        end

        buffer
      end

      def write(string)
        length = string.length
        total_written = 0

        remaining = string
        while total_written < length
          begin
            written = write_nonblock(remaining)
          rescue ::IO::WaitWritable
            wait_writable
            retry
          rescue EOFError
            return total_written
          end

          total_written += written
          if written < remaining.length
            # Avoid mutating string itself, but we can mutate the remaining data
            if remaining == string
              # Copy the remaining data so as to avoid mutating string
              # Note if we have a large amount of data remaining this could be slow
              remaining = string[written..-1]
            else
              remaining.slice!(0, written)
            end
          end
        end

        total_written
      end
      alias_method :<<, :write
    end
  end
end
{'fraction_non_alphanumeric': 0.05853549966614734, 'fraction_numerical': 0.001112842198976185, 'mean_word_length': 1.536117381489842, 'pattern_counts': {'":': 0, '<': 15, '<?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': '5144835', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1131, 'n_words': 349}