Skip to content

Instantly share code, notes, and snippets.

@chuckremes
Created September 15, 2010 23:53
Show Gist options
  • Select an option

  • Save chuckremes/581704 to your computer and use it in GitHub Desktop.

Select an option

Save chuckremes/581704 to your computer and use it in GitHub Desktop.
From 59a7556f7f127b54096b2fa15e3de80ff0afb566 Mon Sep 17 00:00:00 2001
From: Chuck Remes <cremes@mac.com>
Date: Wed, 15 Sep 2010 18:51:51 -0500
Subject: [PATCH] C ext fixes for better perf
---
ext/cbson/cbson.c | 24 +++++++++++-------------
1 files changed, 11 insertions(+), 13 deletions(-)
diff --git a/ext/cbson/cbson.c b/ext/cbson/cbson.c
index 17688b1..8f66b96 100644
--- a/ext/cbson/cbson.c
+++ b/ext/cbson/cbson.c
@@ -31,10 +31,6 @@
# define RSTRING_LEN(v) RSTRING(v)->len
#endif
-#ifndef RARRAY_PTR
-# define RARRAY_PTR(v) RARRAY(v)->ptr
-#endif
-
#ifndef RARRAY_LEN
# define RARRAY_LEN(v) RARRAY(v)->len
#endif
@@ -307,13 +303,12 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
}
items = RARRAY_LEN(value);
- values = RARRAY_PTR(value);
for(i = 0; i < items; i++) {
char* name;
VALUE key;
INT2STRING(&name, i);
key = rb_str_new2(name);
- write_element_with_id(key, values[i], pack_extra(buffer, check_keys));
+ write_element_with_id(key, rb_ary_entry(value, i), pack_extra(buffer, check_keys));
free(name);
}
@@ -394,7 +389,7 @@ static int write_element(VALUE key, VALUE value, VALUE extra, int allow_id) {
int i;
write_name_and_type(buffer, key, 0x07);
for (i = 0; i < 12; i++) {
- char byte = (char)FIX2INT(RARRAY_PTR(as_array)[i]);
+ char byte = (char)FIX2INT(rb_ary_entry(as_array, i));
SAFE_WRITE(buffer, &byte, 1);
}
break;
@@ -567,7 +562,7 @@ static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys, VALUE move_
VALUE keys = rb_funcall(hash, rb_intern("keys"), 0);
int i;
for(i = 0; i < RARRAY_LEN(keys); i++) {
- VALUE key = RARRAY_PTR(keys)[i];
+ VALUE key = rb_ary_entry(keys, i);
VALUE value = rb_hash_aref(hash, key);
write_function(key, value, pack_extra(buffer, check_keys));
@@ -873,16 +868,19 @@ static VALUE method_deserialize(VALUE self, VALUE bson) {
static VALUE fast_pack(VALUE self)
{
VALUE res;
- long i;
+ long i, len;
char c;
+ char *buf;
- res = rb_str_buf_new(0);
+ len = RARRAY_LEN(self);
+ buf = malloc(len * sizeof(char));
- for (i = 0; i < RARRAY_LEN(self); i++) {
- c = FIX2LONG(RARRAY_PTR(self)[i]);
- rb_str_buf_cat(res, &c, sizeof(char));
+ for (i = 0; i < len; i++) {
+ buf[i] = FIX2INT(rb_ary_entry(self, i));
}
+ res = rb_str_new(buf, len);
+
return res;
}
--
1.7.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment