sereal 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e9c00595b7b2a08fc9b2357c3b052ff8a189f2e4
4
- data.tar.gz: baac3f20a92210853ee70a77472343509a025fb5
3
+ metadata.gz: 64ff27261d48f5ecf93ef19adb89cd126590c397
4
+ data.tar.gz: c2a9ecfb806d3dd559bc0b18e9aea7b26128c44a
5
5
  SHA512:
6
- metadata.gz: f9c467d56b2ee8386059b5799da6d40f3aa4dffdb81025ebdc02325593478f34b4ae08ea41a6498a0fb61f680645ef1c8b3e2f2f292158b1a818bb3434c25c72
7
- data.tar.gz: 85e7ed8c617e392617fa6adbf78bc04e7e758eb20e964a6e452eeff81045eb3dff2601b73a0c61548ea91e0fbae3df7ef8ad0bd87d0abcb383739880f32cf5e3
6
+ metadata.gz: 02e40c40cb0a9f151ff59fa257037986519fadd6c68d6733db37c35fe41cd72b0b5b7958317e8732c40a24cc6df9bb0af8a9f1c12442d379eaabc486c6c2203a
7
+ data.tar.gz: edfc06b878c2516420f7358cba6ea478b053ae5b0c1dad802e6634eccd96dc0dfaab6726060ea1b8721621f4275537a3a77ea320b636893f954a1a81922dc715
data/bin/rsrl CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- require 'pp'
3
2
 
3
+ # encoding: UTF-8
4
+ require 'pp'
4
5
  begin
5
6
  require File.join(".",File.dirname(__FILE__),'..','lib','sereal')
6
7
  rescue LoadError
@@ -1,6 +1,5 @@
1
1
  #include "sereal.h"
2
2
  #include <errno.h>
3
- static void s_dump(sereal_t *s);
4
3
 
