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
2
3
4
5
6
7
8
9
10
11
12
13
14
..
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
..
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main

import (
	pprof "0dev.org/debug/pprof"
	iou "0dev.org/ioutil"
	predictor "0dev.org/predictor"
	// "bufio"
	"fmt"
	"io"
	"os"
)

func main() {
	var code int
................................................................................
	case len(os.Args) == 1:
		code = compress(os.Stdout, os.Stdin)
	case len(os.Args) == 2 && os.Args[1] == "-d":
		code = decompress(os.Stdout, os.Stdin)
	default:
		fmt.Fprintln(os.Stdout, "Usage: pdc [-d]")
	}

	pprof.Stop()

	os.Exit(code)
}

// Compress the data from the given io.Reader and write it to the given io.Writer
// I/O is buffered for better performance
func compress(output io.Writer, input io.Reader) int {
	var (
................................................................................
// Decompress the data from the given io.Reader and write it to the given io.Writer
// I/O is buffered for better performance
func decompress(output io.Writer, input io.Reader) int {
	var (
		err          error
		buffer       io.Writer = iou.SizedWriter(output, 4096)
		decompressor io.Reader = predictor.Decompressor(iou.SizedReader(input, 4096))
		//decompressor io.Reader = predictor.Decompressor(bufio.NewReader(input))
	)

	_, err = io.Copy(buffer, decompressor)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while decompressing.\n", err)
		return 1
	}



<


<







 







<
<
<







 







<







1
2
3

4
5

6
7
8
9
10
11
12
..
14
15
16
17
18
19
20



21
22
23
24
25
26
27
..
56
57
58
59
60
61
62

63
64
65
66
67
68
69
package main

import (

	iou "0dev.org/ioutil"
	predictor "0dev.org/predictor"

	"fmt"
	"io"
	"os"
)

func main() {
	var code int
................................................................................
	case len(os.Args) == 1:
		code = compress(os.Stdout, os.Stdin)
	case len(os.Args) == 2 && os.Args[1] == "-d":
		code = decompress(os.Stdout, os.Stdin)
	default:
		fmt.Fprintln(os.Stdout, "Usage: pdc [-d]")
	}



	os.Exit(code)
}

// Compress the data from the given io.Reader and write it to the given io.Writer
// I/O is buffered for better performance
func compress(output io.Writer, input io.Reader) int {
	var (
................................................................................
// Decompress the data from the given io.Reader and write it to the given io.Writer
// I/O is buffered for better performance
func decompress(output io.Writer, input io.Reader) int {
	var (
		err          error
		buffer       io.Writer = iou.SizedWriter(output, 4096)
		decompressor io.Reader = predictor.Decompressor(iou.SizedReader(input, 4096))

	)

	_, err = io.Copy(buffer, decompressor)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error while decompressing.\n", err)
		return 1
	}

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

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	var (
		count int
		err   error
	)

start:
	// Reply with the buffered data if there is any
	if sr.to-sr.from > 0 {
		count = copy(output, sr.buffer[sr.from:sr.to])

		// Advance the data in the buffer
		sr.from += count

		// Check whether we have reached the end of the buffer
		if sr.to-sr.from == 0 {
			// Reset the buffer
			sr.from, sr.to = 0, 0

			return count, err
		}

		// Do not propagate an error until the buffer is exhausted
................................................................................

	// Perform a read into the buffer
	count, err = sr.reader.Read(sr.buffer)

	// Size the buffer down to the read data size
	// and restart if we have successfully read some bytes
	sr.from, sr.to = 0, count
	if sr.to-sr.from > 0 {
		goto start
	}

	// Returning on err/misbehaving noop reader
	return 0, err
}







|






|







 







|






103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	var (
		count int
		err   error
	)

start:
	// Reply with the buffered data if there is any
	if sr.to > 0 {
		count = copy(output, sr.buffer[sr.from:sr.to])

		// Advance the data in the buffer
		sr.from += count

		// Check whether we have reached the end of the buffer
		if sr.from == sr.to {
			// Reset the buffer
			sr.from, sr.to = 0, 0

			return count, err
		}

		// Do not propagate an error until the buffer is exhausted
................................................................................

	// Perform a read into the buffer
	count, err = sr.reader.Read(sr.buffer)

	// Size the buffer down to the read data size
	// and restart if we have successfully read some bytes
	sr.from, sr.to = 0, count
	if sr.to > 0 {
		goto start
	}

	// Returning on err/misbehaving noop reader
	return 0, err
}