Check-in [2cec92909f]
Overview
SHA1:2cec92909fddd24feaed596e3bc146c74d524658
Date: 2014-12-24 23:58:32
User: spaskalev
Comment:0dev.org/ioutil/SizedReader is now as fast as bufio.Reader if not faster for a buffer of 4096 bytes. Switched pdc to use it for decompression buffering and removed profiling code from the former
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2014-12-25
00:26
[46da7a6ae9] Extracted SizedWriter to a sizedWriter struct with a Write() method. (user: spaskalev, tags: trunk)
2014-12-24
23:58
[2cec92909f] 0dev.org/ioutil/SizedReader is now as fast as bufio.Reader if not faster for a buffer of 4096 bytes. Switched pdc to use it for decompression buffering and removed profiling code from the former (user: spaskalev, tags: trunk)
23:45
[b703c38e0b] Extracted SizedReader to a sizedReader type with a Read() method. Closures seem to be slower in Go 1.4 and there is no real need for SizedReader to be a closure. (user: spaskalev, tags: trunk)
Changes

Modified src/0dev.org/commands/pdc/main.go from [675059111f] to [dfe0b7b6cf].

     1      1   package main
     2      2   
     3      3   import (
     4         -	pprof "0dev.org/debug/pprof"
     5      4   	iou "0dev.org/ioutil"
     6      5   	predictor "0dev.org/predictor"
     7         -	// "bufio"
     8      6   	"fmt"
     9      7   	"io"
    10      8   	"os"
    11      9   )
    12     10   
    13     11   func main() {
    14     12   	var code int
................................................................................
    16     14   	case len(os.Args) == 1:
    17     15   		code = compress(os.Stdout, os.Stdin)
    18     16   	case len(os.Args) == 2 && os.Args[1] == "-d":
    19     17   		code = decompress(os.Stdout, os.Stdin)
    20     18   	default:
    21     19   		fmt.Fprintln(os.Stdout, "Usage: pdc [-d]")
    22     20   	}
    23         -
    24         -	pprof.Stop()
    25         -
    26     21   	os.Exit(code)
    27     22   }
    28     23   
    29     24   // Compress the data from the given io.Reader and write it to the given io.Writer
    30     25   // I/O is buffered for better performance
    31     26   func compress(output io.Writer, input io.Reader) int {
    32     27   	var (
................................................................................
    61     56   // Decompress the data from the given io.Reader and write it to the given io.Writer
    62     57   // I/O is buffered for better performance
    63     58   func decompress(output io.Writer, input io.Reader) int {
    64     59   	var (
    65     60   		err          error
    66     61   		buffer       io.Writer = iou.SizedWriter(output, 4096)
    67     62   		decompressor io.Reader = predictor.Decompressor(iou.SizedReader(input, 4096))
    68         -		//decompressor io.Reader = predictor.Decompressor(bufio.NewReader(input))
    69     63   	)
    70     64   
    71     65   	_, err = io.Copy(buffer, decompressor)
    72     66   	if err != nil {
    73     67   		fmt.Fprintln(os.Stderr, "Error while decompressing.\n", err)
    74     68   		return 1
    75     69   	}

Modified src/0dev.org/ioutil/ioutil.go from [c84493a1fe] to [9862ea3396].

   103    103   	var (
   104    104   		count int
   105    105   		err   error
   106    106   	)
   107    107   
   108    108   start:
   109    109   	// Reply with the buffered data if there is any
   110         -	if sr.to-sr.from > 0 {
          110  +	if sr.to > 0 {
   111    111   		count = copy(output, sr.buffer[sr.from:sr.to])
   112    112   
   113    113   		// Advance the data in the buffer
   114    114   		sr.from += count
   115    115   
   116    116   		// Check whether we have reached the end of the buffer
   117         -		if sr.to-sr.from == 0 {
          117  +		if sr.from == sr.to {
   118    118   			// Reset the buffer
   119    119   			sr.from, sr.to = 0, 0
   120    120   
   121    121   			return count, err
   122    122   		}
   123    123   
   124    124   		// Do not propagate an error until the buffer is exhausted
................................................................................
   132    132   
   133    133   	// Perform a read into the buffer
   134    134   	count, err = sr.reader.Read(sr.buffer)
   135    135   
   136    136   	// Size the buffer down to the read data size
   137    137   	// and restart if we have successfully read some bytes
   138    138   	sr.from, sr.to = 0, count
   139         -	if sr.to-sr.from > 0 {
          139  +	if sr.to > 0 {
   140    140   		goto start
   141    141   	}
   142    142   
   143    143   	// Returning on err/misbehaving noop reader
   144    144   	return 0, err
   145    145   }