Skip to content

Commit

Permalink
[ruby/prism] Make pm_integer -> Integer faster
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng authored and kddnewton committed Mar 7, 2024
1 parent 977012b commit 05526a4
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions prism/templates/ext/prism/api_node.c.erb
Expand Up @@ -39,12 +39,21 @@ pm_string_new(const pm_string_t *string, rb_encoding *encoding) {

static VALUE
pm_integer_new(const pm_integer_t *integer) {
VALUE result = UINT2NUM(integer->head.value);
size_t shift = 0;

for (const pm_integer_word_t *node = integer->head.next; node != NULL; node = node->next) {
VALUE receiver = rb_funcall(UINT2NUM(node->value), rb_intern("<<"), 1, ULONG2NUM(++shift * 32));
result = rb_funcall(receiver, rb_intern("|"), 1, result);
VALUE result;
if (integer->head.next) {
size_t length = integer->length + 1;
VALUE str = rb_str_new(NULL, length * 8);
unsigned char *buf = (unsigned char *)RSTRING_PTR(str);
size_t offset = length * 8;
for (const pm_integer_word_t *node = &integer->head; node != NULL; node = node->next) {
for (int i = 0; i < 8; i++) {
int n = (node->value >> (4 * i)) & 0xf;
buf[--offset] = n < 10 ? n + '0' : n - 10 + 'a';
}
}
result = rb_funcall(str, rb_intern("to_i"), 1, UINT2NUM(16));
} else {
result = UINT2NUM(integer->head.value);
}

if (integer->negative) {
Expand Down

0 comments on commit 05526a4

Please sign in to comment.