ffi-hydrogen 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +30 -0
  5. data/.travis.yml +10 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +72 -0
  9. data/Rakefile +46 -0
  10. data/bench/both.rb +86 -0
  11. data/bench/encode.rb +57 -0
  12. data/bench/encrypt.rb +80 -0
  13. data/bench/init.rb +5 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/ffi-hydrogen.gemspec +31 -0
  17. data/lib/ffi/hydrogen.rb +216 -0
  18. data/vendor/.clang-format +2 -0
  19. data/vendor/.gitignore +3 -0
  20. data/vendor/README.md +2 -0
  21. data/vendor/libhydrogen/.clang-format +95 -0
  22. data/vendor/libhydrogen/.gitignore +32 -0
  23. data/vendor/libhydrogen/.travis.yml +22 -0
  24. data/vendor/libhydrogen/LICENSE +18 -0
  25. data/vendor/libhydrogen/Makefile +61 -0
  26. data/vendor/libhydrogen/Makefile.arduino +51 -0
  27. data/vendor/libhydrogen/README.md +29 -0
  28. data/vendor/libhydrogen/hydrogen.c +18 -0
  29. data/vendor/libhydrogen/hydrogen.h +317 -0
  30. data/vendor/libhydrogen/impl/common.h +316 -0
  31. data/vendor/libhydrogen/impl/core.h +220 -0
  32. data/vendor/libhydrogen/impl/gimli-core/portable.h +39 -0
  33. data/vendor/libhydrogen/impl/gimli-core/sse2.h +97 -0
  34. data/vendor/libhydrogen/impl/gimli-core.h +25 -0
  35. data/vendor/libhydrogen/impl/hash.h +138 -0
  36. data/vendor/libhydrogen/impl/hydrogen_p.h +83 -0
  37. data/vendor/libhydrogen/impl/kdf.h +20 -0
  38. data/vendor/libhydrogen/impl/kx.h +441 -0
  39. data/vendor/libhydrogen/impl/pwhash.h +281 -0
  40. data/vendor/libhydrogen/impl/random.h +376 -0
  41. data/vendor/libhydrogen/impl/secretbox.h +236 -0
  42. data/vendor/libhydrogen/impl/sign.h +207 -0
  43. data/vendor/libhydrogen/impl/x25519.h +383 -0
  44. data/vendor/libhydrogen/library.properties +10 -0
  45. data/vendor/libhydrogen/logo.png +0 -0
  46. data/vendor/libhydrogen/tests/tests.c +431 -0
  47. data/vendor/main.c +140 -0
  48. data/vendor/stringencoders/.gitignore +25 -0
  49. data/vendor/stringencoders/.travis.yml +13 -0
  50. data/vendor/stringencoders/AUTHORS +1 -0
  51. data/vendor/stringencoders/COPYING +2 -0
  52. data/vendor/stringencoders/ChangeLog +170 -0
  53. data/vendor/stringencoders/Doxyfile +276 -0
  54. data/vendor/stringencoders/INSTALL +119 -0
  55. data/vendor/stringencoders/LICENSE +22 -0
  56. data/vendor/stringencoders/Makefile.am +3 -0
  57. data/vendor/stringencoders/NEWS +3 -0
  58. data/vendor/stringencoders/README +2 -0
  59. data/vendor/stringencoders/README.md +32 -0
  60. data/vendor/stringencoders/bootstrap.sh +3 -0
  61. data/vendor/stringencoders/configure-gcc-hardened.sh +16 -0
  62. data/vendor/stringencoders/configure.ac +44 -0
  63. data/vendor/stringencoders/doxy/footer.html +34 -0
  64. data/vendor/stringencoders/doxy/header.html +85 -0
  65. data/vendor/stringencoders/indent.sh +9 -0
  66. data/vendor/stringencoders/javascript/base64-speed.html +43 -0
  67. data/vendor/stringencoders/javascript/base64-test.html +209 -0
  68. data/vendor/stringencoders/javascript/base64.html +18 -0
  69. data/vendor/stringencoders/javascript/base64.js +176 -0
  70. data/vendor/stringencoders/javascript/qunit.css +119 -0
  71. data/vendor/stringencoders/javascript/qunit.js +1062 -0
  72. data/vendor/stringencoders/javascript/urlparse-test.html +367 -0
  73. data/vendor/stringencoders/javascript/urlparse.js +328 -0
  74. data/vendor/stringencoders/make-ci.sh +13 -0
  75. data/vendor/stringencoders/makerelease.sh +16 -0
  76. data/vendor/stringencoders/python/b85.py +176 -0
  77. data/vendor/stringencoders/src/Makefile.am +134 -0
  78. data/vendor/stringencoders/src/arraytoc.c +85 -0
  79. data/vendor/stringencoders/src/arraytoc.h +43 -0
  80. data/vendor/stringencoders/src/extern_c_begin.h +3 -0
  81. data/vendor/stringencoders/src/extern_c_end.h +3 -0
  82. data/vendor/stringencoders/src/html_named_entities_generator.py +203 -0
  83. data/vendor/stringencoders/src/modp_ascii.c +159 -0
  84. data/vendor/stringencoders/src/modp_ascii.h +162 -0
  85. data/vendor/stringencoders/src/modp_ascii_data.h +84 -0
  86. data/vendor/stringencoders/src/modp_ascii_gen.c +55 -0
  87. data/vendor/stringencoders/src/modp_b16.c +125 -0
  88. data/vendor/stringencoders/src/modp_b16.h +148 -0
  89. data/vendor/stringencoders/src/modp_b16_data.h +104 -0
  90. data/vendor/stringencoders/src/modp_b16_gen.c +65 -0
  91. data/vendor/stringencoders/src/modp_b2.c +69 -0
  92. data/vendor/stringencoders/src/modp_b2.h +130 -0
  93. data/vendor/stringencoders/src/modp_b2_data.h +44 -0
  94. data/vendor/stringencoders/src/modp_b2_gen.c +36 -0
  95. data/vendor/stringencoders/src/modp_b36.c +108 -0
  96. data/vendor/stringencoders/src/modp_b36.h +170 -0
  97. data/vendor/stringencoders/src/modp_b64.c +254 -0
  98. data/vendor/stringencoders/src/modp_b64.h +236 -0
  99. data/vendor/stringencoders/src/modp_b64_data.h +477 -0
  100. data/vendor/stringencoders/src/modp_b64_gen.c +168 -0
  101. data/vendor/stringencoders/src/modp_b64r.c +254 -0
  102. data/vendor/stringencoders/src/modp_b64r.h +242 -0
  103. data/vendor/stringencoders/src/modp_b64r_data.h +477 -0
  104. data/vendor/stringencoders/src/modp_b64w.c +254 -0
  105. data/vendor/stringencoders/src/modp_b64w.h +231 -0
  106. data/vendor/stringencoders/src/modp_b64w_data.h +477 -0
  107. data/vendor/stringencoders/src/modp_b85.c +109 -0
  108. data/vendor/stringencoders/src/modp_b85.h +171 -0
  109. data/vendor/stringencoders/src/modp_b85_data.h +36 -0
  110. data/vendor/stringencoders/src/modp_b85_gen.c +65 -0
  111. data/vendor/stringencoders/src/modp_bjavascript.c +65 -0
  112. data/vendor/stringencoders/src/modp_bjavascript.h +105 -0
  113. data/vendor/stringencoders/src/modp_bjavascript_data.h +84 -0
  114. data/vendor/stringencoders/src/modp_bjavascript_gen.c +58 -0
  115. data/vendor/stringencoders/src/modp_burl.c +228 -0
  116. data/vendor/stringencoders/src/modp_burl.h +259 -0
  117. data/vendor/stringencoders/src/modp_burl_data.h +136 -0
  118. data/vendor/stringencoders/src/modp_burl_gen.c +121 -0
  119. data/vendor/stringencoders/src/modp_html.c +128 -0
  120. data/vendor/stringencoders/src/modp_html.h +53 -0
  121. data/vendor/stringencoders/src/modp_html_named_entities.h +9910 -0
  122. data/vendor/stringencoders/src/modp_json.c +315 -0
  123. data/vendor/stringencoders/src/modp_json.h +103 -0
  124. data/vendor/stringencoders/src/modp_json_data.h +57 -0
  125. data/vendor/stringencoders/src/modp_json_gen.py +60 -0
  126. data/vendor/stringencoders/src/modp_mainpage.h +120 -0
  127. data/vendor/stringencoders/src/modp_numtoa.c +350 -0
  128. data/vendor/stringencoders/src/modp_numtoa.h +100 -0
  129. data/vendor/stringencoders/src/modp_qsiter.c +76 -0
  130. data/vendor/stringencoders/src/modp_qsiter.h +71 -0
  131. data/vendor/stringencoders/src/modp_stdint.h +43 -0
  132. data/vendor/stringencoders/src/modp_utf8.c +88 -0
  133. data/vendor/stringencoders/src/modp_utf8.h +38 -0
  134. data/vendor/stringencoders/src/modp_xml.c +311 -0
  135. data/vendor/stringencoders/src/modp_xml.h +166 -0
  136. data/vendor/stringencoders/src/stringencoders.pc +10 -0
  137. data/vendor/stringencoders/src/stringencoders.pc.in +10 -0
  138. data/vendor/stringencoders/test/Makefile.am +113 -0
  139. data/vendor/stringencoders/test/apr_base64.c +262 -0
  140. data/vendor/stringencoders/test/apr_base64.h +120 -0
  141. data/vendor/stringencoders/test/cxx_test.cc +482 -0
  142. data/vendor/stringencoders/test/minunit.h +82 -0
  143. data/vendor/stringencoders/test/modp_ascii_test.c +281 -0
  144. data/vendor/stringencoders/test/modp_b16_test.c +288 -0
  145. data/vendor/stringencoders/test/modp_b2_test.c +250 -0
  146. data/vendor/stringencoders/test/modp_b64_test.c +266 -0
  147. data/vendor/stringencoders/test/modp_b85_test.c +130 -0
  148. data/vendor/stringencoders/test/modp_bjavascript_test.c +137 -0
  149. data/vendor/stringencoders/test/modp_burl_test.c +423 -0
  150. data/vendor/stringencoders/test/modp_html_test.c +296 -0
  151. data/vendor/stringencoders/test/modp_json_test.c +336 -0
  152. data/vendor/stringencoders/test/modp_numtoa_test.c +545 -0
  153. data/vendor/stringencoders/test/modp_qsiter_test.c +280 -0
  154. data/vendor/stringencoders/test/modp_utf8_test.c +188 -0
  155. data/vendor/stringencoders/test/modp_xml_test.c +339 -0
  156. data/vendor/stringencoders/test/speedtest.c +241 -0
  157. data/vendor/stringencoders/test/speedtest_ascii.c +345 -0
  158. data/vendor/stringencoders/test/speedtest_msg.c +78 -0
  159. data/vendor/stringencoders/test/speedtest_numtoa.c +276 -0
  160. metadata +314 -0
