Skip to content

Commit 9e64a3a

Browse files
habermancopybara-github
authored andcommitted
Fasttable: normalized the calling convention
After this CL, fasttable functions always take `upb_DecodeFastNext` as a parameter, instead of having some functions return it. This helps functions compose more easily, since all functions follow the same calling convention. Also simplified and normalized the set of `upb_DecodeFastNext` values. PiperOrigin-RevId: 768667743
1 parent 772aed3 commit 9e64a3a

File tree

3 files changed

+74
-82
lines changed

3 files changed

+74
-82
lines changed

upb/wire/decode_fast/cardinality.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,14 @@ bool upb_DecodeFast_DoNextRepeated(upb_Decoder* d, const char** ptr,
371371

372372
if (!upb_DecodeFast_TagMatches(field->expected_tag, tag, tagsize)) {
373373
// A different tag is encountered; perform regular dispatch.
374-
return UPB_DECODEFAST_EXIT(kUpb_DecodeFastNext_TailCallDispatch, next);
374+
return UPB_DECODEFAST_EXIT(kUpb_DecodeFastNext_Dispatch, next);
375375
}
376376

377377
field->dst = UPB_PTR_AT(field->dst, upb_DecodeFast_ValueBytes(type), char);
378378

379379
if (field->dst == field->end) {
380380
// Out of arena memory; fall back to MiniTable decoder which will realloc.
381-
return UPB_DECODEFAST_EXIT(kUpb_DecodeFastNext_Return, next);
381+
return UPB_DECODEFAST_EXIT(kUpb_DecodeFastNext_FallbackToMiniTable, next);
382382
}
383383

384384
// Parse another instance of the repeated field.
@@ -394,7 +394,7 @@ bool upb_DecodeFast_NextRepeated(upb_Decoder* d, const char** ptr,
394394
upb_DecodeFast_TagSize tagsize) {
395395
if (!upb_DecodeFast_IsRepeated(card)) {
396396
// No repetition is possible; perform regular dispatch.
397-
*next = kUpb_DecodeFastNext_TailCallDispatch;
397+
*next = kUpb_DecodeFastNext_Dispatch;
398398
return false;
399399
}
400400

upb/wire/decode_fast/dispatch.h

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,17 @@ void upb_DecodeFast_SetHasbits(upb_Message* msg, uint64_t hasbits) {
160160
}
161161

162162
typedef enum {
163-
// Call the dispatch function using musttail.
164-
kUpb_DecodeFastNext_TailCallDispatch = 0,
163+
kUpb_DecodeFastNext_Dispatch = 0,
165164

166-
// Return from the function with no tail call. This is used either to signal
167-
// a fallback to the mini table or the end of the message if
168-
// d->message_is_done is true.
169-
kUpb_DecodeFastNext_Return = 1,
165+
// Fallback to the MiniTable decoder. This is used either to signal a fallback
166+
// to the mini table or the end of the message if d->message_is_done is true.
167+
kUpb_DecodeFastNext_FallbackToMiniTable = 1,
170168

169+
// Signal an error.
171170
kUpb_DecodeFastNext_Error = 2,
172171

173-
// Alias for clarity in the code.
174-
kUpb_DecodeFastNext_FallbackToMiniTable = kUpb_DecodeFastNext_Return,
175-
176-
// Tail call to the function to parse the current field.
172+
// Handle the case where ptr >= limit, which is either end-of-message or
173+
// end-of-buffer.
177174
kUpb_DecodeFastNext_MessageIsDoneFallback = 3,
178175

179176
// Tail call to the function to parse the current field, except parse it as
@@ -202,26 +199,25 @@ const char* _upb_FastDecoder_ErrorJmp(upb_Decoder* d, upb_DecodeStatus status) {
202199
return _upb_FastDecoder_ErrorJmp2(d);
203200
}
204201

205-
#define UPB_DECODEFAST_NEXTMAYBEPACKED(next, func_unpacked, func_packed) \
206-
if (UPB_UNLIKELY(next != kUpb_DecodeFastNext_TailCallDispatch)) { \
207-
switch (next) { \
208-
case kUpb_DecodeFastNext_Return: \
209-
UPB_MUSTTAIL return _upb_FastDecoder_DecodeGeneric(UPB_PARSE_ARGS); \
210-
case kUpb_DecodeFastNext_Error: \
211-
UPB_ASSERT(d->status != kUpb_DecodeStatus_Ok); \
212-
return _upb_FastDecoder_ErrorJmp2(d); \
213-
case kUpb_DecodeFastNext_MessageIsDoneFallback: \
214-
UPB_MUSTTAIL return upb_DecodeFast_MessageIsDoneFallback( \
215-
UPB_PARSE_ARGS); \
216-
case kUpb_DecodeFastNext_TailCallPacked: \
217-
UPB_MUSTTAIL return func_packed(UPB_PARSE_ARGS); \
218-
case kUpb_DecodeFastNext_TailCallUnpacked: \
219-
UPB_MUSTTAIL return func_unpacked(UPB_PARSE_ARGS); \
220-
default: \
221-
UPB_UNREACHABLE(); \
222-
} \
223-
} \
224-
UPB_MUSTTAIL return upb_DecodeFast_Dispatch(UPB_PARSE_ARGS);
202+
#define UPB_DECODEFAST_NEXTMAYBEPACKED(next, func_unpacked, func_packed) \
203+
switch (next) { \
204+
case kUpb_DecodeFastNext_Dispatch: \
205+
UPB_MUSTTAIL return upb_DecodeFast_Dispatch(UPB_PARSE_ARGS); \
206+
case kUpb_DecodeFastNext_FallbackToMiniTable: \
207+
UPB_MUSTTAIL return _upb_FastDecoder_DecodeGeneric(UPB_PARSE_ARGS); \
208+
case kUpb_DecodeFastNext_Error: \
209+
UPB_ASSERT(d->status != kUpb_DecodeStatus_Ok); \
210+
return _upb_FastDecoder_ErrorJmp2(d); \
211+
case kUpb_DecodeFastNext_MessageIsDoneFallback: \
212+
UPB_MUSTTAIL return upb_DecodeFast_MessageIsDoneFallback( \
213+
UPB_PARSE_ARGS); \
214+
case kUpb_DecodeFastNext_TailCallPacked: \
215+
UPB_MUSTTAIL return func_packed(UPB_PARSE_ARGS); \
216+
case kUpb_DecodeFastNext_TailCallUnpacked: \
217+
UPB_MUSTTAIL return func_unpacked(UPB_PARSE_ARGS); \
218+
default: \
219+
UPB_UNREACHABLE(); \
220+
}
225221

226222
// Uncomment this to see the exit points from the fast decoder.
227223
// #define UPB_LOG_EXITS

upb/wire/decode_fast/field_fixed.c

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,40 @@
2222
#include "upb/port/def.inc"
2323

2424
UPB_FORCEINLINE
25-
int upb_DecodeFast_UnpackedFixed(upb_Decoder* d, const char** ptr,
26-
upb_Message* msg, intptr_t table,
27-
uint64_t* hasbits, uint64_t* data,
28-
upb_DecodeFast_Type type,
29-
upb_DecodeFast_Cardinality card,
30-
upb_DecodeFast_TagSize tagsize) {
31-
upb_DecodeFastNext ret;
25+
void upb_DecodeFast_UnpackedFixed(upb_Decoder* d, const char** ptr,
26+
upb_Message* msg, intptr_t table,
27+
uint64_t* hasbits, uint64_t* data,
28+
upb_DecodeFastNext* ret,
29+
upb_DecodeFast_Type type,
30+
upb_DecodeFast_Cardinality card,
31+
upb_DecodeFast_TagSize tagsize) {
3232
upb_DecodeFastField field;
3333
int valbytes = upb_DecodeFast_ValueBytes(type);
3434

35-
if (!upb_DecodeFast_CheckPackableTag(type, card, tagsize, data,
36-
kUpb_DecodeFastNext_TailCallPacked,
37-
&ret) ||
38-
!Upb_DecodeFast_GetField(d, *ptr, msg, *data, hasbits, &ret, &field, type,
35+
if (!upb_DecodeFast_CheckPackableTag(
36+
type, card, tagsize, data, kUpb_DecodeFastNext_TailCallPacked, ret) ||
37+
!Upb_DecodeFast_GetField(d, *ptr, msg, *data, hasbits, ret, &field, type,
3938
card)) {
40-
return ret;
39+
return;
4140
}
4241

4342
do {
4443
*ptr += upb_DecodeFast_TagSizeBytes(tagsize);
4544
memcpy(field.dst, *ptr, valbytes);
4645
*ptr += valbytes;
4746
_upb_Decoder_Trace(d, 'F');
48-
} while (upb_DecodeFast_NextRepeated(d, ptr, *data, &ret, &field, type, card,
47+
} while (upb_DecodeFast_NextRepeated(d, ptr, *data, ret, &field, type, card,
4948
tagsize));
50-
51-
return kUpb_DecodeFastNext_TailCallDispatch;
5249
}
5350

5451
UPB_FORCEINLINE
55-
int upb_DecodeFast_PackedFixed(upb_Decoder* d, const char** ptr,
56-
upb_Message* msg, intptr_t table,
57-
uint64_t* hasbits, uint64_t* data,
58-
upb_DecodeFast_Type type,
59-
upb_DecodeFast_Cardinality card,
60-
upb_DecodeFast_TagSize tagsize) {
61-
upb_DecodeFastNext ret;
52+
void upb_DecodeFast_PackedFixed(upb_Decoder* d, const char** ptr,
53+
upb_Message* msg, intptr_t table,
54+
uint64_t* hasbits, uint64_t* data,
55+
upb_DecodeFastNext* ret,
56+
upb_DecodeFast_Type type,
57+
upb_DecodeFast_Cardinality card,
58+
upb_DecodeFast_TagSize tagsize) {
6259
int size;
6360
upb_DecodeFastField field;
6461
uint8_t valbytes = upb_DecodeFast_ValueBytes(type);
@@ -67,21 +64,21 @@ int upb_DecodeFast_PackedFixed(upb_Decoder* d, const char** ptr,
6764

6865
if (!upb_DecodeFast_CheckPackableTag(type, card, tagsize, data,
6966
kUpb_DecodeFastNext_TailCallUnpacked,
70-
&ret) ||
67+
ret) ||
7168
!upb_DecodeFast_DecodeShortSizeForImmediateRead(d, &data_ptr, &size,
72-
&ret)) {
73-
return ret;
69+
ret)) {
70+
return;
7471
}
7572

7673
if (size != 0) {
7774
if (size % valbytes != 0) {
78-
UPB_DECODEFAST_ERROR(d, kUpb_DecodeStatus_Malformed, &ret);
79-
return ret;
75+
UPB_DECODEFAST_ERROR(d, kUpb_DecodeStatus_Malformed, ret);
76+
return;
8077
}
8178

8279
if (!upb_DecodeFast_GetArrayForAppend(d, *ptr, msg, *data, hasbits, &field,
83-
type, size / valbytes, &ret)) {
84-
return ret;
80+
type, size / valbytes, ret)) {
81+
return;
8582
}
8683

8784
upb_DecodeFast_InlineMemcpy(field.dst, data_ptr, size);
@@ -90,37 +87,36 @@ int upb_DecodeFast_PackedFixed(upb_Decoder* d, const char** ptr,
9087

9188
*ptr = data_ptr + size;
9289
_upb_Decoder_Trace(d, 'F');
93-
94-
return kUpb_DecodeFastNext_TailCallDispatch;
9590
}
9691

9792
UPB_FORCEINLINE
98-
int upb_DecodeFast_Fixed(upb_Decoder* d, const char** ptr, upb_Message* msg,
99-
intptr_t table, uint64_t* hasbits, uint64_t* data,
100-
upb_DecodeFast_Type type,
101-
upb_DecodeFast_Cardinality card,
102-
upb_DecodeFast_TagSize tagsize) {
93+
void upb_DecodeFast_Fixed(upb_Decoder* d, const char** ptr, upb_Message* msg,
94+
intptr_t table, uint64_t* hasbits, uint64_t* data,
95+
upb_DecodeFastNext* ret, upb_DecodeFast_Type type,
96+
upb_DecodeFast_Cardinality card,
97+
upb_DecodeFast_TagSize tagsize) {
10398
if (card == kUpb_DecodeFast_Packed) {
104-
return upb_DecodeFast_PackedFixed(d, ptr, msg, table, hasbits, data, type,
105-
card, tagsize);
99+
upb_DecodeFast_PackedFixed(d, ptr, msg, table, hasbits, data, ret, type,
100+
card, tagsize);
106101
} else {
107-
return upb_DecodeFast_UnpackedFixed(d, ptr, msg, table, hasbits, data, type,
108-
card, tagsize);
102+
upb_DecodeFast_UnpackedFixed(d, ptr, msg, table, hasbits, data, ret, type,
103+
card, tagsize);
109104
}
110105
}
111106

112107
/* Generate all combinations:
113108
* {s,o,r,p} x {f4,f8} x {1bt,2bt} */
114109

115-
#define F(type, card, tagbytes) \
116-
UPB_NOINLINE UPB_PRESERVE_NONE const char* UPB_DECODEFAST_FUNCNAME( \
117-
type, card, tagbytes)(UPB_PARSE_PARAMS) { \
118-
int next = upb_DecodeFast_Fixed( \
119-
d, &ptr, msg, table, &hasbits, &data, kUpb_DecodeFast_##type, \
120-
kUpb_DecodeFast_##card, kUpb_DecodeFast_##tagbytes); \
121-
UPB_DECODEFAST_NEXTMAYBEPACKED( \
122-
next, UPB_DECODEFAST_FUNCNAME(type, Repeated, tagbytes), \
123-
UPB_DECODEFAST_FUNCNAME(type, Packed, tagbytes)); \
110+
#define F(type, card, tagbytes) \
111+
UPB_NOINLINE UPB_PRESERVE_NONE const char* UPB_DECODEFAST_FUNCNAME( \
112+
type, card, tagbytes)(UPB_PARSE_PARAMS) { \
113+
upb_DecodeFastNext next = kUpb_DecodeFastNext_Dispatch; \
114+
upb_DecodeFast_Fixed(d, &ptr, msg, table, &hasbits, &data, &next, \
115+
kUpb_DecodeFast_##type, kUpb_DecodeFast_##card, \
116+
kUpb_DecodeFast_##tagbytes); \
117+
UPB_DECODEFAST_NEXTMAYBEPACKED( \
118+
next, UPB_DECODEFAST_FUNCNAME(type, Repeated, tagbytes), \
119+
UPB_DECODEFAST_FUNCNAME(type, Packed, tagbytes)); \
124120
}
125121

126122
UPB_DECODEFAST_CARDINALITIES(UPB_DECODEFAST_TAGSIZES, F, Fixed32)

0 commit comments

Comments
 (0)