Skip to content

Commit 14384f5

Browse files
committed
Try harder to not reload the mask
1 parent 00a4c53 commit 14384f5

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/lib/lwan-websocket.c

+20-11
Original file line numberDiff line numberDiff line change
@@ -182,33 +182,42 @@ static void unmask(char *msg, size_t msg_len, char mask[static 4])
182182
}
183183
#endif
184184

185-
if (sizeof(void *) == 8 && msg_len >= 8) {
185+
#if __SIZEOF_POINTER__ == 8
186+
186187
#if defined(__SSE_4_1__)
187-
/* We're far away enough from the AVX2 path that it's
188-
* probably better to use mask128 instead of mask256
189-
* here. */
190-
const __int64 mask64 = _mm_extract_epi64(mask128, 0);
188+
/* We're far away enough from the AVX2 path that it's
189+
* probably better to use mask128 instead of mask256
190+
* here. */
191+
const uint64_t mask64 = _mm_extract_epi64(mask128, 0);
191192
#else
192-
const uint32_t mask32 = string_as_uint32(mask);
193-
const uint64_t mask64 = (uint64_t)mask32 << 32 | (uint64_t)mask32;
193+
const uint32_t mask32 = string_as_uint32(mask);
194+
const uint64_t mask64 = (uint64_t)mask32 << 32 | (uint64_t)mask32;
195+
#define HAS_MASK32
194196
#endif
197+
198+
if (msg_len >= 8) {
195199
do {
196200
uint64_t v = string_as_uint64(msg);
197-
v ^= (uint64_t)mask64;
201+
v ^= mask64;
198202
msg = mempcpy(msg, &v, sizeof(v));
199203
msg_len -= 8;
200204
} while (msg_len >= 8);
201205
}
206+
#endif
202207

203208
if (msg_len >= 4) {
204-
#if defined(__SSE_4_1__)
209+
#if defined(HAS_MASK32)
210+
/* do nothing */
211+
#elif __SIZEOF_POINTER__ == 8
212+
const uint32_t mask32 = (uint32_t)mask64;
213+
#elif defined(__SSE_4_1__)
205214
const uint32_t mask32 = _mm_extract_epi32(mask128, 0);
206215
#else
207216
const uint32_t mask32 = string_as_uint32(mask);
208217
#endif
209218
do {
210219
uint32_t v = string_as_uint32(msg);
211-
v ^= (uint32_t)mask32;
220+
v ^= mask32;
212221
msg = mempcpy(msg, &v, sizeof(v));
213222
msg_len -= 4;
214223
} while (msg_len >= 4);
@@ -225,7 +234,7 @@ static void unmask(char *msg, size_t msg_len, char mask[static 4])
225234
default:
226235
__builtin_unreachable();
227236
}
228-
#undef MASK32_SET
237+
#undef HAS_MASK32
229238
}
230239

231240
static void send_websocket_pong(struct lwan_request *request, uint16_t header)

0 commit comments

Comments
 (0)