Просмотр исходного кода

codec: remove decDriver.DecodeString() - redundant, and replaced by DecodeStringAsBytes()

Ugorji Nwoke 6 лет назад
Родитель
Сommit
372321e378
8 измененных файлов с 42 добавлено и 81 удалено
  1. 9 45
      codec/binc.go
  2. 2 6
      codec/cbor.go
  3. 4 3
      codec/gen.go
  4. 1 1
      codec/helper_not_unsafe.go
  5. 1 1
      codec/helper_unsafe.go
  6. 23 15
      codec/json.go
  7. 1 5
      codec/msgpack.go
  8. 1 5
      codec/simple.go

+ 9 - 45
codec/binc.go

@@ -399,7 +399,7 @@ type bincDecDriver struct {
 	// _      [3]byte // padding
 	// linear searching on this slice is ok,
 	// because we typically expect < 32 symbols in each stream.
-	s map[uint16]strBytes // []bincDecSymbol
+	s map[uint16][]byte // []bincDecSymbol
 
 	// noStreamingCodec
 	// decNoSeparator
@@ -682,8 +682,7 @@ func (d *bincDecDriver) decLenNumber() (v uint64) {
 	return
 }
 
-func (d *bincDecDriver) decStringAndBytes(bs []byte, withString, zerocopy bool) (
-	bs2 []byte, s string) {
+func (d *bincDecDriver) decStringBytes(bs []byte, zerocopy bool) (bs2 []byte) {
 	if !d.bdRead {
 		d.readNextBd()
 	}
@@ -707,9 +706,6 @@ func (d *bincDecDriver) decStringAndBytes(bs []byte, withString, zerocopy bool)
 		} else {
 			bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, bs)
 		}
-		if withString {
-			s = string(bs2)
-		}
 	case bincVdSymbol:
 		// zerocopy doesn't apply for symbols,
 		// as the values must be stored in a table for later use.
@@ -726,31 +722,11 @@ func (d *bincDecDriver) decStringAndBytes(bs []byte, withString, zerocopy bool)
 			symbol = uint16(bigen.Uint16(d.r.readx(2)))
 		}
 		if d.s == nil {
-			d.s = pool.mapU16StrBytes.Get().(map[uint16]strBytes) // make([]bincDecSymbol, 0, 16)
+			d.s = pool.mapU16Bytes.Get().(map[uint16][]byte) // make([]bincDecSymbol, 0, 16)
 		}
 
 		if vs&0x4 == 0 {
-			ss := d.s[symbol]
-			bs2 = ss.b
-			if withString {
-				if ss.s == "" && len(ss.b) > 0 {
-					ss.s = string(ss.b)
-				}
-				s = ss.s
-			}
-			// for i := range d.s {
-			// 	j := &d.s[i]
-			// 	if j.i == symbol {
-			// 		bs2 = j.b
-			// 		if withString {
-			// 			if j.s == "" && bs2 != nil {
-			// 				j.s = string(bs2)
-			// 			}
-			// 			s = j.s
-			// 		}
-			// 		break
-			// 	}
-			// }
+			bs2 = d.s[symbol]
 		} else {
 			switch vs & 0x3 {
 			case 0:
@@ -766,10 +742,7 @@ func (d *bincDecDriver) decStringAndBytes(bs []byte, withString, zerocopy bool)
 			// the parameter bs in the map, as it might be a shared buffer.
 			// bs2 = decByteSlice(d.r, slen, bs)
 			bs2 = decByteSlice(d.r, slen, d.d.h.MaxInitLen, nil)
-			if withString {
-				s = string(bs2)
-			}
-			d.s[symbol] = strBytes{s: s, b: bs2}
+			d.s[symbol] = bs2
 			// d.s = append(d.s, bincDecSymbol{i: symbol, s: s, b: bs2})
 		}
 	default:
@@ -780,17 +753,8 @@ func (d *bincDecDriver) decStringAndBytes(bs []byte, withString, zerocopy bool)
 	return
 }
 