@@ -0,0 +1,345 @@
1
+ #include "modp_ascii.h"
2
+ #include "modp_ascii_data.h"
3
+ #include <ctype.h>
4
+ #include <stdint.h>
5
+ #include <stdio.h>
6
+ #include <time.h>
7
+
8
+ //extern const char gsToUpperMap[256];
9
+ /**
10
+ * This is standard clib implementation of uppercasing a string.
11
+ * It has an unfair advantage since it's inside the test file
12
+ * so the optimizer can inline it.
13
+ */
14
+ static void toupper_copy1(char* dest, const char* str, size_t len)
15
+ {
16
+ size_t i;
17
+ for (i = 0; i < len; ++i) {
18
+ // toupper is defined in <ctype.h>
19
+ *dest++ = (char)toupper((int)str[i]);
20
+ }
21
+ *dest = 0;
22
+ }
23
+
24
+ /**
25
+ * Skipping ctype, and doing the compare directly
26
+ *
27
+ */
28
+ static void toupper_copy2(char* dest, const char* str, size_t len)
29
+ {
30
+ size_t i;
31
+ char c;
32
+ for (i = 0; i < len; ++i) {
33
+ c = str[i];
34
+ *dest++ = (char)((c >= 'a' && c <= 'z') ? c : (c - 32));
35
+ }
36
+ *dest = 0;
37
+ }
38
+
39
+ /**
40
+ * Sequential table lookup
41
+ */
42
+ static void toupper_copy3(char* dest, const char* str, size_t len)
43
+ {
44
+ size_t i;
45
+ unsigned char c;
46
+ for (i = 0; i < len; ++i) {
47
+ c = (unsigned char)str[i];
48
+ *dest++ = (char)gsToUpperMap[c];
49
+ }
50
+ *dest = 0;
51
+ }
52
+
53
+ /** \brief toupper Version 4 -- parallel table lookup
54
+ *
55
+ *
56
+ */
57
+ static void toupper_copy4(char* dest, const char* str, size_t len)
58
+ {
59
+ /*
60
+ * size_t i;
61
+ * for (i = 0; i < len; ++i) {
62
+ * char c = str[i];
63
+ * *dest++ = (c >= 'a' && c <= 'z') ? c - 32 : c;
64
+ * }
65
+ */
66
+
67
+ size_t i;
68
+ uint8_t c1, c2, c3, c4;
69
+
70
+ const size_t leftover = len % 4;
71
+ const size_t imax = len - leftover;
72
+ const uint8_t* s = (const uint8_t*)str;
73
+ for (i = 0; i != imax; i += 4) {
74
+ /*
75
+ * it's important to make these variables
76
+ * it helps the optimizer to figure out what to do
77
+ */
78
+ c1 = s[i], c2 = s[i + 1], c3 = s[i + 2], c4 = s[i + 3];
79
+ dest[0] = (char)gsToUpperMap[c1];
80
+ dest[1] = (char)gsToUpperMap[c2];
81
+ dest[2] = (char)gsToUpperMap[c3];
82
+ dest[3] = (char)gsToUpperMap[c4];
83
+ dest += 4;
84
+ }
85
+
86
+ switch (leftover) {
87
+ case 3:
88
+ *dest++ = (char)gsToUpperMap[s[i++]];
89
+ case 2:
90
+ *dest++ = (char)gsToUpperMap[s[i++]];
91
+ case 1:
92
+ *dest++ = (char)gsToUpperMap[s[i]];
93
+ case 0:
94
+ *dest = '\0';
95
+ }
96
+ }
97
+
98
+ /** \brief toupper Versions 5 -- hsieh alternate
99
+ * Based code from Paul Hsieh
100
+ * http://www.azillionmonkeys.com/qed/asmexample.html
101
+ *
102
+ * This was his "improved" version, but it appears to either run just
103
+ * as fast, or a bit slower than his original version
104
+ */
105
+ static void toupper_copy5(char* dest, const char* str, size_t len)
106
+ {
107
+ size_t i;
108
+ uint32_t eax, ebx, ecx, edx;
109
+ const uint8_t* ustr = (const uint8_t*)str;
110
+ const int leftover = len % 4;
111
+ const size_t imax = len / 4;
112
+ const uint32_t* s = (const uint32_t*)str;
113
+ uint32_t* d = (uint32_t*)dest;
114
+ for (i = 0; i != imax; ++i) {
115
+ eax = s[i];
116
+ ebx = 0x80808080u | eax;
117
+ ecx = ebx - 0x61616161u;
118
+ edx = ~(ebx - 0x7b7b7b7bu);
119
+ ebx = (ecx & edx) & (~eax & 0x80808080u);
120
+ *d++ = eax - (ebx >> 2);
121
+ }
122
+
123
+ i = imax * 4;
124
+ dest = (char*)d;
125
+ switch (leftover) {
126
+ case 3:
127
+ *dest++ = (char)gsToUpperMap[ustr[i++]];
128
+ case 2:
129
+ *dest++ = (char)gsToUpperMap[ustr[i++]];
130
+ case 1:
131
+ *dest++ = (char)gsToUpperMap[ustr[i]];
132
+ case 0:
133
+ *dest = '\0';
134
+ }
135
+ }
136
+
137
+ /** \brief ToUpper Version 6 -- Hsieh original, ASM style
138
+ * Based code from Paul Hsieh
139
+ * http://www.azillionmonkeys.com/qed/asmexample.html
140
+ *
141
+ * This is almost a direct port of the original ASM code, on some
142
+ * platforms/compilers it does run faster then the "de-asm'ed" version
143
+ * used in the modp library.
144
+ *
145
+ */
146
+ static void toupper_copy6(char* dest, const char* str, size_t len)
147
+ {
148
+ size_t i = 0;
149
+ uint32_t eax, ebx, ecx, edx;
150
+ const uint8_t* ustr = (const uint8_t*)str;
151
+ const int leftover = len % 4;
152
+ const size_t imax = len / 4;
153
+ const uint32_t* s = (const uint32_t*)str;
154
+ uint32_t* d = (uint32_t*)dest;
155
+ for (i = 0; i != imax; ++i) {
156
+ #if 1
157
+ /*
158
+ * as close to original asm code as possible
159
+ */
160
+ eax = s[i];
161
+ ebx = 0x7f7f7f7f;
162
+ edx = 0x7f7f7f7f;
163
+ ebx = ebx & eax;
164
+ ebx = ebx + 0x05050505;
165
+ ecx = eax;
166
+ ecx = ~ecx;
167
+ ebx = ebx & edx;
168
+ ebx = ebx + 0x1a1a1a1a;
169
+ ebx = ebx & ecx;
170
+ ebx = ebx >> 2;
171
+ ebx = ebx & 0x20202020;
172
+ eax = eax - ebx;
173
+ *d++ = eax;
174
+ #else
175
+ /*
176
+ * "de-asm'ed" version, this is what is used in the modp library
177
+ */
178
+ eax = s[i];
179
+ ebx = (0x7f7f7f7ful & eax) + 0x05050505ul;
180
+ ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
181
+ ebx = ((ebx & ~eax) >> 2) & 0x20202020ul;
182
+ *d++ = eax - ebx;
183
+ #endif
184
+ }
185
+
186
+ i = imax * 4;
187
+ dest = (char*)d;
188
+ switch (leftover) {
189
+ case 3:
190
+ *dest++ = (char)gsToUpperMap[ustr[i++]];
191
+ case 2:
192
+ *dest++ = (char)gsToUpperMap[ustr[i++]];
193
+ case 1:
194
+ *dest++ = (char)gsToUpperMap[ustr[i]];
195
+ case 0:
196
+ *dest = '\0';
197
+ }
198
+ }
199
+
200
+ static void modp_toupper_copy_a2(char* dest, const char* str, size_t len)
201
+ {
202
+ size_t i = 0;
203
+ uint32_t eax, ebx;
204
+ const uint8_t* ustr = (const uint8_t*)str;
205
+ const size_t leftover = len % 4;
206
+ const size_t imax = len / 4;
207
+ const uint32_t* s = (const uint32_t*)str;
208
+ uint32_t* d = (uint32_t*)dest;
209
+ for (i = 0; i != imax; ++i) {
210
+ eax = s[i];
211
+
212
+ ebx = (0x7f7f7f7fu & eax) + 0x05050505u;
213
+ ebx = (0x7f7f7f7fu & ebx) + 0x1a1a1a1au;
214
+ ebx = ((ebx & ~eax) >> 2) & 0x20202020u;
215
+ *d++ = eax - ebx;
216
+ }
217
+
218
+ i = imax * 4;
219
+ dest = (char*)d;
220
+ switch (leftover) {
221
+ case 3:
222
+ *dest++ = (char)gsToUpperMap[ustr[i++]];
223
+ case 2:
224
+ *dest++ = (char)gsToUpperMap[ustr[i++]];
225
+ case 1:
226
+ *dest++ = (char)gsToUpperMap[ustr[i]];
227
+ case 0:
228
+ *dest = '\0';
229
+ }
230
+ }
231
+
232
+ int main(void)
233
+ {
234
+ double last = 0.0;
235
+ size_t i = 0;
236
+ char buf[256];
237
+ char obuf[300];
238
+
239
+ for (i = 0; i < 256; ++i) {
240
+ buf[i] = (char)i;
241
+ }
242
+
243
+ uint32_t max = 1000000;
244
+ clock_t t0, t1;
245
+ printf("%s", "type\tclib\tdirect\tmap\tpara\thsieh1\thsieh2\tAlt\tFinal\timprovement\n");
246
+
247
+ printf("toupper\t");
248
+ fflush(stdout);
249
+
250
+ /**
251
+ ** V1
252
+ **/
253
+ t0 = clock();
254
+ for (i = 0; i < max; ++i) {
255
+ toupper_copy1(obuf, buf, sizeof(buf));
256
+ }
257
+ t1 = clock();
258
+ last = (double)(t1 - t0);
259
+ printf("%lu\t", (t1 - t0));
260
+ fflush(stdout);
261
+
262
+ /**
263
+ ** V2
264
+ **/
265
+ t0 = clock();
266
+ for (i = 0; i < max; ++i) {
267
+ toupper_copy2(obuf, buf, sizeof(buf));
268
+ }
269
+ t1 = clock();
270
+ printf("%lu\t", (t1 - t0));
271
+ fflush(stdout);
272
+
273
+ /**
274
+ ** V3
275
+ **/
276
+ t0 = clock();
277
+ for (i = 0; i < max; ++i) {
278
+ toupper_copy3(obuf, buf, sizeof(buf));
279
+ }
280
+ t1 = clock();
281
+ printf("%lu\t", (t1 - t0));
282
+ fflush(stdout);
283
+
284
+ /**
285
+ ** V4 -- Parallel Table Lookup
286
+ **/
287
+ t0 = clock();
288
+ for (i = 0; i < max; ++i) {
289
+ toupper_copy4(obuf, buf, sizeof(buf));
290
+ }
291
+ t1 = clock();
292
+ printf("%lu\t", (t1 - t0));
293
+ fflush(stdout);
294
+
295
+ /**
296
+ ** V5 -- Hsieh Alternate
297
+ **/
298
+ t0 = clock();
299
+ for (i = 0; i < max; ++i) {
300
+ toupper_copy5(obuf, buf, sizeof(buf));
301
+ }
302
+ t1 = clock();
303
+ printf("%lu\t", (t1 - t0));
304
+ fflush(stdout);
305
+
306
+ /**
307
+ ** HSEIH -- asm style
308
+ **/
309
+ t0 = clock();
310
+ for (i = 0; i < max; ++i) {
311
+ toupper_copy6(obuf, buf, sizeof(buf));
312
+ }
313
+ t1 = clock();
314
+ printf("%lu\t", (t1 - t0));
315
+ fflush(stdout);
316
+
317
+ /**
318
+ ** MODP ALT
319
+ **/
320
+ t0 = clock();
321
+ for (i = 0; i < max; ++i) {
322
+ modp_toupper_copy_a2(obuf, buf, sizeof(buf));
323
+ }
324
+ t1 = clock();
325
+
326
+ printf("%lu\t", (t1 - t0));
327
+ fflush(stdout);
328
+
329
+ /**
330
+ ** MODP FINAL
331
+ **/
332
+ t0 = clock();
333
+ for (i = 0; i < max; ++i) {
334
+ modp_toupper_copy(obuf, buf, sizeof(buf));
335
+ }
336
+ t1 = clock();
337
+
338
+ printf("%lu\t", (t1 - t0));
339
+ fflush(stdout);
340
+
341
+ printf("%.1fx\n", last / ((double)(t1 - t0)));
342
+ fflush(stdout);
343
+
344
+ return 0;
345
+ }
@@ -0,0 +1,78 @@
1
+ #include "modp_json.h"
2
+
3
+ #include <time.h>
4
+ #ifndef CLOCKS_PER_SEC
5
+ #ifdef CLK_TCK
6
+ #define CLOCKS_PER_SEC (CLK_TCK)
7
+ #endif
8
+ #endif
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <string.h>
12
+
13
+ size_t test_json_encode(char* dest)
14
+ {
15
+ modp_json_ctx ctx;
16
+ modp_json_init(&ctx, dest);
17
+ modp_json_map_open(&ctx);
18
+
19
+ modp_json_add_cstring(&ctx, "start_ms");
20
+ modp_json_add_uint32(&ctx, 123456789);
21
+
22
+ modp_json_add_cstring(&ctx, "remote_ip");
23
+ modp_json_add_cstring(&ctx, "123.123.123.13");
24
+
25
+ modp_json_add_cstring(&ctx, "request");
26
+ modp_json_add_cstring(&ctx, "GET /foobar HTTP/1.1");
27
+
28
+ modp_json_add_cstring(&ctx, "headers_in");
29
+ modp_json_ary_open(&ctx);
30
+
31
+ modp_json_ary_open(&ctx);
32
+ modp_json_add_cstring(&ctx, "Accept");
33
+ modp_json_add_cstring(&ctx, "*/*");
34
+ modp_json_ary_close(&ctx);
35
+
36
+ modp_json_ary_open(&ctx);
37
+ modp_json_add_cstring(&ctx, "Content-type");
38
+ modp_json_add_cstring(&ctx, "text/plain");
39
+ modp_json_ary_close(&ctx);
40
+
41
+ modp_json_ary_open(&ctx);
42
+ modp_json_add_cstring(&ctx, "Connection");
43
+ modp_json_add_cstring(&ctx, "close");
44
+ modp_json_ary_close(&ctx);
45
+
46
+ modp_json_ary_open(&ctx);
47
+ modp_json_add_cstring(&ctx, "User-agent");
48
+ modp_json_add_cstring(&ctx, "Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405");
49
+ modp_json_ary_close(&ctx);
50
+
51
+ modp_json_ary_close(&ctx);
52
+ modp_json_map_close(&ctx);
53
+ return modp_json_end(&ctx);
54
+ }
55
+
56
+ int main(void)
57
+ {
58
+
59
+ const int imax = 1000000;
60
+ clock_t t0, t1;
61
+ int i;
62
+ double s1;
63
+ size_t len;
64
+ char buf2[512];
65
+
66
+ printf("ALG\tEncodes/Sec\tBYTES\n");
67
+ fflush(stdout);
68
+ t0 = clock();
69
+ for (i = 0; i < imax; ++i) {
70
+ len = test_json_encode(NULL);
71
+ len = test_json_encode(buf2);
72
+ }
73
+ t1 = clock();
74
+ s1 = (double)(t1 - t0) * (1.0 / (double)CLOCKS_PER_SEC);
75
+ printf("%s\t%8.0f\t%u\n", "JSON", imax / s1, (unsigned)len);
76
+ fflush(stdout);
77
+ return 0;
78
+ }
@@ -0,0 +1,276 @@
1
+ /* needed since we compile as C90
2
+ * trick gcc to accepting snprintf which is a C99-ism
3
+ */
4
+ #define _ISOC99_SOURCE
5
+ #define _DEFAULT_SOURCE
6
+ #define _XOPEN_SOURCE 500
7
+ #define _POSIX_C_SOURCE 200112L
8
+ #include <stdint.h>
9
+ #include <stdio.h>
10
+ #include <time.h>
11
+
12
+ #include "modp_numtoa.h"
13
+
14
+ int main(void)
15
+ {
16
+ char buf[100];
17
+ uint32_t max = 1000000;
18
+ clock_t t0, t1;
19
+ double d, last;
20
+ uint32_t i;
21
+ uint8_t ju8;
22
+ uint16_t ju16;
23
+ int8_t j8;
24
+ int16_t j16;
25
+ printf("%s", " type \tsprintf\tsnprf\tnumtoa\timprovement\n");
26
+
27
+ printf("unsigned 8\t");
28
+ t0 = clock();
29
+ for (i = 0; i < max; ++i) {
30
+ ju8 = (uint8_t)i;
31
+ sprintf(buf, "%hhu", ju8);
32
+ }
33
+ t1 = clock();
34
+ printf("%lu\t", (t1 - t0));
35
+ fflush(stdout);
36
+
37
+ t0 = clock();
38
+ for (i = 0; i < max; ++i) {
39
+ ju8 = (uint8_t)i;
40
+ snprintf(buf, sizeof(buf), "%hhu", ju8);
41
+ }
42
+ t1 = clock();
43
+ last = ((double)(t1 - t0));
44
+ printf("%lu\t", (t1 - t0));
45
+ fflush(stdout);
46
+
47
+ t0 = clock();
48
+ for (i = 0; i < max; ++i) {
49
+ ju8 = (uint8_t)i;
50
+ modp_uitoa10(ju8, buf);
51
+ }
52
+ t1 = clock();
53
+ printf("%lu\t", (t1 - t0));
54
+ printf("%.1fx\n", last / ((double)(t1 - t0)));
55
+ fflush(stdout);
56
+
57
+ printf("unsigned 16\t");
58
+ t0 = clock();
59
+ for (i = 0; i < max; ++i) {
60
+ ju16 = (uint16_t)i;
61
+ sprintf(buf, "%hu", ju16);
62
+ }
63
+ t1 = clock();
64
+ printf("%lu\t", (t1 - t0));
65
+ fflush(stdout);
66
+
67
+ t0 = clock();
68
+ for (i = 0; i < max; ++i) {
69
+ ju16 = (uint16_t)i;
70
+ snprintf(buf, sizeof(buf), "%hu", ju16);
71
+ }
72
+ t1 = clock();
73
+ last = ((double)(t1 - t0));
74
+ printf("%lu\t", (t1 - t0));
75
+ fflush(stdout);
76
+
77
+ t0 = clock();
78
+ for (i = 0; i < max; ++i) {
79
+ ju16 = (uint16_t)i;
80
+ modp_uitoa10(ju16, buf);
81
+ }
82
+ t1 = clock();
83
+ printf("%lu\t", ((t1 - t0)));
84
+ printf("%.1fx\n", last / ((double)(t1 - t0)));
85
+ fflush(stdout);
86
+
87
+ printf("unsigned 32\t");
88
+ t0 = clock();
89
+ for (i = 0; i < max; ++i) {
90
+ sprintf(buf, "%u", i);
91
+ }
92
+ t1 = clock();
93
+ printf("%lu\t", ((t1 - t0)));
94
+ fflush(stdout);
95
+
96
+ t0 = clock();
97
+ for (i = 0; i < max; ++i) {
98
+ snprintf(buf, sizeof(buf), "%u", i);
99
+ }
100
+ t1 = clock();
101
+ last = ((double)(t1 - t0));
102
+ printf("%lu\t", ((t1 - t0)));
103
+ fflush(stdout);
104
+
105
+ t0 = clock();
106
+ for (i = 0; i < max; ++i) {
107
+ modp_uitoa10(i, buf);
108
+ }
109
+ t1 = clock();
110
+ printf("%lu\t", ((t1 - t0)));
111
+ printf("%.1fx\n", last / ((double)(t1 - t0)));
112
+ fflush(stdout);
113
+
114
+ printf("signed 8\t");
115
+ t0 = clock();
116
+ for (i = 0; i < max; ++i) {
117
+ j8 = (int8_t)i;
118
+ sprintf(buf, "%hhd", j8);
119
+ }
120
+ t1 = clock();
121
+ printf("%lu\t", ((t1 - t0)));
122
+ fflush(stdout);
123
+
124
+ t0 = clock();
125
+ for (i = 0; i < max; ++i) {
126
+ j8 = (int8_t)i;
127
+ snprintf(buf, sizeof(buf), "%hhd", j8);
128
+ }
129
+ t1 = clock();
130
+ last = ((double)(t1 - t0));
131
+ printf("%lu\t", ((t1 - t0)));
132
+ fflush(stdout);
133
+
134
+ t0 = clock();
135
+ for (i = 0; i < max; ++i) {
136
+ j8 = (int8_t)i;
137
+ modp_itoa10(j8, buf);
138
+ }
139
+ t1 = clock();
140
+ printf("%lu\t", ((t1 - t0)));
141
+ printf("%.1fx\n", last / ((double)(t1 - t0)));
142
+ fflush(stdout);
143
+
144
+ printf("signed 16\t");
145
+ t0 = clock();
146
+ for (i = 0; i < max; ++i) {
147
+ j16 = (int16_t)i;
148
+ sprintf(buf, "%hd", j16);
149
+ }
150
+ t1 = clock();
151
+ printf("%lu\t", ((t1 - t0)));
152
+ fflush(stdout);
153
+
154
+ t0 = clock();
155
+ for (i = 0; i < max; ++i) {
156
+ j16 = (int16_t)i;
157
+ snprintf(buf, sizeof(buf), "%hd", j16);
158
+ }
159
+ t1 = clock();
160
+ last = ((double)(t1 - t0));
161
+ printf("%lu\t", ((t1 - t0)));
162
+ fflush(stdout);
163
+
164
+ t0 = clock();
165
+ for (i = 0; i < max; ++i) {
166
+ j16 = (int16_t)i;
167
+ modp_itoa10(j16, buf);
168
+ }
169
+ t1 = clock();
170
+ printf("%lu\t", ((t1 - t0)));
171
+ printf("%.1fx\n", last / ((double)(t1 - t0)));
172
+ fflush(stdout);
173
+
174
+ printf("%s", "signed 32\t");
175
+ fflush(stdout);
176
+
177
+ t0 = clock();
178
+ for (i = 0; i < max; ++i) {
179
+ sprintf(buf, "%d", i);
180
+ }
181
+ t1 = clock();
182
+ printf("%lu\t", ((t1 - t0)));
183
+ fflush(stdout);
184
+
185
+ t0 = clock();
186
+ for (i = 0; i < max; ++i) {
187
+ snprintf(buf, sizeof(buf), "%d", i);
188
+ }
189
+ t1 = clock();
190
+ last = ((double)(t1 - t0));
191
+ printf("%lu\t", ((t1 - t0)));
192
+ fflush(stdout);
193
+
194
+ t0 = clock();
195
+ for (i = 0; i < max; ++i) {
196
+ modp_itoa10((int32_t)i, buf);
197
+ }
198
+ t1 = clock();
199
+ printf("%lu\t", ((t1 - t0)));
200
+ printf("%.1fx\n", last / ((double)(t1 - t0)));
201
+ fflush(stdout);
202
+
203
+ /* UITOA16 */
204
+
205
+ printf("%s", "unsigned 32 hex\t");
206
+ fflush(stdout);
207
+
208
+ t0 = clock();
209
+ for (i = 0; i < max; ++i) {
210
+ sprintf(buf, "%X", i);
211
+ }
212
+ t1 = clock();
213
+ printf("%lu\t", ((t1 - t0)));
214
+ fflush(stdout);
215
+
216
+ t0 = clock();
217
+ for (i = 0; i < max; ++i) {
218
+ snprintf(buf, sizeof(buf), "%08X", i);
219
+ }
220
+ t1 = clock();
221
+ last = ((double)(t1 - t0));
222
+ printf("%lu\t", ((t1 - t0)));
223
+ fflush(stdout);
224
+
225
+ t0 = clock();
226
+ for (i = 0; i < max; ++i) {
227
+ modp_uitoa16(i, buf, 1);
228
+ }
229
+ t1 = clock();
230
+ printf("%lu\t", ((t1 - t0)));
231
+ printf("%.1fx\n", last / ((double)(t1 - t0)));
232
+ fflush(stdout);
233
+
234
+ /** FLOATING POINT **/
235
+
236
+ printf("%s", "\n type \t%e\t%f\t%g\tdtoa\timprovement\n");
237
+ printf("%s", "double\t\t");
238
+ t0 = clock();
239
+ for (i = 0; i < max; ++i) {
240
+ sprintf(buf, "%e", (double)(i));
241
+ }
242
+ t1 = clock();
243
+ printf("%lu\t", ((t1 - t0)));
244
+ fflush(stdout);
245
+
246
+ t0 = clock();
247
+ for (i = 0; i < max; ++i) {
248
+ d = (double)i + 0.1;
249
+ snprintf(buf, sizeof(buf), "%f", d);
250
+ }
251
+ t1 = clock();
252
+ printf("%lu\t", ((t1 - t0)));
253
+ fflush(stdout);
254
+
255
+ t0 = clock();
256
+ for (i = 0; i < max; ++i) {
257
+ d = (double)i + 0.123456;
258
+ snprintf(buf, sizeof(buf), "%g", d);
259
+ }
260
+ t1 = clock();
261
+ last = ((double)(t1 - t0));
262
+ printf("%lu\t", ((t1 - t0)));
263
+ fflush(stdout);
264
+
265
+ t0 = clock();
266
+ for (i = 0; i < max; ++i) {
267
+ d = (double)i + 0.123456;
268
+ modp_dtoa(d, buf, 6);
269
+ }
270
+ t1 = clock();
271
+ printf("%lu\t", ((t1 - t0)));
272
+ printf("%.1fx\n", last / ((double)(t1 - t0)));
273
+ fflush(stdout);
274
+
275
+ return 0;
276
+ }