i18nema 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +1 -1
  2. data/ext/i18nema/i18nema.c +26 -15
  3. data/lib/i18nema.rb +0 -6
  4. metadata +2 -2
data/README.md CHANGED
@@ -27,7 +27,7 @@ an initializer, just do `I18n.backend.init_translations`.
27
27
 
28
28
  ### Faster Startup
29
29
 
30
- Loading all the translations into memory is dramtically faster with
30
+ Loading all the translations into memory is dramatically faster with
31
31
  I18nema (about 4x). While this is just a one time hit, it's pretty
32
32
  noticeable when you're waiting on it (e.g. console, specs). In
33
33
  [canvas-lms](https://github/com/instructure/canvas-lms), I18nema brings
@@ -3,6 +3,8 @@
3
3
  #include "vendor/syck.h"
4
4
  #include "vendor/uthash.h"
5
5
 
6
+ #define CAN_FREE(item) item != NULL && item->type != i_type_true && item->type != i_type_false && item->type != i_type_null
7
+
6
8
  VALUE I18nema = Qnil,
7
9
  I18nemaBackend = Qnil,
8
10
  I18nemaBackendLoadError = Qnil;
@@ -18,7 +20,7 @@ static void delete_object_r(struct i_object *object);
18
20
  static VALUE normalize_key(VALUE self, VALUE key, VALUE separator);
19
21
 
20
22
  enum i_object_type {
21
- i_type_none,
23
+ i_type_unused,
22
24
  i_type_string,
23
25
  i_type_array,
24
26
  i_type_hash,
@@ -32,7 +34,7 @@ enum i_object_type {
32
34
 
33
35
  union i_object_data {
34
36
  char *string;
35
- struct i_object **array;
37
+ struct i_object *array;
36
38
  struct i_key_value *hash;
37
39
  };
38
40
 
@@ -92,7 +94,7 @@ array_to_rarray(i_object_t *array)
92
94
  {
93
95
  VALUE result = rb_ary_new2(array->size);
94
96
  for (unsigned long i = 0; i < array->size; i++)
95
- rb_ary_store(result, i, i_object_to_robject(array->data.array[i]));
97
+ rb_ary_store(result, i, i_object_to_robject(&array->data.array[i]));
96
98
  return result;
97
99
  }
98
100
 
@@ -167,13 +169,13 @@ empty_object(i_object_t *object, int recurse)
167
169
  case i_type_array:
168
170
  if (recurse)
169
171
  for (unsigned long i = 0; i < object->size; i++)
170
- delete_object_r(object->data.array[i]);
172
+ empty_object(&object->data.array[i], 1);
171
173
  xfree(object->data.array);
172
174
  break;
173
175
  case i_type_hash:
174
176
  delete_hash(&object->data.hash, recurse);
175
177
  break;
176
- case i_type_none:
178
+ case i_type_unused:
177
179
  break;
178
180
  default:
179
181
  xfree(object->data.string);
@@ -185,7 +187,7 @@ static void
185
187
  delete_object(i_object_t *object, int recurse)
186
188
  {
187
189
  empty_object(object, recurse);
188
- if (object->type != i_type_null && object->type != i_type_true && object->type != i_type_false)
190
+ if (CAN_FREE(object))
189
191
  xfree(object);
190
192
  }
191
193
 
@@ -248,8 +250,9 @@ static int
248
250
  delete_syck_st_entry(char *key, char *value, char *arg)
249
251
  {
250
252
  i_object_t *object = (i_object_t *)value;
251
- // key object whose string we have yoinked into a kv
252
- if (object->type == i_type_none)
253
+ // key object whose string we have yoinked into a kv, or item that
254
+ // has been copied into an array
255
+ if (object->type == i_type_unused)
253
256
  delete_object_r(object);
254
257
  return ST_DELETE;
255
258
  }
@@ -293,13 +296,19 @@ new_string(char *orig, long len)
293
296
  return str;
294
297
  }
295
298
 
296
- static i_object_t*
297
- new_string_object(char *str, long len)
299
+ static void
300
+ set_string_object(i_object_t *object, char *str, long len)
298
301
  {
299
- i_object_t *object = ALLOC(i_object_t);
300
302
  object->type = i_type_string;
301
303
  object->size = len;
302
304
  object->data.string = new_string(str, len);
305
+ }
306
+
307
+ static i_object_t*
308
+ new_string_object(char *str, long len)
309
+ {
310
+ i_object_t *object = ALLOC(i_object_t);
311
+ set_string_object(object, str, len);
303
312
  return object;
304
313
  }
305
314
 
@@ -309,7 +318,7 @@ new_array_object(long size)
309
318
  i_object_t *object = ALLOC(i_object_t);
310
319
  object->type = i_type_array;
311
320
  object->size = size;
312
- object->data.array = ALLOC_N(i_object_t*, size);
321
+ object->data.array = ALLOC_N(i_object_t, size);
313
322
  return object;
314
323
  }
315
324
 
@@ -372,7 +381,9 @@ handle_syck_node(SyckParser *parser, SyckNode *node)
372
381
  syck_lookup_sym(parser, oid, (void **)&item);
373
382
  if (item->type == i_type_string)
374
383
  current_translation_count++;
375
- result->data.array[i] = item;
384
+ memcpy(&result->data.array[i], item, sizeof(i_object_t));
385
+ if (CAN_FREE(item))
386
+ item->type = i_type_unused;
376
387
  }
377
388
  break;
378
389
  case syck_map_kind:
@@ -386,7 +397,7 @@ handle_syck_node(SyckParser *parser, SyckNode *node)
386
397
  syck_lookup_sym(parser, oid, (void **)&value);
387
398
 
388
399
  i_key_value_t *kv = new_key_value(key->data.string, value);
389
- key->type = i_type_none; // so we know to free this node in delete_syck_st_entry
400
+ key->type = i_type_unused; // so we know to free this node in delete_syck_st_entry
390
401
  if (value->type == i_type_string)
391
402
  current_translation_count++;
392
403
  add_key_value(&result->data.hash, kv);
@@ -538,7 +549,7 @@ normalize_key(VALUE self, VALUE key, VALUE separator)
538
549
  if (RSTRING_LEN(part) == 0)
539
550
  skipped++;
540
551
  else
541
- key_frd->data.array[i - skipped] = new_string_object(RSTRING_PTR(part), RSTRING_LEN(part));
552
+ set_string_object(&key_frd->data.array[i - skipped], RSTRING_PTR(part), RSTRING_LEN(part));
542
553
  }
543
554
  key_frd->size -= skipped;
544
555
 
data/lib/i18nema.rb CHANGED
@@ -42,12 +42,6 @@ module I18nema
42
42
  entry
43
43
  end
44
44
 
45
- alias_method :reload_old, :reload!
46
- def reload!
47
- debugger
48
- reload_old
49
- end
50
-
51
45
  protected
52
46
  def load_file(filename)
53
47
  type = File.extname(filename).tr('.', '').downcase
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18nema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-01 00:00:00.000000000 Z
12
+ date: 2013-06-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n