mh: Optimize for speed.
* mh/burst.c (transtab): Revamp as a proper translation table. It is indexed by the current state offset by 1 and the last read caracter. (token_num): Remove. (finish_stream, flush_stream): Inline functions. (burst_digest): Change transtab indexing.
Showing
2 changed files
with
271 additions
and
28 deletions
... | @@ -192,31 +192,169 @@ burst_mime (mu_message_t msg) | ... | @@ -192,31 +192,169 @@ burst_mime (mu_message_t msg) |
192 | #define S5 5 | 192 | #define S5 5 |
193 | 193 | ||
194 | /* Negative state means no write */ | 194 | /* Negative state means no write */ |
195 | int transtab[][4] = { | 195 | int transtab[5][256] = { |
196 | /* DEF '\n' ' ' '-' */ | 196 | /* S1 */ { S2, S2, S2, S2, S2, S2, S2, S2, |
197 | /* S1 */ { S2, S1, S2, -S3 }, | 197 | S2, S2, S1, S2, S2, S2, S2, S2, |
198 | /* S2 */ { S2, S1, S2, S2 }, | 198 | S2, S2, S2, S2, S2, S2, S2, S2, |
199 | /* S3 */ { -S4, -S4, -S2, -S4 }, | 199 | S2, S2, S2, S2, S2, S2, S2, S2, |
200 | /* S4 */ { -S4, -S5, -S4, -S4 }, | 200 | S2, S2, S2, S2, S2, S2, S2, S2, |
201 | /* S5 */ { S2, -S5, S2, S2 } | 201 | S2, S2, S2, S2, S2, -S3, S2, S2, |
202 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
203 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
204 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
205 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
206 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
207 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
208 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
209 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
210 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
211 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
212 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
213 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
214 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
215 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
216 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
217 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
218 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
219 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
220 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
221 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
222 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
223 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
224 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
225 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
226 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
227 | S2, S2, S2, S2, S2, S2, S2, S2 }, | ||
228 | /* S2 */ { S2, S2, S2, S2, S2, S2, S2, S2, | ||
229 | S2, S2, S1, S2, S2, S2, S2, S2, | ||
230 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
231 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
232 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
233 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
234 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
235 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
236 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
237 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
238 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
239 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
240 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
241 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
242 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
243 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
244 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
245 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
246 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
247 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
248 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
249 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
250 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
251 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
252 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
253 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
254 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
255 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
256 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
257 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
258 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
259 | S2, S2, S2, S2, S2, S2, S2, S2 }, | ||
260 | /* S3 */ { -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
261 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
262 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
263 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
264 | -S2, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
265 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
266 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
267 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
268 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
269 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
270 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
271 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
272 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
273 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
274 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
275 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
276 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
277 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
278 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
279 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
280 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
281 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
282 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
283 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
284 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
285 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
286 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
287 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
288 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
289 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
290 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
291 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4 }, | ||
292 | /* S4 */ { -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
293 | -S4, -S4, -S5, -S4, -S4, -S4, -S4, -S4, | ||
294 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
295 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
296 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
297 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
298 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
299 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
300 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
301 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
302 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
303 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
304 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
305 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
306 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
307 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
308 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
309 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
310 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
311 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
312 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
313 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
314 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
315 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
316 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
317 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
318 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
319 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
320 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
321 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
322 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4, | ||
323 | -S4, -S4, -S4, -S4, -S4, -S4, -S4, -S4 }, | ||
324 | /* S5 */ { S2, S2, S2, S2, S2, S2, S2, S2, | ||
325 | S2, S2, -S5, S2, S2, S2, S2, S2, | ||
326 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
327 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
328 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
329 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
330 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
331 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
332 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
333 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
334 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
335 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
336 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
337 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
338 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
339 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
340 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
341 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
342 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
343 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
344 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
345 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
346 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
347 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
348 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
349 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
350 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
351 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
352 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
353 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
354 | S2, S2, S2, S2, S2, S2, S2, S2, | ||
355 | S2, S2, S2, S2, S2, S2, S2, S2 } | ||
202 | }; | 356 | }; |
203 | 357 | ||
204 | static int | ||
205 | token_num(int c) | ||
206 | { | ||
207 | switch (c) | ||
208 | { | ||
209 | case '\n': | ||
210 | return 1; | ||
211 | case ' ': | ||
212 | return 2; | ||
213 | case '-': | ||
214 | return 3; | ||
215 | default: | ||
216 | return 0; | ||
217 | } | ||
218 | } | ||
219 | |||
220 | #define F_FIRST 0x01 /* First part of the message (no EB seen so far) */ | 358 | #define F_FIRST 0x01 /* First part of the message (no EB seen so far) */ |
221 | #define F_ENCAPS 0x02 /* Within encapsulated part */ | 359 | #define F_ENCAPS 0x02 /* Within encapsulated part */ |
222 | 360 | ||
... | @@ -228,7 +366,7 @@ struct burst_stream | ... | @@ -228,7 +366,7 @@ struct burst_stream |
228 | size_t partno; /* Number of the part within the message */ | 366 | size_t partno; /* Number of the part within the message */ |
229 | }; | 367 | }; |
230 | 368 | ||
231 | static void | 369 | static inline void |
232 | finish_stream (struct burst_stream *bs) | 370 | finish_stream (struct burst_stream *bs) |
233 | { | 371 | { |
234 | if (bs->stream) | 372 | if (bs->stream) |
... | @@ -249,7 +387,7 @@ finish_stream (struct burst_stream *bs) | ... | @@ -249,7 +387,7 @@ finish_stream (struct burst_stream *bs) |
249 | } | 387 | } |
250 | } | 388 | } |
251 | 389 | ||
252 | static void | 390 | static inline void |
253 | flush_stream (struct burst_stream *bs, char *buf, size_t size) | 391 | flush_stream (struct burst_stream *bs, char *buf, size_t size) |
254 | { | 392 | { |
255 | int rc; | 393 | int rc; |
... | @@ -297,7 +435,7 @@ int | ... | @@ -297,7 +435,7 @@ int |
297 | burst_digest (mu_message_t msg) | 435 | burst_digest (mu_message_t msg) |
298 | { | 436 | { |
299 | mu_stream_t is; | 437 | mu_stream_t is; |
300 | char c; | 438 | unsigned char c; |
301 | size_t n; | 439 | size_t n; |
302 | int state = S1; | 440 | int state = S1; |
303 | int eb_length = 0; | 441 | int eb_length = 0; |
... | @@ -312,7 +450,7 @@ burst_digest (mu_message_t msg) | ... | @@ -312,7 +450,7 @@ burst_digest (mu_message_t msg) |
312 | mu_message_get_streamref (msg, &is); | 450 | mu_message_get_streamref (msg, &is); |
313 | while (mu_stream_read (is, &c, 1, &n) == 0 && n == 1) | 451 | while (mu_stream_read (is, &c, 1, &n) == 0 && n == 1) |
314 | { | 452 | { |
315 | int newstate = transtab[state - 1][token_num (c)]; | 453 | int newstate = transtab[state - 1][c]; |
316 | int silent = 0; | 454 | int silent = 0; |
317 | 455 | ||
318 | if (newstate < 0) | 456 | if (newstate < 0) |
... | @@ -358,7 +496,7 @@ burst_digest (mu_message_t msg) | ... | @@ -358,7 +496,7 @@ burst_digest (mu_message_t msg) |
358 | } | 496 | } |
359 | state = newstate; | 497 | state = newstate; |
360 | if (!silent) | 498 | if (!silent) |
361 | flush_stream (&bs, &c, 1); | 499 | flush_stream (&bs, (char*)&c, 1); |
362 | } | 500 | } |
363 | mu_stream_destroy (&is); | 501 | mu_stream_destroy (&is); |
364 | 502 | ... | ... |
mh/burstfsa.c
0 → 100644
1 | /* Generate FSA states for RFC 934 bursting agent. | ||
2 | Copyright (C) 2010 Free Software Foundation, Inc. | ||
3 | |||
4 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation; either version 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | GNU Mailutils is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */ | ||
16 | |||
17 | #include <stdio.h> | ||
18 | |||
19 | /* Bursting FSA states accoring to RFC 934: | ||
20 | |||
21 | S1 :: "-" S3 | ||
22 | | CRLF {CRLF} S1 | ||
23 | | c {c} S2 | ||
24 | |||
25 | S2 :: CRLF {CRLF} S1 | ||
26 | | c {c} S2 | ||
27 | |||
28 | S3 :: " " S2 | ||
29 | | c S4 ;; the bursting agent should consider the current | ||
30 | ;; message ended. | ||
31 | |||
32 | S4 :: CRLF S5 | ||
33 | | c S4 | ||
34 | |||
35 | S5 :: CRLF S5 | ||
36 | | c {c} S2 ;; The bursting agent should consider a new | ||
37 | ;; message started | ||
38 | */ | ||
39 | |||
40 | static int | ||
41 | token_num (int c) | ||
42 | { | ||
43 | switch (c) | ||
44 | { | ||
45 | case '\n': | ||
46 | return 1; | ||
47 | case ' ': | ||
48 | return 2; | ||
49 | case '-': | ||
50 | return 3; | ||
51 | default: | ||
52 | return 0; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | #define S1 1 | ||
57 | #define S2 2 | ||
58 | #define S3 3 | ||
59 | #define S4 4 | ||
60 | #define S5 5 | ||
61 | |||
62 | /* Negative state means no write */ | ||
63 | int transtab[][4] = { | ||
64 | /* DEF '\n' ' ' '-' */ | ||
65 | /* S1 */ { S2, S1, S2, -S3 }, | ||
66 | /* S2 */ { S2, S1, S2, S2 }, | ||
67 | /* S3 */ { -S4, -S4, -S2, -S4 }, | ||
68 | /* S4 */ { -S4, -S5, -S4, -S4 }, | ||
69 | /* S5 */ { S2, -S5, S2, S2 } | ||
70 | }; | ||
71 | |||
72 | int | ||
73 | main() | ||
74 | { | ||
75 | int i, state; | ||
76 | |||
77 | for (state = S1; state <= S5; state++) | ||
78 | { | ||
79 | printf ("/* S%d */ { ", state); | ||
80 | for (i = 0; i < 256; i++) | ||
81 | { | ||
82 | int newstate = transtab[state - 1][token_num (i)]; | ||
83 | |||
84 | if (i > 0) | ||
85 | { | ||
86 | putchar (','); | ||
87 | if (i % 8 == 0) | ||
88 | printf ("\n "); | ||
89 | } | ||
90 | putchar (' '); | ||
91 | if (newstate < 0) | ||
92 | { | ||
93 | putchar ('-'); | ||
94 | newstate = -newstate; | ||
95 | } | ||
96 | else | ||
97 | putchar (' '); | ||
98 | printf ("S%d", newstate); | ||
99 | } | ||
100 | printf (" },\n"); | ||
101 | } | ||
102 | return 0; | ||
103 | } | ||
104 | |||
105 |
-
Please register or sign in to post a comment