• extconf.rb

    require 'mkmf'
    $CFLAGS << ' -O3 '
    $CFLAGS << ' -std=c99'
    create_makefile('rotate')
    
    

    rotate.h

    #ifndef ROTATE_H
    #define ROTATE_H
    /* Nothing to expose */
    #endif
    
    

    rotate.c

    #include "rotate.h"
    #include <ruby.h>
    #include <stdint.h>
    #ifdef __x86_64__
    #ifdef __linux__
    #include <x86intrin.h>
    #endif
    #endif
    /* Support more arch/cc if you want */
    
    static VALUE rb_mRotate;
    static VALUE rb_mRotate_Native;
    static VALUE rb_mRotate_Native_U64;
    
    static VALUE rotate_native_u64_rb_left(VALUE self, VALUE src, VALUE bits);
    
    void Init_rotate(void)
    {
        rb_mRotate = rb_define_module("Rotate");
        rb_mRotate_Native = rb_define_module_under(rb_mRotate, "Native");
        rb_mRotate_Native_U64 = rb_define_module_under(rb_mRotate_Native, "U64");
        rb_define_module_function(rb_mRotate_Native_U64, "left", rotate_native_u64_rb_left, 2);
    }
    
    static VALUE rotate_native_u64_rb_left(VALUE self, VALUE src, VALUE rot)
    {
        uint64_t source = NUM2ULL(src);
        uint64_t rotate = NUM2ULL(rot);
    
        return ULL2NUM(__rolq(source, rotate));
    }
    
    

    Run ruby extconf.rb && make

    test.rb

    require_relative 'rotate'
    
    puts Rotate::Native::U64.left(1,63)
    # => 9223372036854775808
    puts Rotate::Native::U64.left(1,64)
    # => 1
    
    

  • Intel Xeon E5-2680v2, Ivy Bridge, 20C/40T

    32G DRAM

    5.10.0-3-amd64 #1 SMP Debian 5.10.12-1 (2021-01-30) x86_64 GNU/Linux

    Ruby 3.0.0

    正常运行

    require 'digest/md5'
    e = (1..1_000_000).to_a
    arr = [e,e,e,e]
    
    result = arr.map { |sub_arr|
      Ractor.new(sub_arr) do |sub_arr|
        sub_arr.map {
          Digest::MD5.hexdigest(rand.to_s)
        }
      end
    }.map{|r| r.take}.reduce(:+)
    

    heap trace