-func (d *bincDecDriver) DecodeString() (s string) {
-	// DecodeBytes does not accommodate symbols, whose impl stores string version in map.
-	// Use decStringAndBytes directly.
-	// return string(d.DecodeBytes(d.b[:], true, true))
-	_, s = d.decStringAndBytes(d.b[:], true, true)
-	return
-}
-
 func (d *bincDecDriver) DecodeStringAsBytes() (s []byte) {
-	s, _ = d.decStringAndBytes(d.b[:], false, true)
-	return
+	return d.decStringBytes(d.b[:], true)
 }
 
 func (d *bincDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
@@ -934,10 +898,10 @@ func (d *bincDecDriver) DecodeNaked() {
 		n.f = d.decFloat()
 	case bincVdSymbol:
 		n.v = valueTypeSymbol
-		n.s = d.DecodeString()
+		n.s = string(d.DecodeStringAsBytes())
 	case bincVdString:
 		n.v = valueTypeString
-		n.s = d.DecodeString()
+		n.s = string(d.DecodeStringAsBytes())
 	case bincVdByteArray:
 		decNakedReadRawBytes(d, d.d, n, d.h.RawToString)
 	case bincVdTimestamp:
@@ -1064,7 +1028,7 @@ func (d *bincDecDriver) atEndOfDecode() {
 		for k := range d.s {
 			delete(d.s, k)
 		}
-		pool.mapU16StrBytes.Put(d.s)
+		pool.mapU16Bytes.Put(d.s)
 		d.s = nil
 	}
 }

+ 2 - 6
codec/cbor.go

@@ -649,10 +649,6 @@ func (d *cborDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
 	return decByteSlice(d.r, clen, d.h.MaxInitLen, bs)
 }
 
-func (d *cborDecDriver) DecodeString() (s string) {
-	return string(d.DecodeBytes(d.d.b[:], true))
-}
-
 func (d *cborDecDriver) DecodeStringAsBytes() (s []byte) {
 	return d.DecodeBytes(d.d.b[:], true)
 }
@@ -761,7 +757,7 @@ func (d *cborDecDriver) DecodeNaked() {
 		decNakedReadRawBytes(d, d.d, n, d.h.RawToString)
 	case cborMajorString:
 		n.v = valueTypeString
-		n.s = d.DecodeString()
+		n.s = string(d.DecodeStringAsBytes())
 	case cborMajorArray:
 		n.v = valueTypeArray
 		decodeFurther = true
@@ -802,7 +798,7 @@ func (d *cborDecDriver) DecodeNaked() {
 			decNakedReadRawBytes(d, d.d, n, d.h.RawToString)
 		case cborBdIndefiniteString:
 			n.v = valueTypeString
-			n.s = d.DecodeString()
+			n.s = string(d.DecodeStringAsBytes())
 		case cborBdIndefiniteArray:
 			n.v = valueTypeArray
 			decodeFurther = true

+ 4 - 3
codec/gen.go

@@ -108,7 +108,8 @@ import (
 // v10: modified encDriver and decDriver interfaces.
 // v11: remove deprecated methods of encDriver and decDriver.
 // v12: removed deprecated methods from genHelper and changed container tracking logic
-const genVersion = 12
+// v13: 20190603 removed DecodeString - use DecodeStringAsBytes instead
+const genVersion = 13
 
 const (
 	genCodecPkg        = "codec1978"
@@ -1557,7 +1558,7 @@ func (x *genRunner) decTryAssignPrimitive(varname string, t reflect.Type, isptr
 	case reflect.Bool:
 		x.linef("%s%s = (%s)(r.DecodeBool())", ptr, varname, x.genTypeName(t))
 	case reflect.String:
-		x.linef("%s%s = (%s)(r.DecodeString())", ptr, varname, x.genTypeName(t))
+		x.linef("%s%s = (%s)(string(r.DecodeStringAsBytes()))", ptr, varname, x.genTypeName(t))
 	default:
 		return false
 	}
@@ -2086,7 +2087,7 @@ func genInternalDecCommandAsString(s string) string {
 		return "d.d.DecodeInt64()"
 
 	case "string":
-		return "d.d.DecodeString()"
+		return "string(d.d.DecodeStringAsBytes())"
 	case "[]byte", "[]uint8", "bytes":
 		return "d.d.DecodeBytes(nil, false)"
 	case "float32":

+ 1 - 1
codec/helper_not_unsafe.go

@@ -170,7 +170,7 @@ func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
 }
 
 func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
-	rv.SetString(d.d.DecodeString())
+	rv.SetString(string(d.d.DecodeStringAsBytes()))
 }
 
 func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {

+ 1 - 1
codec/helper_unsafe.go

@@ -312,7 +312,7 @@ func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
 
 func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
 	urv := (*unsafeReflectValue)(unsafe.Pointer(&rv))
-	*(*string)(urv.ptr) = d.d.DecodeString()
+	*(*string)(urv.ptr) = string(d.d.DecodeStringAsBytes())
 }
 
 func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {

+ 23 - 15
codec/json.go

@@ -46,11 +46,20 @@ const (
 	jsonLitTrue   = 1
 	jsonLitFalseQ = 6
 	jsonLitFalse  = 7
-	// jsonLitNullQ  = 13
-	jsonLitNull = 14
+	jsonLitNullQ  = 13
+	jsonLitNull   = 14
 )
 
 var (
+	jsonLiteralTrueQ  = jsonLiterals[jsonLitTrueQ : jsonLitTrueQ+6]
+	jsonLiteralFalseQ = jsonLiterals[jsonLitFalseQ : jsonLitFalseQ+7]
+	jsonLiteralNullQ  = jsonLiterals[jsonLitNullQ : jsonLitNullQ+6]
+
+	jsonLiteralTrue  = jsonLiterals[jsonLitTrue : jsonLitTrue+4]
+	jsonLiteralFalse = jsonLiterals[jsonLitFalse : jsonLitFalse+5]
+	jsonLiteralNull  = jsonLiterals[jsonLitNull : jsonLitNull+4]
+
+	// these are used, after consuming the first char
 	jsonLiteral4True  = jsonLiterals[jsonLitTrue+1 : jsonLitTrue+4]
 	jsonLiteral4False = jsonLiterals[jsonLitFalse+1 : jsonLitFalse+5]
 	jsonLiteral4Null  = jsonLiterals[jsonLitNull+1 : jsonLitNull+4]
@@ -259,15 +268,15 @@ func (e *jsonEncDriverGeneric) WriteMapEnd() {
 func (e *jsonEncDriverGeneric) EncodeBool(b bool) {
 	if e.ks && e.e.c == containerMapKey {
 		if b {
-			e.w.writeb(jsonLiterals[jsonLitTrueQ : jsonLitTrueQ+6])
+			e.w.writeb(jsonLiteralTrueQ)
 		} else {
-			e.w.writeb(jsonLiterals[jsonLitFalseQ : jsonLitFalseQ+7])
+			e.w.writeb(jsonLiteralFalseQ)
 		}
 	} else {
 		if b {
-			e.w.writeb(jsonLiterals[jsonLitTrue : jsonLitTrue+4])
+			e.w.writeb(jsonLiteralTrue)
 		} else {
-			e.w.writeb(jsonLiterals[jsonLitFalse : jsonLitFalse+5])
+			e.w.writeb(jsonLiteralFalse)
 		}
 	}
 }
@@ -1022,10 +1031,10 @@ func (d *jsonDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
 	return
 }
 
-func (d *jsonDecDriver) DecodeString() (s string) {
-	d.appendStringAsBytes()
-	return d.bsToString()
-}
+// func (d *jsonDecDriver) DecodeString() (s string) {
+// 	d.appendStringAsBytes()
+// 	return d.bsToString()
+// }
 
 func (d *jsonDecDriver) DecodeStringAsBytes() (s []byte) {
 	d.appendStringAsBytes()
@@ -1259,16 +1268,15 @@ func (d *jsonDecDriver) DecodeNaked() {
 		// if a string, and MapKeyAsString, then try to decode it as a nil, bool or number first
 		d.appendStringAsBytes()
 		if len(d.bs) > 0 && d.d.c == containerMapKey && d.h.MapKeyAsString {
-			switch stringView(d.bs) {
-			case "null":
+			if bytes.Equal(d.bs, jsonLiteralNull) {
 				z.v = valueTypeNil
-			case "true":
+			} else if bytes.Equal(d.bs, jsonLiteralTrue) {
 				z.v = valueTypeBool
 				z.b = true
-			case "false":
+			} else if bytes.Equal(d.bs, jsonLiteralFalse) {
 				z.v = valueTypeBool
 				z.b = false
-			default:
+			} else {
 				// check if a number: float, int or uint
 				if err := d.nakedNum(z, d.bs); err != nil {
 					z.v = valueTypeString

+ 1 - 5
codec/msgpack.go

@@ -509,7 +509,7 @@ func (d *msgpackDecDriver) DecodeNaked() {
 		case bd == mpStr8, bd == mpStr16, bd == mpStr32, bd >= mpFixStrMin && bd <= mpFixStrMax:
 			if d.h.WriteExt || d.h.RawToString {
 				n.v = valueTypeString
-				n.s = d.DecodeString()
+				n.s = string(d.DecodeStringAsBytes())
 			} else {
 				n.v = valueTypeBytes
 				n.l = d.DecodeBytes(nil, false)
@@ -719,10 +719,6 @@ func (d *msgpackDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte)
 	return decByteSlice(d.r, clen, d.h.MaxInitLen, bs)
 }
 
-func (d *msgpackDecDriver) DecodeString() (s string) {
-	return string(d.DecodeBytes(d.d.b[:], true))
-}
-
 func (d *msgpackDecDriver) DecodeStringAsBytes() (s []byte) {
 	return d.DecodeBytes(d.d.b[:], true)
 }

+ 1 - 5
codec/simple.go

@@ -408,10 +408,6 @@ func (d *simpleDecDriver) decLen() int {
 	return -1
 }
 
-func (d *simpleDecDriver) DecodeString() (s string) {
-	return string(d.DecodeBytes(d.d.b[:], true))
-}
-
 func (d *simpleDecDriver) DecodeStringAsBytes() (s []byte) {
 	return d.DecodeBytes(d.d.b[:], true)
 }
@@ -558,7 +554,7 @@ func (d *simpleDecDriver) DecodeNaked() {
 	case simpleVdString, simpleVdString + 1,
 		simpleVdString + 2, simpleVdString + 3, simpleVdString + 4:
 		n.v = valueTypeString
-		n.s = d.DecodeString()
+		n.s = string(d.DecodeStringAsBytes())
 	case simpleVdByteArray, simpleVdByteArray + 1,
 		simpleVdByteArray + 2, simpleVdByteArray + 3, simpleVdByteArray + 4:
 		decNakedReadRawBytes(d, d.d, n, d.h.RawToString)