Browse Source

codec: fastpath is only for map/slice types. Do that check in fastEnc/fastDec funcs.

Ugorji Nwoke 11 years ago
parent
commit
16e449d52b
2 changed files with 10 additions and 8 deletions
  1. 5 4
      codec/decode.go
  2. 5 4
      codec/encode.go

+ 5 - 4
codec/decode.go

@@ -722,6 +722,7 @@ func (d *Decoder) getDecFn(rt reflect.Type) (fn decFn) {
 		// debugf("\tCreating new dec fn for type: %v\n", rt)
 		// debugf("\tCreating new dec fn for type: %v\n", rt)
 		fi := decFnInfo{ti: getTypeInfo(rtid, rt), d: d, dd: d.d}
 		fi := decFnInfo{ti: getTypeInfo(rtid, rt), d: d, dd: d.d}
 		fn.i = &fi
 		fn.i = &fi
+		rk := rt.Kind()
 		// An extension can be registered for any type, regardless of the Kind
 		// An extension can be registered for any type, regardless of the Kind
 		// (e.g. type BitSet int64, type MyStruct { / * unexported fields * / }, type X []int, etc.
 		// (e.g. type BitSet int64, type MyStruct { / * unexported fields * / }, type X []int, etc.
 		//
 		//
@@ -740,12 +741,12 @@ func (d *Decoder) getDecFn(rt reflect.Type) (fn decFn) {
 			fn.f = (*decFnInfo).ext
 			fn.f = (*decFnInfo).ext
 		} else if supportBinaryMarshal && fi.ti.unm {
 		} else if supportBinaryMarshal && fi.ti.unm {
 			fn.f = (*decFnInfo).binaryMarshal
 			fn.f = (*decFnInfo).binaryMarshal
-		} else if xxf := fastDec(rtid); xxf != nil {
+		} else if xxf := fastDec(rtid, rk); xxf != nil {
 			fn.f = xxf
 			fn.f = xxf
 			// } else if xxf, xxok := fastpathsDec[rtid]; xxok {
 			// } else if xxf, xxok := fastpathsDec[rtid]; xxok {
 			// 	fn.f = xxf
 			// 	fn.f = xxf
 		} else {
 		} else {
-			switch rk := rt.Kind(); rk {
+			switch rk {
 			case reflect.String:
 			case reflect.String:
 				fn.f = (*decFnInfo).kString
 				fn.f = (*decFnInfo).kString
 			case reflect.Bool:
 			case reflect.Bool:
@@ -861,9 +862,9 @@ func decErr(format string, params ...interface{}) {
 	doPanic(msgTagDec, format, params...)
 	doPanic(msgTagDec, format, params...)
 }
 }
 
 
-func fastDec(rtid uintptr) func(*decFnInfo, reflect.Value) {
+func fastDec(rtid uintptr, rk reflect.Kind) func(*decFnInfo, reflect.Value) {
 	// Unfortunately, accessing an empty map is not free free.
 	// Unfortunately, accessing an empty map is not free free.
-	if fastpathEnabled {
+	if fastpathEnabled && (rk == reflect.Map || rk == reflect.Slice) {
 		return fastpathsDec[rtid]
 		return fastpathsDec[rtid]
 	}
 	}
 	return nil
 	return nil

+ 5 - 4
codec/encode.go

@@ -806,6 +806,7 @@ func (e *Encoder) getEncFn(rt reflect.Type) (fn encFn) {
 		// debugf("\tCreating new enc fn for type: %v\n", rt)
 		// debugf("\tCreating new enc fn for type: %v\n", rt)
 		fi := encFnInfo{ti: getTypeInfo(rtid, rt), e: e, ee: e.e}
 		fi := encFnInfo{ti: getTypeInfo(rtid, rt), e: e, ee: e.e}
 		fn.i = &fi
 		fn.i = &fi
+		rk := rt.Kind()
 		if rtid == rawExtTypId {
 		if rtid == rawExtTypId {
 			fn.f = (*encFnInfo).rawExt
 			fn.f = (*encFnInfo).rawExt
 		} else if e.e.isBuiltinType(rtid) {
 		} else if e.e.isBuiltinType(rtid) {
@@ -815,12 +816,12 @@ func (e *Encoder) getEncFn(rt reflect.Type) (fn encFn) {
 			fn.f = (*encFnInfo).ext
 			fn.f = (*encFnInfo).ext
 		} else if supportBinaryMarshal && fi.ti.m {
 		} else if supportBinaryMarshal && fi.ti.m {
 			fn.f = (*encFnInfo).binaryMarshal
 			fn.f = (*encFnInfo).binaryMarshal
-		} else if xxf := fastEnc(rtid); xxf != nil {
+		} else if xxf := fastEnc(rtid, rk); xxf != nil {
 			fn.f = xxf
 			fn.f = xxf
 			// } else if xxf, xxok := fastpathsEnc[rtid]; xxok {
 			// } else if xxf, xxok := fastpathsEnc[rtid]; xxok {
 			// 	fn.f = xxf
 			// 	fn.f = xxf
 		} else {
 		} else {
-			switch rk := rt.Kind(); rk {
+			switch rk {
 			case reflect.Bool:
 			case reflect.Bool:
 				fn.f = (*encFnInfo).kBool
 				fn.f = (*encFnInfo).kBool
 			case reflect.String:
 			case reflect.String:
@@ -883,9 +884,9 @@ func encErr(format string, params ...interface{}) {
 	doPanic(msgTagEnc, format, params...)
 	doPanic(msgTagEnc, format, params...)
 }
 }
 
 
-func fastEnc(rtid uintptr) func(*encFnInfo, reflect.Value) {
+func fastEnc(rtid uintptr, rk reflect.Kind) func(*encFnInfo, reflect.Value) {
 	// Unfortunately, accessing an empty map is not free free.
 	// Unfortunately, accessing an empty map is not free free.
-	if fastpathEnabled {
+	if fastpathEnabled && (rk == reflect.Map || rk == reflect.Slice) {
 		return fastpathsEnc[rtid]
 		return fastpathsEnc[rtid]
 	}
 	}
 	return nil
 	return nil