5
4
  static inline void s_free_data_if_not_mine(sereal_t *s) {
6
5
  if (!(s->flags & __NOT_MINE)) {
@@ -47,7 +46,7 @@ static inline void s_destroy(sereal_t *s) {
47
46
 
48
47
  static inline void *s_realloc_or_raise(sereal_t *s, void *p, u32 n) {
49
48
  u8 *buf = realloc(p,n);
50
- if (!buf)
49
+ if (unlikely(!buf))
51
50
  s_raise(s,rb_eNoMemError,"memory allocation failure for %d bytes",n);
52
51
  return buf;
53
52
  }
@@ -66,15 +65,17 @@ static inline sereal_t * s_create(void) {
66
65
  }
67
66
 
68
67
  static inline void s_alloc(sereal_t *s, u32 len) {
69
- if (s->rsize > s->size + len)
68
+ if (likely(s->rsize > s->size + len))
70
69
  return;
71
70
 
72
- u32 size = s->size + len + 512; // every time allocate 512b more, so we wont alloc for a while
71
+ u32 size = s->size + len + 10240; // every time allocate 10k more, so we wont alloc for a while
73
72
  u8 *buf = s_realloc_or_raise(s,s->data,size);
74
73
  s->data = buf;
75
74
  s->rsize = size;
76
75
  }
77
-
76
+ static inline void s_prepare(sereal_t *s, u32 s_len) {
77
+ s_alloc(s,s_len);
78
+ }
78
79
  static inline void s_append(sereal_t *s, void *suffix, u32 s_len) {
79
80
  s_alloc(s,s_len);
80
81
  COPY(suffix,(s->data + s->size), s_len);
@@ -96,12 +97,12 @@ static inline int s_read_stream(sereal_t *s, u32 end) {
96
97
  u32 req = end - s->size;
97
98
  u32 left = s->buffer.size - s->buffer.pos;
98
99
  if (left == 0) {
99
- int rc = read(s->fd,s->buffer.data,BUFSIZ);
100
+ size_t rc = read(s->fd,s->buffer.data,BUFSIZ);
100
101
  if (rc <= 0)
101
102
  return -1;
102
- s->buffer.size = rc;
103
+ s->buffer.size = (u32) rc;
103
104
  s->buffer.pos = 0;
104
- left = rc;
105
+ left = (u32) rc;
105
106
  }
106
107
  left = left > req ? req : left;
107
108
  s_append(s,s->buffer.data + s->buffer.pos,left);
@@ -113,8 +114,8 @@ static inline int s_read_stream(sereal_t *s, u32 end) {
113
114
 
114
115
  static inline void *s_get_p_at_pos(sereal_t *s, u32 pos,u32 req) {
115
116
  // returning s->data[pos], so we just make size count from 0
116
- if (pos + req >= s->size) {
117
- if (s->flags & __STREAM) {
117
+ if (unlikely(pos + req >= s->size)) {
118
+ if (unlikely(s->flags & __STREAM)) {
118
119
  if (s_read_stream(s,pos + req + 1) < 0) {
119
120
  s_raise(s,rb_eRangeError,"stream request for %d bytes failed (err: %s)",
120
121
  req,strerror(errno));
@@ -191,7 +192,7 @@ static inline void s_set_flag_at_pos(sereal_t *s, u32 pos, u8 flag) {
191
192
  }
192
193
 
193
194
  static void b_dump(u8 *p, u32 len, u32 pos) {
194
- int i;
195
+ u32 i;
195
196
 
196
197
  fprintf(stderr,"\n-----------\n");
197
198
  for (i = 0; i < len; i++) {
@@ -107,7 +107,7 @@ static VALUE s_read_rb_string_bang(sereal_t *s,u8 t) {
107
107
  // len - 1: we also use the current byte, similar to u32,float.. casts
108
108
  #define RETURN_STRING(fx_l,fx_gen) \
109
109
  do { \
110
- len = fx_l; \
110
+ len = (u32) fx_l; \
111
111
  char *ptr = (char *) (len == 0 ? 0 : s_get_p_req_inclusive(s,len)); \
112
112
  string = fx_gen; \
113
113
  s_shift_position_bang(s,len); \
@@ -186,7 +186,7 @@ static VALUE s_read_ref(sereal_t *s, u8 tag) {
186
186
 
187
187
  #define TRAVEL(s,__stored) \
188
188
  do { \
189
- u32 offset = s_get_varint_bang(s) - 1; \
189
+ u32 offset = ((u32) s_get_varint_bang(s)) - 1; \
190
190
  __stored = s->pos; \
191
191
  s->pos = offset + s->hdr_end; \
192
192
  SD(s,"going back offset: %d, stored position: %d (tag: %d) new pos: %d",offset,stored_pos,tag,s->pos); \
@@ -272,7 +272,7 @@ VALUE sereal_to_rb_object(sereal_t *s) {
272
272
 
273
273
  VALUE decoded = (*READERS[t])(s,t);
274
274
 
275
- if (tracked) {
275
+ if (unlikely(tracked)) {
276
276
  s_init_tracker(s);
277
277
  SD(s,"tracking object of class: %s(id: %d) at position: %d",rb_obj_classname(decoded),FIX2INT(rb_obj_id(decoded)),pos);
278
278
  VALUE v_pos = INT2FIX(pos);
@@ -346,8 +346,8 @@ again:
346
346
  }
347
347
 
348
348
  u32 magic = s_get_u32_bang(s);
349
- if (magic != SRL_MAGIC_STRING_UINT_LE)
350
- s_raise(s,rb_eTypeError,"invalid header: %d (%x)",magic,magic);
349
+ if (magic != SRL_MAGIC_STRING_UINT_LE && magic != SRL_MAGIC_STRING_UINT_LE_HB)
350
+ s_raise(s,rb_eTypeError,"invalid header: %d (%x) vs %x",magic,magic,SRL_MAGIC_STRING_UINT_LE);
351
351
 
352
352
  u8 version = s_get_u8_bang(s);
353
353
  u8 suffix = s_get_varint_bang(s);
@@ -13,9 +13,7 @@ static VALUE s_read_hash(sereal_t *s, u8 tag);
13
13
  static VALUE s_read_hashref(sereal_t *s, u8 tag);
14
14
  static VALUE s_read_rb_string_bang(sereal_t *s,u8 t);
15
15
  static VALUE s_read_next_rb_string_bang(sereal_t *s);
16
- static VALUE s_read_object(sereal_t *s,u8 tag);
17
16
  static VALUE s_read_regexp(sereal_t *s, u8 tag);
18
- static VALUE s_read_sym(sereal_t *s,u8 tag);
19
17
  static VALUE s_read_nil(sereal_t *s, u8 tag);
20
18
  static VALUE s_read_true(sereal_t *s, u8 tag);
21
19
  static VALUE s_read_false(sereal_t *s, u8 tag);
@@ -45,8 +45,7 @@ static void rb_object_to_sereal(sereal_t *s, VALUE object);
45
45
  static void s_append_varint(sereal_t *s,u64 n);
46
46
  static void s_append_hdr_with_varint(sereal_t *s,u8 hdr, u64 n);
47
47
  static void s_append_zigzag(sereal_t *s,long long n);
48
- static void s_append_string(sereal_t *s,u8 *string, u32 len,u8 is_utf8);
49
- static void s_append_rb_string(sereal_t *s, VALUE object);
48
+ static void s_append_string(sereal_t *s, VALUE object);
50
49
  static void s_append_array(sereal_t *s, VALUE object);
51
50
  static void s_append_hash(sereal_t *s, VALUE object);
52
51
  static void s_append_symbol(sereal_t *s, VALUE object);
@@ -70,7 +69,7 @@ void s_init_writers(void) {
70
69
  WRITER[T_FLOAT] = s_append_double;
71
70
  WRITER[T_OBJECT] = s_append_object;
72
71
  WRITER[T_REGEXP] = s_append_regexp;
73
- WRITER[T_STRING] = s_append_rb_string;
72
+ WRITER[T_STRING] = s_append_string;
74
73
  WRITER[T_ARRAY] = s_append_array;
75
74
  WRITER[T_HASH] = s_append_hash;
76
75
  WRITER[T_SYMBOL] = s_append_symbol;
@@ -84,13 +83,14 @@ static void s_default_writer(sereal_t *s, VALUE object) {
84
83
  s_raise(s,rb_eTypeError, "invalid type for input %s",rb_obj_classname(object));
85
84
  }
86
85
 
87
-
88
86
  static inline void s_append_varint(sereal_t *s,u64 n) {
89
- while (n >= 0x80) {
90
- s_append_u8(s,((n & 0x7f) | 0x80));
91
- n >>= 7;
87
+ s_prepare(s,SRL_MAX_VARINT_LENGTH+1);
88
+ while (likely(n >= 0x80)) {
89
+ s->data[s->size++] = (n & 0x7f) | 0x80;
90
+ n = n >> 7;
92
91
  }
93
- s_append_u8(s,n);
92
+ s->data[s->size++] = n;
93
+ s->pos = s->size;
94
94
  }
95
95
 
96
96
  static inline void s_append_hdr_with_varint(sereal_t *s,u8 hdr, u64 n) {
@@ -102,28 +102,23 @@ static inline void s_append_zigzag(sereal_t *s,long long n) {
102
102
  s_append_hdr_with_varint(s,SRL_HDR_ZIGZAG,(n << 1) ^ (n >> 63));
103
103
  }
104
104
 
105
- static inline void s_append_string(sereal_t *s,u8 *string, u32 len,u8 is_utf8) {
106
- if (is_utf8) {
107
- s_append_hdr_with_varint(s,SRL_HDR_STR_UTF8,len);
108
- } else {
109
- if (len < SRL_MASK_SHORT_BINARY_LEN) {
105
+ static void s_append_string(sereal_t *s, VALUE object) {
106
+ u32 len = (u32) RSTRING_LEN(object);
107
+ if (likely(is_ascii_string(object))) {
108
+ if (likely(len < SRL_MASK_SHORT_BINARY_LEN)) {
110
109
  s_append_u8(s,SRL_HDR_SHORT_BINARY_LOW | (u8)len);
111
110
  } else {
112
111
  s_append_hdr_with_varint(s,SRL_HDR_BINARY,len);
113
112
  }
113
+ } else {
114
+ s_append_hdr_with_varint(s,SRL_HDR_STR_UTF8,len);
114
115
  }
115
- s_append(s,string,len);
116
- }
117
-
118
- static void s_append_rb_string(sereal_t *s, VALUE object) {
119
- s_append_string(s,(u8 *)RSTRING_PTR(object),
120
- RSTRING_LEN(object),
121
- (is_ascii_string(object) ? FALSE : TRUE));
116
+ s_append(s,(u8 *)RSTRING_PTR(object),len);
122
117
  }
123
118
 
124
119
  #define REF_THRESH(thresh,low,high) \
125
120
  do { \
126
- if (len < (thresh)) \
121
+ if (unlikely(len < (thresh))) \
127
122
  s_append_u8(s, low | (u8) len); \
128
123
  else \
129
124
  s_append_hdr_with_varint(s,high,len); \
@@ -139,7 +134,7 @@ static void s_append_array(sereal_t *s, VALUE object) {
139
134
 
140
135
 
141
136
  int s_hash_foreach(VALUE key, VALUE value, VALUE sereal_t_object) {
142
- if (key == Qundef)
137
+ if (unlikely(key == Qundef))
143
138
  return ST_CONTINUE;
144
139
  rb_object_to_sereal((sereal_t *) sereal_t_object,key);
145
140
  rb_object_to_sereal((sereal_t *) sereal_t_object,value);
@@ -156,8 +151,7 @@ static void s_append_hash(sereal_t *s, VALUE object) {
156
151
  convert symbols to strings
157
152
  */
158
153
  static void s_append_symbol(sereal_t *s, VALUE object) {
159
- VALUE string = rb_sym_to_s(object);
160
- s_append_rb_string(s,string);
154
+ s_append_string(s,rb_sym_to_s(object));
161
155
  }
162
156
 
163
157
  static void s_append_copy(sereal_t *s, u8 tag,VALUE object) {
@@ -166,11 +160,11 @@ static void s_append_copy(sereal_t *s, u8 tag,VALUE object) {
166
160
  }
167
161
 
168
162
  static VALUE s_copy_or_keep_in_mind(sereal_t *s, VALUE object, u8 offset) {
169
- if (s->copy == Qnil)
163
+ if (likely(s->copy == Qnil))
170
164
  return Qnil;
171
165
 
172
166
  VALUE stored_position = rb_hash_lookup(s->copy,object);
173
- if (stored_position == Qnil)
167
+ if (unlikely(stored_position == Qnil))
174
168
  rb_hash_aset(s->copy,object,INT2FIX(s->pos + offset));
175
169
  return stored_position;
176
170
  }
@@ -193,7 +187,7 @@ static void s_append_object(sereal_t *s, VALUE object) {
193
187
  s_append_copy(s,SRL_HDR_OBJECTV_FREEZE,copy);
194
188
  } else {
195
189
  s_append_u8(s,SRL_HDR_OBJECT_FREEZE);
196
- s_append_rb_string(s,klass);
190
+ s_append_string(s,klass);
197
191
  }
198
192
  VALUE frozen = rb_funcall(object,FREEZE,1,ID2SYM(SEREAL));
199
193
  if (TYPE(frozen) != T_ARRAY)
@@ -223,7 +217,7 @@ static void s_append_regexp(sereal_t *s, VALUE object) {
223
217
  #else
224
218
  pattern = rb_enc_str_new(RREGEXP_SRC_PTR(object),RREGEXP_SRC_LEN(object), enc);
225
219
  #endif
226
- s_append_rb_string(s,pattern);
220
+ s_append_string(s,pattern);
227
221
 
228
222
  int flags = rb_reg_options(object);
229
223
  VALUE f = rb_str_new("",0);
@@ -233,18 +227,18 @@ static void s_append_regexp(sereal_t *s, VALUE object) {
233
227
  rb_str_cat(f,"x",1);
234
228
  if (flags & MULTILINE)
235
229
  rb_str_cat(f,"m",1);
236
- s_append_rb_string(s,f);
230
+ s_append_string(s,f);
237
231
  }
238
232
 
239
233
  #define I_APPEND(_v) \
240
234
  do { \
241
- if (_v >= 0) { \
242
- if (_v < 16) \
235
+ if (likely(_v >= 0)) { \
236
+ if (unlikely(_v < 16)) \
243
237
  s_append_u8(s,SRL_HDR_POS_LOW | (u8) _v); \
244
238
  else \
245
- s_append_hdr_with_varint(s,SRL_HDR_VARINT,_v); \
239
+ s_append_hdr_with_varint(s,SRL_HDR_VARINT,(u64)_v); \
246
240
  } else { \
247
- if (_v > -17) \
241
+ if (unlikely(_v > -17)) \
248
242
  s_append_u8(s,SRL_HDR_NEG_LOW | ((u8) _v + 32)); \
249
243
  else \
250
244
  s_append_zigzag(s,_v); \
@@ -252,12 +246,12 @@ static void s_append_regexp(sereal_t *s, VALUE object) {
252
246
  } while(0)
253
247
 
254
248
  static void s_append_fixnum(sereal_t *s, VALUE object) {
255
- long long v = FIXNUM_P(object) ? FIX2LONG(object) : NUM2LL(object);
249
+ long long v = likely(FIXNUM_P(object)) ? FIX2LONG(object) : NUM2LL(object);
256
250
  I_APPEND(v);
257
251
  }
258
252
 
259
253
  static void s_append_bignum(sereal_t *s, VALUE object) {
260
- if (RBIGNUM_POSITIVE_P(object)) {
254
+ if (likely(RBIGNUM_POSITIVE_P(object))) {
261
255
  unsigned long long uv = rb_big2ull(object);
262
256
  I_APPEND(uv);
263
257
  } else {
@@ -292,7 +286,7 @@ static void s_append_refp(sereal_t *s, VALUE object) {
292
286
  static void rb_object_to_sereal(sereal_t *s, VALUE object) {
293
287
  S_RECURSE_INC(s);
294
288
  u32 pos = s->pos;
295
- if (COMPLEX(object)) {
289
+ if (unlikely(s->tracked != Qnil || s->copy != Qnil) && likely(COMPLEX(object))) {
296
290
  VALUE stored;
297
291
  if (s->tracked != Qnil) {
298
292
  VALUE id = rb_obj_id(object);
@@ -331,7 +325,6 @@ void fixup_varint_from_to(u8 *varint_start, u8 *varint_end, u32 number) {
331
325
  }
332
326
  }
333
327
 
334
-
335
328
  /*
336
329
  * Encodes object into Sereal
337
330
  */
@@ -440,8 +433,7 @@ method_sereal_encode(VALUE self, VALUE args) {
440
433
  s->size = compressed_len + s_header_len + compressed_len_varint + un_compressed_len_varint;
441
434
  s->pos = s->size;
442
435
  }
443
-
444
- VALUE result = rb_str_new((char *) s->data,s->size);
436
+ VALUE ret = rb_str_new(s->data,s->size);
445
437
  s_destroy(s);
446
- return result;
438
+ return ret;
447
439
  }
@@ -1,5 +1,6 @@
1
1
  #define SRL_MAGIC_STRING "=srl" /* Magic string for header. Every packet starts with this */
2
2
  #define SRL_MAGIC_STRING_UINT_LE 0x6c72733d /* SRL_MAGIC_STRING as a little endian integer */
3
+ #define SRL_MAGIC_STRING_UINT_LE_HB 0x6c72f33d /* "=\xF3rl" */
3
4
 
4
5
  #define SRL_PROTOCOL_VERSION ( 2 ) /* this is the first. for some reason we did not use 0 */
5
6
  #define SRL_PROTOCOL_VERSION_BITS ( 4 ) /* how many bits we use for the version, the rest go to the encoding */
@@ -13,6 +13,12 @@ typedef uint16_t u16;
13
13
  typedef uint8_t u8;
14
14
  typedef struct _sereal sereal_t;
15
15
 
16
+ #ifndef __GNUC__
17
+ #define __builtin_expect(a,b) a
18
+ #endif
19
+ #define likely(x) __builtin_expect(!!(x), 1)
20
+ #define unlikely(x) __builtin_expect(!!(x), 0)
21
+ #define SRL_MAX_VARINT_LENGTH 11
16
22
  #define TRUE 1
17
23
  #define FALSE 0
18
24
  #define MAX_RECURSION_DEPTH 100
@@ -46,11 +52,11 @@ typedef struct _sereal sereal_t;
46
52
  #define SD(s,fmt,arg...) \
47
53
  do { \
48
54
  if (s->flags & __DEBUG) { \
49
- int i; \
55
+ u32 i; \
50
56
  for (i = 0; i < s->level; i++) { \
51
57
  printf(" "); \
52
58
  } \
53
- D(fmt " { p: %d, s: %d, l: %u, h: %u } ",##arg,s->pos,s->size,s->level,s->hdr_end); \
59
+ D(fmt " { p: %u, s: %u, l: %u, h: %u } ",##arg,s->pos,s->size,s->level,s->hdr_end); \
54
60
  } \
55
61
  } while(0);
56
62
 
@@ -88,6 +94,7 @@ extern ID TO_SRL;
88
94
  extern ID SEREAL;
89
95
  extern ID ID_CLASS;
90
96
  extern ID ID_VALUE;
97
+ extern VALUE Sereal;
91
98
  extern VALUE SerealPerlObject;
92
99
 
93
100
  #define S_RECURSE_INC(s) \
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sereal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Borislav Nikolov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-05 00:00:00.000000000 Z
11
+ date: 2014-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler