Check-in [0f4bc650d1]
Overview
Comment:Removed err variable from compressor
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0f4bc650d1a9c78739435916114e097bab766019
User & Date: spaskalev on 2014-12-16 23:29:42
Other Links: manifest | tags
Context
2014-12-19
21:54
Implemented commands/pdc using predictor. Made predictor's Compressor(...) return an io.Writer. check-in: c9f3a59cb6 user: spaskalev tags: trunk
2014-12-16
23:29
Removed err variable from compressor check-in: 0f4bc650d1 user: spaskalev tags: trunk
22:56
Reworked the compressor's buffering code, switched to table testing, add step testing for the compressor. Code coverage at 88.3% check-in: 1847f77062 user: spaskalev tags: trunk
Changes

Modified src/0dev.org/predictor/predictor.go from [2d1c692ccb] to [2cc1907fb9].

    22     22   	ctx.input = ctx.buffer[:0]
    23     23   
    24     24   	// Forward declaration as it is required for recursion
    25     25   	var write func(data []byte) error
    26     26   
    27     27   	write = func(data []byte) error {
    28     28   		var (
    29         -			err          error
    30     29   			blockSize    int = 8
    31     30   			bufferLength int = len(ctx.input)
    32     31   		)
    33     32   
    34     33   		// Force a flush if we are called with no data to write
    35     34   		if len(data) == 0 {
    36     35   			// Nothing to flush if the buffer is empty though
................................................................................
    55     54   				// ... otherwise just return
    56     55   				return nil
    57     56   			}
    58     57   
    59     58   			// The current buffer + new data overflow the block size
    60     59   			// Complete the block, flush it ...
    61     60   			ctx.input = append(ctx.input, data[:blockSize-bufferLength]...)
    62         -			err = write(nil)
    63         -			if err != nil {
           61  +			if err := write(nil); err != nil {
    64     62   				return err
    65     63   			}
    66     64   			// ... and stage the rest of the data in the buffer
    67     65   			ctx.input = append(ctx.input, data[blockSize-bufferLength:]...)
    68     66   			return nil
    69     67   		}
    70     68   
           69  +		// TODO allocate this on ctx.buffer ...
    71     70   		var buf []byte = make([]byte, 1, blockSize+1)
    72     71   		for block := 0; block < len(data)/blockSize; block++ {
    73     72   			for i := 0; i < blockSize; i++ {
    74     73   				var current byte = data[(block*blockSize)+i]
    75     74   				if ctx.table[ctx.hash] == current {
    76     75   					// Guess was right - don't output
    77     76   					buf[0] |= 1 << uint(i)
................................................................................
    79     78   					// Guess was wrong, output char
    80     79   					ctx.table[ctx.hash] = current
    81     80   					buf = append(buf, current)
    82     81   				}
    83     82   				ctx.hash = (ctx.hash << 4) ^ uint16(current)
    84     83   			}
    85     84   
    86         -			_, err = writer.Write(buf)
    87         -			if err != nil {
           85  +			if _, err := writer.Write(buf); err != nil {
    88     86   				return err
    89     87   			}
    90     88   
    91     89   			// Reset the flags and buffer for the next iteration
    92         -			buf[0] ^= buf[0]
    93         -			buf = buf[:1]
           90  +			buf, buf[0] = buf[:1], 0
    94     91   		}
    95     92   
    96         -		var remaining int = len(data) % blockSize
    97         -		if remaining > 0 {
           93  +		if remaining := len(data) % blockSize; remaining > 0 {
    98     94   			ctx.input = ctx.buffer[:remaining]
    99     95   			copy(ctx.input, data[len(data)-remaining:])
   100     96   		} else {
   101     97   			ctx.input = ctx.buffer[:0]
   102     98   		}
   103     99   
   104    100   		return nil