リビジョン | 4bf16172f09741942e4f2a42669ef176c9479ed9 (tree) |
---|---|
日時 | 2018-10-11 05:19:59 |
作者 | Agustina Arzille <avarzille@rise...> |
コミッター | Agustina Arzille |
Optimize serialization
@@ -56,15 +56,15 @@ | ||
56 | 56 | itype == typecode::TABLE || |
57 | 57 | itype == typecode::TREE || |
58 | 58 | itype == typecode::PKG || |
59 | - itype == typecode::SYMBOL || | |
59 | + itype == typecode::SYMBOL /* || | |
60 | 60 | itype == typecode::BVECTOR || |
61 | - itype == typecode::STR); | |
61 | + itype == typecode::STR */); | |
62 | 62 | } |
63 | 63 | |
64 | 64 | int xserialize (interpreter *interp, stream *strm, |
65 | 65 | object obj, serial_info& info) |
66 | 66 | { |
67 | - int ret = -2, idx = -1, tp = itype (obj); | |
67 | + int ret = 0, idx = -1, tp = itype (obj); | |
68 | 68 | |
69 | 69 | if (tp < 0 || tp >= typecode::LAST) |
70 | 70 | invalid_arg (interp, "serialize"); |
@@ -77,6 +77,18 @@ | ||
77 | 77 | return (strm->putb (interp, SERIAL_REF) + |
78 | 78 | strm->write (interp, &idx)); |
79 | 79 | } |
80 | + else if (tp == typecode::CHAR && as_char (obj) <= 0xff) | |
81 | + { | |
82 | + unsigned char data[] = { SERIAL_BCHAR, (unsigned char)as_char (obj) }; | |
83 | + return (strm->write (interp, data, sizeof (data))); | |
84 | + } | |
85 | + else if (tp == typecode::INT && as_int (obj) <= 0xff) | |
86 | + { | |
87 | + unsigned char data[] = { SERIAL_BINT, (unsigned char)as_int (obj) }; | |
88 | + return (strm->write (interp, data, sizeof (data))); | |
89 | + } | |
90 | + else if (obj == NIL) | |
91 | + return (strm->putb (interp, SERIAL_NIL)); | |
80 | 92 | |
81 | 93 | ret += strm->putb (interp, tp); |
82 | 94 | valref pos (interp, UNBOUND); |
@@ -115,13 +127,17 @@ | ||
115 | 127 | #undef DISPATCH |
116 | 128 | } |
117 | 129 | |
118 | - if (!(idx < 0) && !((idx = info.del_ref (idx)) < 0)) | |
119 | - { // XXX: Optimize for conses (too many seeks) | |
130 | + if ((idx >= 0 && (idx = info.del_ref (idx)) >= 0) || | |
131 | + (tp == typecode::CONS && (idx = 0, true))) | |
132 | + { | |
120 | 133 | valref p2 (interp, strm->tell (interp)); |
121 | 134 | if (!strm->seek (interp, *pos, SEEK_SET)) |
122 | 135 | return (-1); |
123 | 136 | |
124 | 137 | strm->write (interp, &idx); |
138 | + if (tp == typecode::CONS) | |
139 | + strm->write (interp, &info.output); | |
140 | + | |
125 | 141 | strm->seek (interp, *p2, SEEK_SET); |
126 | 142 | } |
127 | 143 |
@@ -144,6 +160,14 @@ | ||
144 | 160 | |
145 | 161 | qp_return (info.find_id (tp)); |
146 | 162 | } |
163 | + else if (tp == SERIAL_BINT || tp == SERIAL_BCHAR) | |
164 | + { | |
165 | + int nb = strm->getb (interp); | |
166 | + if (nb < 0) | |
167 | + qp_return (UNBOUND); | |
168 | + | |
169 | + qp_return (tp == SERIAL_BINT ? intobj (nb) : charobj (nb)); | |
170 | + } | |
147 | 171 | |
148 | 172 | info.output = 0; |
149 | 173 | if (serial_ref_p (tp) && !strm->sread (interp, &info.output)) |
@@ -434,7 +434,7 @@ | ||
434 | 434 | if (obj == NIL) |
435 | 435 | return (strm->putb (interp, SERIAL_NIL)); |
436 | 436 | |
437 | - valref tmp (interp, obj), pos1 (interp, strm->tell (interp)); | |
437 | + valref tmp (interp, obj); | |
438 | 438 | int len = 0, ret = 0; |
439 | 439 | |
440 | 440 | ret += strm->write (interp, &len); |
@@ -455,12 +455,7 @@ | ||
455 | 455 | } |
456 | 456 | } |
457 | 457 | |
458 | - valref pos2 (interp, strm->tell (interp)); | |
459 | - if (!strm->seek (interp, *pos1, SEEK_SET)) | |
460 | - return (-1); | |
461 | - | |
462 | - strm->write (interp, &len); | |
463 | - strm->seek (interp, *pos2, SEEK_SET); | |
458 | + info.output = len; // Let the caller know our length. | |
464 | 459 | return (ret); |
465 | 460 | } |
466 | 461 |
@@ -130,9 +130,11 @@ | ||
130 | 130 | { |
131 | 131 | sign = fneg_p (val); |
132 | 132 | #ifdef QP_ARCH_WIDE |
133 | - sign ^= varobj_sign (obj); | |
134 | - if (sign != 0) | |
135 | - val = -val; | |
133 | + if (varobj_sign (obj)) | |
134 | + { | |
135 | + sign ^= 1; | |
136 | + val = -val; | |
137 | + } | |
136 | 138 | #endif |
137 | 139 | cls = FP_NORMAL; |
138 | 140 | } |
@@ -121,6 +121,8 @@ | ||
121 | 121 | |
122 | 122 | enum |
123 | 123 | { |
124 | + SERIAL_BINT = 0xfa, | |
125 | + SERIAL_BCHAR = 0xfb, | |
124 | 126 | SERIAL_REF = 0xfc, |
125 | 127 | SERIAL_NIL = 0xfd, |
126 | 128 | SERIAL_END = 0xfe |