Skip to content

Commit f97a0bb

Browse files
committed
marshal: remove proto field from NativeType struct
Remove the functionally unused proto byte field from NativeType, reducing struct size from 32 to 24 bytes (1 byte + 7 padding). Reduces memory consumption and thus GC. Changes: - Remove proto byte field from NativeType struct - Version() now returns protoVersion4 constant, marked deprecated - NewNativeType/NewCustomType accept but ignore proto param, marked deprecated - Remove Marshal panic guard that checked proto == 0 - Remove proto: from all struct literals across production and test files - Remove protoVer parameter from internal getCassandraType/getCassandraLongType - Remove unused v uint8 parameter from unmarshalTabletHint All unit tests passed, ran Scylla integration tests as well. Signed-off-by: Yaniv Kaul <yaniv.kaul@scylladb.com>
1 parent 68ff48d commit f97a0bb

File tree

8 files changed

+122
-125
lines changed

8 files changed

+122
-125
lines changed

conn.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ func (c *Conn) executeQuery(ctx context.Context, qry *Query) (iter *Iter) {
15831583

15841584
if len(framer.customPayload) > 0 {
15851585
if hint, ok := framer.customPayload["tablets-routing-v1"]; ok {
1586-
tablet, err := unmarshalTabletHint(hint, c.version, qry.routingInfo.keyspace, qry.routingInfo.table)
1586+
tablet, err := unmarshalTabletHint(hint, qry.routingInfo.keyspace, qry.routingInfo.table)
15871587
if err != nil {
15881588
return &Iter{err: err}
15891589
}
@@ -1989,20 +1989,20 @@ func (e *QueryError) Unwrap() error {
19891989
return e.err
19901990
}
19911991

1992-
func unmarshalTabletHint(hint []byte, v uint8, keyspace, table string) (*tablets.TabletInfo, error) {
1992+
func unmarshalTabletHint(hint []byte, keyspace, table string) (*tablets.TabletInfo, error) {
19931993
tabletBuilder := tablets.NewTabletInfoBuilder()
19941994
err := Unmarshal(TupleTypeInfo{
1995-
NativeType: NativeType{proto: v, typ: TypeTuple},
1995+
NativeType: NativeType{typ: TypeTuple},
19961996
Elems: []TypeInfo{
19971997
NativeType{typ: TypeBigInt},
19981998
NativeType{typ: TypeBigInt},
19991999
CollectionType{
2000-
NativeType: NativeType{proto: v, typ: TypeList},
2000+
NativeType: NativeType{typ: TypeList},
20012001
Elem: TupleTypeInfo{
2002-
NativeType: NativeType{proto: v, typ: TypeTuple},
2002+
NativeType: NativeType{typ: TypeTuple},
20032003
Elems: []TypeInfo{
2004-
NativeType{proto: v, typ: TypeUUID},
2005-
NativeType{proto: v, typ: TypeInt},
2004+
NativeType{typ: TypeUUID},
2005+
NativeType{typ: TypeInt},
20062006
}},
20072007
},
20082008
},

frame.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,7 @@ func (f *framer) readTypeInfo() TypeInfo {
720720
id := f.readShort()
721721

722722
simple := NativeType{
723-
proto: f.proto,
724-
typ: Type(id),
723+
typ: Type(id),
725724
}
726725

727726
if simple.typ == TypeCustom {
@@ -781,7 +780,7 @@ func (f *framer) readTypeInfo() TypeInfo {
781780
idx := strings.LastIndex(spec, ",")
782781
typeStr := spec[:idx]
783782
dimStr := spec[idx+1:]
784-
subType := getCassandraLongType(strings.TrimSpace(typeStr), f.proto, nopLogger{})
783+
subType := getCassandraLongType(strings.TrimSpace(typeStr), nopLogger{})
785784
dim, _ := strconv.Atoi(strings.TrimSpace(dimStr))
786785
vector := VectorType{
787786
NativeType: simple,

helpers.go

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ func asVectorType(t TypeInfo) (VectorType, bool) {
6969
if err != nil {
7070
return VectorType{}, false
7171
}
72-
subType := getCassandraLongType(subStr, n.Version(), nopLogger{})
72+
subType := getCassandraLongType(subStr, nopLogger{})
7373
// recurse if subtype itself is still a custom vector
7474
if innerVec, ok := asVectorType(subType); ok {
7575
subType = innerVec
7676
}
7777
return VectorType{
78-
NativeType: NewCustomType(n.Version(), TypeCustom, vectorTypePrefix),
78+
NativeType: NewCustomType(0, TypeCustom, vectorTypePrefix),
7979
SubType: subType,
8080
Dimensions: dim,
8181
}, true
@@ -219,39 +219,39 @@ func getCassandraBaseType(name string) Type {
219219

220220
// TODO: Cover with unit tests.
221221
// Parses long Java-style type definition to internal data structures.
222-
func getCassandraLongType(name string, protoVer byte, logger StdLogger) TypeInfo {
222+
func getCassandraLongType(name string, logger StdLogger) TypeInfo {
223223
const prefix = apacheCassandraTypePrefix
224224
if strings.HasPrefix(name, prefix+"SetType") {
225225
return CollectionType{
226-
NativeType: NewNativeType(protoVer, TypeSet),
227-
Elem: getCassandraLongType(unwrapCompositeTypeDefinition(name, prefix+"SetType", '('), protoVer, logger),
226+
NativeType: NewNativeType(0, TypeSet),
227+
Elem: getCassandraLongType(unwrapCompositeTypeDefinition(name, prefix+"SetType", '('), logger),
228228
}
229229
} else if strings.HasPrefix(name, prefix+"ListType") {
230230
return CollectionType{
231-
NativeType: NewNativeType(protoVer, TypeList),
232-
Elem: getCassandraLongType(unwrapCompositeTypeDefinition(name, prefix+"ListType", '('), protoVer, logger),
231+
NativeType: NewNativeType(0, TypeList),
232+
Elem: getCassandraLongType(unwrapCompositeTypeDefinition(name, prefix+"ListType", '('), logger),
233233
}
234234
} else if strings.HasPrefix(name, prefix+"MapType") {
235235
names := splitJavaCompositeTypes(name, prefix+"MapType")
236236
if len(names) != 2 {
237237
logger.Printf("gocql: error parsing map type, it has %d subelements, expecting 2\n", len(names))
238-
return NewNativeType(protoVer, TypeCustom)
238+
return NewNativeType(0, TypeCustom)
239239
}
240240
return CollectionType{
241-
NativeType: NewNativeType(protoVer, TypeMap),
242-
Key: getCassandraLongType(names[0], protoVer, logger),
243-
Elem: getCassandraLongType(names[1], protoVer, logger),
241+
NativeType: NewNativeType(0, TypeMap),
242+
Key: getCassandraLongType(names[0], logger),
243+
Elem: getCassandraLongType(names[1], logger),
244244
}
245245
} else if strings.HasPrefix(name, prefix+"TupleType") {
246246
names := splitJavaCompositeTypes(name, prefix+"TupleType")
247247
types := make([]TypeInfo, len(names))
248248

249249
for i, name := range names {
250-
types[i] = getCassandraLongType(name, protoVer, logger)
250+
types[i] = getCassandraLongType(name, logger)
251251
}
252252

253253
return TupleTypeInfo{
254-
NativeType: NewNativeType(protoVer, TypeTuple),
254+
NativeType: NewNativeType(0, TypeTuple),
255255
Elems: types,
256256
}
257257
} else if strings.HasPrefix(name, prefix+"UserType") {
@@ -263,94 +263,92 @@ func getCassandraLongType(name string, protoVer byte, logger StdLogger) TypeInfo
263263
fieldName, _ := hex.DecodeString(spec[0])
264264
fields[i-2] = UDTField{
265265
Name: string(fieldName),
266-
Type: getCassandraLongType(spec[1], protoVer, logger),
266+
Type: getCassandraLongType(spec[1], logger),
267267
}
268268
}
269269

270270
udtName, _ := hex.DecodeString(names[1])
271271
return UDTTypeInfo{
272-
NativeType: NewNativeType(protoVer, TypeUDT),
272+
NativeType: NewNativeType(0, TypeUDT),
273273
KeySpace: names[0],
274274
Name: string(udtName),
275275
Elements: fields,
276276
}
277277
} else if strings.HasPrefix(name, prefix+"VectorType") {
278278
names := splitJavaCompositeTypes(name, prefix+"VectorType")
279-
subType := getCassandraLongType(strings.TrimSpace(names[0]), protoVer, logger)
279+
subType := getCassandraLongType(strings.TrimSpace(names[0]), logger)
280280
dim, err := strconv.Atoi(strings.TrimSpace(names[1]))
281281
if err != nil {
282282
logger.Printf("gocql: error parsing vector dimensions: %v\n", err)
283-
return NewNativeType(protoVer, TypeCustom)
283+
return NewNativeType(0, TypeCustom)
284284
}
285285

286286
return VectorType{
287-
NativeType: NewCustomType(protoVer, TypeCustom, prefix+"VectorType"),
287+
NativeType: NewCustomType(0, TypeCustom, prefix+"VectorType"),
288288
SubType: subType,
289289
Dimensions: dim,
290290
}
291291
} else if strings.HasPrefix(name, prefix+"FrozenType") {
292292
names := splitJavaCompositeTypes(name, prefix+"FrozenType")
293-
return getCassandraLongType(strings.TrimSpace(names[0]), protoVer, logger)
293+
return getCassandraLongType(strings.TrimSpace(names[0]), logger)
294294
} else {
295295
// basic type
296296
return NativeType{
297-
proto: protoVer,
298-
typ: getApacheCassandraType(name),
297+
typ: getApacheCassandraType(name),
299298
}
300299
}
301300
}
302301

303302
// Parses short CQL type representation (e.g. map<text, text>) to internal data structures.
304-
func getCassandraType(name string, protoVer byte, logger StdLogger) TypeInfo {
303+
func getCassandraType(name string, logger StdLogger) TypeInfo {
305304
if strings.HasPrefix(name, "frozen<") {
306-
return getCassandraType(unwrapCompositeTypeDefinition(name, "frozen", '<'), protoVer, logger)
305+
return getCassandraType(unwrapCompositeTypeDefinition(name, "frozen", '<'), logger)
307306
} else if strings.HasPrefix(name, "set<") {
308307
return CollectionType{
309-
NativeType: NewNativeType(protoVer, TypeSet),
310-
Elem: getCassandraType(unwrapCompositeTypeDefinition(name, "set", '<'), protoVer, logger),
308+
NativeType: NewNativeType(0, TypeSet),
309+
Elem: getCassandraType(unwrapCompositeTypeDefinition(name, "set", '<'), logger),
311310
}
312311
} else if strings.HasPrefix(name, "list<") {
313312
return CollectionType{
314-
NativeType: NewNativeType(protoVer, TypeList),
315-
Elem: getCassandraType(unwrapCompositeTypeDefinition(name, "list", '<'), protoVer, logger),
313+
NativeType: NewNativeType(0, TypeList),
314+
Elem: getCassandraType(unwrapCompositeTypeDefinition(name, "list", '<'), logger),
316315
}
317316
} else if strings.HasPrefix(name, "map<") {
318317
names := splitCQLCompositeTypes(name, "map")
319318
if len(names) != 2 {
320319
logger.Printf("Error parsing map type, it has %d subelements, expecting 2\n", len(names))
321-
return NewNativeType(protoVer, TypeCustom)
320+
return NewNativeType(0, TypeCustom)
322321
}
323322
return CollectionType{
324-
NativeType: NewNativeType(protoVer, TypeMap),
325-
Key: getCassandraType(names[0], protoVer, logger),
326-
Elem: getCassandraType(names[1], protoVer, logger),
323+
NativeType: NewNativeType(0, TypeMap),
324+
Key: getCassandraType(names[0], logger),
325+
Elem: getCassandraType(names[1], logger),
327326
}
328327
} else if strings.HasPrefix(name, "tuple<") {
329328
names := splitCQLCompositeTypes(name, "tuple")
330329
types := make([]TypeInfo, len(names))
331330

332331
for i, name := range names {
333-
types[i] = getCassandraType(name, protoVer, logger)
332+
types[i] = getCassandraType(name, logger)
334333
}
335334

336335
return TupleTypeInfo{
337-
NativeType: NewNativeType(protoVer, TypeTuple),
336+
NativeType: NewNativeType(0, TypeTuple),
338337
Elems: types,
339338
}
340339
} else if strings.HasPrefix(name, "vector<") {
341340
names := splitCQLCompositeTypes(name, "vector")
342-
subType := getCassandraType(strings.TrimSpace(names[0]), protoVer, logger)
341+
subType := getCassandraType(strings.TrimSpace(names[0]), logger)
343342
dim, _ := strconv.Atoi(strings.TrimSpace(names[1]))
344343

345344
return VectorType{
346-
NativeType: NewCustomType(protoVer, TypeCustom, apacheCassandraTypePrefix+"VectorType"),
345+
NativeType: NewCustomType(0, TypeCustom, apacheCassandraTypePrefix+"VectorType"),
347346
SubType: subType,
348347
Dimensions: dim,
349348
}
350349
} else {
351350
return NativeType{
352-
proto: protoVer,
353-
typ: getCassandraBaseType(name),
351+
typ: getCassandraBaseType(name),
354352
}
355353
}
356354
}

helpers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
func TestGetCassandraType_Set(t *testing.T) {
3636
t.Parallel()
3737

38-
typ := getCassandraType("set<text>", protoVersion4, &defaultLogger{})
38+
typ := getCassandraType("set<text>", &defaultLogger{})
3939
set, ok := typ.(CollectionType)
4040
if !ok {
4141
t.Fatalf("expected CollectionType got %T", typ)
@@ -291,7 +291,7 @@ func TestGetCassandraType(t *testing.T) {
291291

292292
for _, test := range tests {
293293
t.Run(test.input, func(t *testing.T) {
294-
got := getCassandraType(test.input, 0, &defaultLogger{})
294+
got := getCassandraType(test.input, &defaultLogger{})
295295

296296
// TODO(zariel): define an equal method on the types?
297297
if !reflect.DeepEqual(got, test.exp) {

marshal.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ func (d *DirectUnmarshal) UnmarshalCQL(_ TypeInfo, data []byte) error {
160160
// The marshal/unmarshal error provides a list of supported types when an unsupported type is attempted.
161161

162162
func Marshal(info TypeInfo, value interface{}) ([]byte, error) {
163-
if info.Version() < protoVersion1 {
164-
panic("protocol version not set")
165-
}
166-
167163
if valueRef := reflect.ValueOf(value); valueRef.Kind() == reflect.Ptr {
168164
if valueRef.IsNil() {
169165
return nil, nil
@@ -1706,15 +1702,16 @@ type NativeType struct {
17061702
//only used for TypeCustom
17071703
custom string
17081704
typ Type
1709-
proto byte
17101705
}
17111706

1707+
// NewNativeType creates a NativeType. The proto parameter is ignored and kept for API compatibility.
17121708
func NewNativeType(proto byte, typ Type) NativeType {
1713-
return NativeType{proto: proto, typ: typ, custom: ""}
1709+
return NativeType{typ: typ}
17141710
}
17151711

1712+
// NewCustomType creates a custom NativeType. The proto parameter is ignored and kept for API compatibility.
17161713
func NewCustomType(proto byte, typ Type, custom string) NativeType {
1717-
return NativeType{proto: proto, typ: typ, custom: custom}
1714+
return NativeType{typ: typ, custom: custom}
17181715
}
17191716

17201717
func (t NativeType) NewWithError() (interface{}, error) {
@@ -1729,8 +1726,11 @@ func (t NativeType) Type() Type {
17291726
return t.typ
17301727
}
17311728

1729+
// Deprecated: Version always returns protoVersion4. The proto field has been
1730+
// removed from NativeType because marshal/unmarshal logic never branches on it;
1731+
// real protocol-version handling lives in framer.proto and Conn.version.
17321732
func (t NativeType) Version() byte {
1733-
return t.proto
1733+
return protoVersion4
17341734
}
17351735

17361736
func (t NativeType) Custom() string {
@@ -1836,7 +1836,7 @@ type UDTField struct {
18361836

18371837
func NewUDTType(proto byte, name, keySpace string, elems ...UDTField) UDTTypeInfo {
18381838
return UDTTypeInfo{
1839-
NativeType: NativeType{proto: proto, typ: TypeUDT, custom: ""},
1839+
NativeType: NativeType{typ: TypeUDT},
18401840
Name: name,
18411841
KeySpace: keySpace,
18421842
Elements: elems,

0 commit comments

Comments
 (0)