Commit 0c33d435 authored by Rob Pike's avatar Rob Pike

arrays, not slices, and only with non-pointer elements.

(actually slices encode but do not decode yet)

R=rsc
DELTA=221  (82 added, 65 deleted, 74 changed)
OCL=31095
CL=31095
parent 29e6eb21
This diff is collapsed.
...@@ -20,7 +20,6 @@ import ( ...@@ -20,7 +20,6 @@ import (
type DecState struct { type DecState struct {
r io.Reader; r io.Reader;
err os.Error; err os.Error;
base uintptr;
fieldnum int; // the last field number read. fieldnum int; // the last field number read.
buf [1]byte; // buffer used by the decoder; here to avoid allocation. buf [1]byte; // buffer used by the decoder; here to avoid allocation.
} }
...@@ -279,11 +278,21 @@ type decEngine struct { ...@@ -279,11 +278,21 @@ type decEngine struct {
instr []decInstr instr []decInstr
} }
func (engine *decEngine) decodeStruct(r io.Reader, p uintptr) os.Error { func decodeStruct(engine *decEngine, rtyp reflect.StructType, r io.Reader, p uintptr, indir int) os.Error {
if indir > 0 {
up := unsafe.Pointer(p);
if *(*unsafe.Pointer)(up) == nil {
// Allocate the structure by making a slice of bytes and recording the
// address of the beginning of the array. TODO(rsc).
b := make([]byte, rtyp.Size());
*(*unsafe.Pointer)(up) = unsafe.Pointer(&b[0]);
}
p = *(*uintptr)(up);
}
state := new(DecState); state := new(DecState);
state.r = r; state.r = r;
state.base = p;
state.fieldnum = -1; state.fieldnum = -1;
basep := p;
for state.err == nil { for state.err == nil {
delta := int(DecodeUint(state)); delta := int(DecodeUint(state));
if state.err != nil || delta == 0 { // struct terminator is zero delta fieldnum if state.err != nil || delta == 0 { // struct terminator is zero delta fieldnum
...@@ -294,7 +303,7 @@ func (engine *decEngine) decodeStruct(r io.Reader, p uintptr) os.Error { ...@@ -294,7 +303,7 @@ func (engine *decEngine) decodeStruct(r io.Reader, p uintptr) os.Error {
panicln("TODO(r): need to handle unknown data"); panicln("TODO(r): need to handle unknown data");
} }
instr := &engine.instr[fieldnum]; instr := &engine.instr[fieldnum];
p := unsafe.Pointer(state.base+instr.offset); p := unsafe.Pointer(basep+instr.offset);
if instr.indir > 1 { if instr.indir > 1 {
p = decIndirect(p, instr.indir); p = decIndirect(p, instr.indir);
} }
...@@ -304,6 +313,28 @@ func (engine *decEngine) decodeStruct(r io.Reader, p uintptr) os.Error { ...@@ -304,6 +313,28 @@ func (engine *decEngine) decodeStruct(r io.Reader, p uintptr) os.Error {
return state.err return state.err
} }
func decodeArray(atyp reflect.ArrayType, state *DecState, p uintptr, elemOp decOp, elemWid int, length int, indir int) os.Error {
if indir > 0 {
up := unsafe.Pointer(p);
if *(*unsafe.Pointer)(up) == nil {
// Allocate the structure by making a slice of bytes and recording the
// address of the beginning of the array. TODO(rsc).
b := make([]byte, atyp.Size());
*(*unsafe.Pointer)(up) = unsafe.Pointer(&b[0]);
}
p = *(*uintptr)(up);
}
instr := &decInstr{elemOp, 0, 0, 0}; // TODO(r): indir on elements
if DecodeUint(state) != uint64(length) {
state.err = os.ErrorString("length mismatch in decodeArray");
}
for i := 0; i < length && state.err == nil; i++ {
elemOp(instr, state, unsafe.Pointer(p));
p += uintptr(elemWid);
}
return state.err
}
var decEngineMap = make(map[reflect.Type] *decEngine) var decEngineMap = make(map[reflect.Type] *decEngine)
var decOpMap = map[int] decOp { var decOpMap = map[int] decOp {
reflect.BoolKind: decBool, reflect.BoolKind: decBool,
...@@ -331,16 +362,23 @@ func decOpFor(typ reflect.Type) decOp { ...@@ -331,16 +362,23 @@ func decOpFor(typ reflect.Type) decOp {
// Special cases // Special cases
if typ.Kind() == reflect.ArrayKind { if typ.Kind() == reflect.ArrayKind {
atyp := typ.(reflect.ArrayType); atyp := typ.(reflect.ArrayType);
switch atyp.Elem().Kind() { switch {
case reflect.Uint8Kind: case atyp.Elem().Kind() == reflect.Uint8Kind:
op = decUint8Array op = decUint8Array
case atyp.IsSlice():
case !atyp.IsSlice():
elemOp := decOpFor(atyp.Elem());
op = func(i *decInstr, state *DecState, p unsafe.Pointer) {
state.err = decodeArray(atyp, state, uintptr(p), elemOp, atyp.Elem().Size(), atyp.Len(), i.indir);
};
} }
} }
if typ.Kind() == reflect.StructKind { if typ.Kind() == reflect.StructKind {
// Generate a closure that calls out to the engine for the nested type. // Generate a closure that calls out to the engine for the nested type.
engine := getDecEngine(typ); engine := getDecEngine(typ);
styp := typ.(reflect.StructType);
op = func(i *decInstr, state *DecState, p unsafe.Pointer) { op = func(i *decInstr, state *DecState, p unsafe.Pointer) {
state.err = engine.decodeStruct(state.r, uintptr(p)) state.err = decodeStruct(engine, styp, state.r, uintptr(p), i.indir)
}; };
} }
} }
...@@ -401,11 +439,11 @@ func Decode(r io.Reader, e interface{}) os.Error { ...@@ -401,11 +439,11 @@ func Decode(r io.Reader, e interface{}) os.Error {
rt = pt.Sub(); rt = pt.Sub();
v = reflect.Indirect(v); v = reflect.Indirect(v);
} }
if v.Kind() != reflect.StructKind { if rt.Kind() != reflect.StructKind {
return os.ErrorString("decode can't handle " + v.Type().String()) return os.ErrorString("decode can't handle " + rt.String())
} }
typeLock.Lock(); typeLock.Lock();
engine := getDecEngine(rt); engine := getDecEngine(rt);
typeLock.Unlock(); typeLock.Unlock();
return engine.decodeStruct(r, uintptr(v.Addr())); return decodeStruct(engine, rt.(reflect.StructType), r, uintptr(v.Addr()), 0);
} }
...@@ -20,7 +20,6 @@ import ( ...@@ -20,7 +20,6 @@ import (
// 0 terminates the structure. // 0 terminates the structure.
type EncState struct { type EncState struct {
w io.Writer; w io.Writer;
base uintptr; // the base address of the data structure being written
err os.Error; // error encountered during encoding; err os.Error; // error encountered during encoding;
fieldnum int; // the last field number written. fieldnum int; // the last field number written.
buf [16]byte; // buffer used by the encoder; here to avoid allocation. buf [16]byte; // buffer used by the encoder; here to avoid allocation.
...@@ -71,6 +70,15 @@ type encInstr struct { ...@@ -71,6 +70,15 @@ type encInstr struct {
offset uintptr; // offset in the structure of the field to encode offset uintptr; // offset in the structure of the field to encode
} }
// Emit a field number and update the state to record its value for delta encoding.
// If the instruction pointer is nil, do nothing
func (state *EncState) update(instr *encInstr) {
if instr != nil {
EncodeUint(state, uint64(instr.field - state.fieldnum));
state.fieldnum = instr.field;
}
}
// Each encoder is responsible for handling any indirections associated // Each encoder is responsible for handling any indirections associated
// with the data structure. If any pointer so reached is nil, no bytes are written. // with the data structure. If any pointer so reached is nil, no bytes are written.
// If the data item is zero, no bytes are written. // If the data item is zero, no bytes are written.
...@@ -90,99 +98,88 @@ func encIndirect(p unsafe.Pointer, indir int) unsafe.Pointer { ...@@ -90,99 +98,88 @@ func encIndirect(p unsafe.Pointer, indir int) unsafe.Pointer {
func encBool(i *encInstr, state *EncState, p unsafe.Pointer) { func encBool(i *encInstr, state *EncState, p unsafe.Pointer) {
b := *(*bool)(p); b := *(*bool)(p);
if b { if b {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeUint(state, 1); EncodeUint(state, 1);
state.fieldnum = i.field;
} }
} }
func encInt(i *encInstr, state *EncState, p unsafe.Pointer) { func encInt(i *encInstr, state *EncState, p unsafe.Pointer) {
v := int64(*(*int)(p)); v := int64(*(*int)(p));
if v != 0 { if v != 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeInt(state, v); EncodeInt(state, v);
state.fieldnum = i.field;
} }
} }
func encUint(i *encInstr, state *EncState, p unsafe.Pointer) { func encUint(i *encInstr, state *EncState, p unsafe.Pointer) {
v := uint64(*(*uint)(p)); v := uint64(*(*uint)(p));
if v != 0 { if v != 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeUint(state, v); EncodeUint(state, v);
state.fieldnum = i.field;
} }
} }
func encInt8(i *encInstr, state *EncState, p unsafe.Pointer) { func encInt8(i *encInstr, state *EncState, p unsafe.Pointer) {
v := int64(*(*int8)(p)); v := int64(*(*int8)(p));
if v != 0 { if v != 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeInt(state, v); EncodeInt(state, v);
state.fieldnum = i.field;
} }
} }
func encUint8(i *encInstr, state *EncState, p unsafe.Pointer) { func encUint8(i *encInstr, state *EncState, p unsafe.Pointer) {
v := uint64(*(*uint8)(p)); v := uint64(*(*uint8)(p));
if v != 0 { if v != 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeUint(state, v); EncodeUint(state, v);
state.fieldnum = i.field;
} }
} }
func encInt16(i *encInstr, state *EncState, p unsafe.Pointer) { func encInt16(i *encInstr, state *EncState, p unsafe.Pointer) {
v := int64(*(*int16)(p)); v := int64(*(*int16)(p));
if v != 0 { if v != 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeInt(state, v); EncodeInt(state, v);
state.fieldnum = i.field;
} }
} }
func encUint16(i *encInstr, state *EncState, p unsafe.Pointer) { func encUint16(i *encInstr, state *EncState, p unsafe.Pointer) {
v := uint64(*(*uint16)(p)); v := uint64(*(*uint16)(p));
if v != 0 { if v != 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeUint(state, v); EncodeUint(state, v);
state.fieldnum = i.field;
} }
} }
func encInt32(i *encInstr, state *EncState, p unsafe.Pointer) { func encInt32(i *encInstr, state *EncState, p unsafe.Pointer) {
v := int64(*(*int32)(p)); v := int64(*(*int32)(p));
if v != 0 { if v != 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeInt(state, v); EncodeInt(state, v);
state.fieldnum = i.field;
} }
} }
func encUint32(i *encInstr, state *EncState, p unsafe.Pointer) { func encUint32(i *encInstr, state *EncState, p unsafe.Pointer) {
v := uint64(*(*uint32)(p)); v := uint64(*(*uint32)(p));
if v != 0 { if v != 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeUint(state, v); EncodeUint(state, v);
state.fieldnum = i.field;
} }
} }
func encInt64(i *encInstr, state *EncState, p unsafe.Pointer) { func encInt64(i *encInstr, state *EncState, p unsafe.Pointer) {
v := *(*int64)(p); v := *(*int64)(p);
if v != 0 { if v != 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeInt(state, v); EncodeInt(state, v);
state.fieldnum = i.field;
} }
} }
func encUint64(i *encInstr, state *EncState, p unsafe.Pointer) { func encUint64(i *encInstr, state *EncState, p unsafe.Pointer) {
v := *(*uint64)(p); v := *(*uint64)(p);
if v != 0 { if v != 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeUint(state, v); EncodeUint(state, v);
state.fieldnum = i.field;
} }
} }
...@@ -206,9 +203,8 @@ func encFloat(i *encInstr, state *EncState, p unsafe.Pointer) { ...@@ -206,9 +203,8 @@ func encFloat(i *encInstr, state *EncState, p unsafe.Pointer) {
f := float(*(*float)(p)); f := float(*(*float)(p));
if f != 0 { if f != 0 {
v := floatBits(float64(f)); v := floatBits(float64(f));
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeUint(state, v); EncodeUint(state, v);
state.fieldnum = i.field;
} }
} }
...@@ -216,19 +212,17 @@ func encFloat32(i *encInstr, state *EncState, p unsafe.Pointer) { ...@@ -216,19 +212,17 @@ func encFloat32(i *encInstr, state *EncState, p unsafe.Pointer) {
f := float32(*(*float32)(p)); f := float32(*(*float32)(p));
if f != 0 { if f != 0 {
v := floatBits(float64(f)); v := floatBits(float64(f));
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeUint(state, v); EncodeUint(state, v);
state.fieldnum = i.field;
} }
} }
func encFloat64(i *encInstr, state *EncState, p unsafe.Pointer) { func encFloat64(i *encInstr, state *EncState, p unsafe.Pointer) {
f := *(*float64)(p); f := *(*float64)(p);
if f != 0 { if f != 0 {
state.update(i);
v := floatBits(f); v := floatBits(f);
EncodeUint(state, uint64(i.field - state.fieldnum));
EncodeUint(state, v); EncodeUint(state, v);
state.fieldnum = i.field;
} }
} }
...@@ -236,10 +230,9 @@ func encFloat64(i *encInstr, state *EncState, p unsafe.Pointer) { ...@@ -236,10 +230,9 @@ func encFloat64(i *encInstr, state *EncState, p unsafe.Pointer) {
func encUint8Array(i *encInstr, state *EncState, p unsafe.Pointer) { func encUint8Array(i *encInstr, state *EncState, p unsafe.Pointer) {
b := *(*[]byte)(p); b := *(*[]byte)(p);
if len(b) > 0 { if len(b) > 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeUint(state, uint64(len(b))); EncodeUint(state, uint64(len(b)));
state.w.Write(b); state.w.Write(b);
state.fieldnum = i.field;
} }
} }
...@@ -247,10 +240,9 @@ func encUint8Array(i *encInstr, state *EncState, p unsafe.Pointer) { ...@@ -247,10 +240,9 @@ func encUint8Array(i *encInstr, state *EncState, p unsafe.Pointer) {
func encString(i *encInstr, state *EncState, p unsafe.Pointer) { func encString(i *encInstr, state *EncState, p unsafe.Pointer) {
s := *(*string)(p); s := *(*string)(p);
if len(s) > 0 { if len(s) > 0 {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
EncodeUint(state, uint64(len(s))); EncodeUint(state, uint64(len(s)));
io.WriteString(state.w, s); io.WriteString(state.w, s);
state.fieldnum = i.field;
} }
} }
...@@ -267,14 +259,13 @@ type encEngine struct { ...@@ -267,14 +259,13 @@ type encEngine struct {
instr []encInstr instr []encInstr
} }
func (engine *encEngine) encodeStruct(w io.Writer, p uintptr) os.Error { func encodeStruct(engine *encEngine, w io.Writer, basep uintptr) os.Error {
state := new(EncState); state := new(EncState);
state.w = w; state.w = w;
state.base = p;
state.fieldnum = -1; state.fieldnum = -1;
for i := 0; i < len(engine.instr); i++ { for i := 0; i < len(engine.instr); i++ {
instr := &engine.instr[i]; instr := &engine.instr[i];
p := unsafe.Pointer(state.base+instr.offset); p := unsafe.Pointer(basep+instr.offset);
if instr.indir > 0 { if instr.indir > 0 {
if p = encIndirect(p, instr.indir); p == nil { if p = encIndirect(p, instr.indir); p == nil {
state.fieldnum = i; state.fieldnum = i;
...@@ -289,6 +280,18 @@ func (engine *encEngine) encodeStruct(w io.Writer, p uintptr) os.Error { ...@@ -289,6 +280,18 @@ func (engine *encEngine) encodeStruct(w io.Writer, p uintptr) os.Error {
return state.err return state.err
} }
func encodeArray(w io.Writer, p uintptr, op encOp, elemWid int, length int) os.Error {
state := new(EncState);
state.w = w;
state.fieldnum = -1;
EncodeUint(state, uint64(length));
for i := 0; i < length && state.err == nil; i++ {
op(nil, state, unsafe.Pointer(p)); // TODO(r): indir on elements
p += uintptr(elemWid);
}
return state.err
}
var encEngineMap = make(map[reflect.Type] *encEngine) var encEngineMap = make(map[reflect.Type] *encEngine)
var encOpMap = map[int] encOp { var encOpMap = map[int] encOp {
reflect.BoolKind: encBool, reflect.BoolKind: encBool,
...@@ -316,18 +319,35 @@ func encOpFor(typ reflect.Type) encOp { ...@@ -316,18 +319,35 @@ func encOpFor(typ reflect.Type) encOp {
// Special cases // Special cases
if typ.Kind() == reflect.ArrayKind { if typ.Kind() == reflect.ArrayKind {
atyp := typ.(reflect.ArrayType); atyp := typ.(reflect.ArrayType);
switch atyp.Elem().Kind() { switch {
case reflect.Uint8Kind: case atyp.Elem().Kind() == reflect.Uint8Kind:
op = encUint8Array op = encUint8Array
case atyp.IsSlice():
// Slices have a header; we decode it to find the underlying array.
elemOp := encOpFor(atyp.Elem());
op = func(i *encInstr, state *EncState, p unsafe.Pointer) {
slice := *(*reflect.SliceHeader)(p);
if slice.Len == 0 {
return
}
state.update(i);
state.err = encodeArray(state.w, slice.Data, elemOp, atyp.Elem().Size(), int(slice.Len));
};
case !atyp.IsSlice():
// True arrays have size in the type.
elemOp := encOpFor(atyp.Elem());
op = func(i *encInstr, state *EncState, p unsafe.Pointer) {
state.update(i);
state.err = encodeArray(state.w, uintptr(p), elemOp, atyp.Elem().Size(), atyp.Len());
};
} }
} }
if typ.Kind() == reflect.StructKind { if typ.Kind() == reflect.StructKind {
// Generate a closure that calls out to the engine for the nested type. // Generate a closure that calls out to the engine for the nested type.
engine := getEncEngine(typ); engine := getEncEngine(typ);
op = func(i *encInstr, state *EncState, p unsafe.Pointer) { op = func(i *encInstr, state *EncState, p unsafe.Pointer) {
EncodeUint(state, uint64(i.field - state.fieldnum)); state.update(i);
state.err = engine.encodeStruct(state.w, uintptr(p)); state.err = encodeStruct(engine, state.w, uintptr(p));
state.fieldnum = i.field;
}; };
} }
} }
...@@ -394,5 +414,5 @@ func Encode(w io.Writer, e interface{}) os.Error { ...@@ -394,5 +414,5 @@ func Encode(w io.Writer, e interface{}) os.Error {
typeLock.Lock(); typeLock.Lock();
engine := getEncEngine(rt); engine := getEncEngine(rt);
typeLock.Unlock(); typeLock.Unlock();
return engine.encodeStruct(w, uintptr(v.(reflect.StructValue).Addr())); return encodeStruct(engine, w, uintptr(v.(reflect.StructValue).Addr()));
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment