Bacancy Bacancy
  • Our Engagement Models

    We offer flexible engagement models tailored to your project scope and timeline.

    Software Development Outsourcing
    Staff Augmentation
    Dedicated Teams
    Engagement Models

    High-Quality, Affordable IT Outsourcing

    Explore All Services

    AI & ML

    • AI Strategy & Roadmap
    • AI Development
    • Generative AI
    • AI Agent Development
    • Machine Learning
    • Computer Vision
    • LLM Development
    • RAG Development

    Data

    • Data Engineering
    • Data Analytics & BI
    • Data Science
    • Data Warehouse
    • Data Lake
    • Data Governance

    Cloud

    • Cloud Consulting
    • Cloud Migration
    • AWS Services
    • Azure Services
    • GCP Services
    • Kubernetes
    • DevOps

    Product Engineering

    • Full Stack Development
    • Custom Software Development
    • SaaS Development
    • App Modernization
    • Mobile App Development
    • Web Development
    • UI/UX Design

    Platforms

    • Salesforce
    • SAP
    • ServiceNow
    • Microsoft Dynamics
    • Snowflake
    • Databricks
  • Why Bacancy

    14+ years building domain-specific products for regulated and high-growth industries worldwide.

    Global delivery, every time zone

    Global delivery, every time zone

    1050+ vetted engineers

    1050+ vetted engineers

    Onboard in 48 hours

    Onboard in 48 hours

    Explore Our Case Studies

    Industries

    • Healthcare
    • Finance
    • eCommerce
    • Logistics
    • Fintech
    • Real Estate
    • Education
    • Insurance
    How Bacancy Built a Custom Epic EHR Solution to Automate Clinical Workflows

    How Bacancy Built a Custom Epic EHR Solution to Automate Clinical Workflows

    Read case study
  • About.

    1050+ world-class tech experts. One standard: customer satisfaction.

    About Company

    About Company

    • About Us
    • Leadership Team
    • Customer Reviews
    • Infrastructure
    • Our Locations
    • Partnership

    Resources

    • Press Room
    • Blog
    • Insights
    ISO/IEC 27001:2022 Certified

    ISO/IEC 27001:2022 Certified

    Built for enterprise security and compliance.

    Get Quote
  • Technologies
  • Customer Stories
  • Contact Us
Find a Developer
book a 30 min call
  • AI Strategy & Roadmap
  • AI Development
  • Generative AI
  • AI Agent Development
  • Machine Learning
  • Computer Vision
  • LLM Development
  • RAG Development
  • Data Engineering
  • Data Analytics & BI
  • Data Science
  • Data Warehouse
  • Data Lake
  • Data Governance
  • Cloud Consulting
  • Cloud Migration
  • AWS Services
  • Azure Services
  • GCP Services
  • Kubernetes
  • DevOps
  • Full Stack Development
  • Custom Software Development
  • SaaS Development
  • App Modernization
  • Mobile App Development
  • Web Development
  • UI/UX Design
  • Salesforce
  • SAP
  • ServiceNow
  • Microsoft Dynamics
  • Snowflake
  • Databricks
  • Healthcare
  • Finance
  • eCommerce
  • Logistics
  • Fintech
  • Real Estate
  • Education
  • Insurance
  • About Us
  • Leadership Team
  • Customer Reviews
  • Infrastructure
  • Our Locations
  • Partnership
  • Press Room
  • Blog
  • Insights

Technologies

Customer Stories

Contact Us

Find a Developer
book a 30 min call

A Real-World need for C extension in the Ruby

osc-timestamps-ntp
Last Updated on March 5, 2025 | Written By: Tanay Sharma

I seriously want to start this relationship with you, the readership with pure honesty: I completely agree with the incredibility of C. However, before we get stuck into the detail, do you have any idea about C extension and why you should write one in 2016?

Here’s a discussion of a real-world need for a C extension in the Ruby language and how a non-C developer can get it done right.

Why would you want to write one in 2016?

There are two reasons behind it. One of them is speed and another is leverage. If there is a well defined problem and if there is a need for speed, then writing an extension can be a way to go.

Writing C Extension….

Sharing my personal experience, writing C extension is a tough job and it an it advisable to go with FFI – don’t use it, until you have no other option. But, frankly speaking, I was scared of writing C into widely used application.

Apart from FFI, there were great options like for writing Ruby Extensions in Go, Crystal, Rust and many other languages where C bindings can be exposed. Anyways All I wanted to do is Go with C extension.

clean C - rtosc

Considering several OSC and canonical implementations, it made me use macros and structs,which I didn’t completely understand at the time. I was about to give up and fortunately I stumbled on the rtosc library. It is consisted with two files rtosc.h and rtosc.c. Wishing the best spirit of open source, I enquired on GitHub issue and within few hours got a thoughtful and polite response -viva la internet!

This helped me to try writing a C extension and trust me I’m glad that I did.

Source Code – https://www.xavierriley.co.uk/writing-a-c-extension-for-ruby-in-2016/”sif”, [“This is a string”, 123, 3.14]

So here you can have a look at the type of the arguments corresponds to a tag – s for a string, i for an int and for the Ruby API I decided to hide this so you do not consider about types.

>> FastOsc.encode_single_message("/foo", ["baz", 1, 2.0])
=> "/foo\x00\x00\x00\x00,sif\x00\x00\x00\x00baz\x00\x00\x00\x00\x01@\x00\x00\x00"

It’s a tag string and an output array of the encoded versions.

Partner with us to turbocharge your entrepreneurial journey with a master-blaster Rails Mobile App.
The ideal Ruby on Rails Development Company Awaits you!

Ruby’s C API


Working out the type

switch(TYPE(current_arg))

Ints and Longs

case T_FIXNUM

Floats

case T_FLOAT:

To convert from Ruby to C NUM2DBL

Strings and Symbols

case T_STRING:

To convert Ruby symbols use rb_sym_to_s().

Unicode Strings

I had broken Unicode strings. However it needs to be fixed so did my job this way.

string_arg = rb_str_new2(string_from_c);  

enc = rb_enc_find_index("UTF-8");  

rb_enc_associate_index(string_arg, enc);  

Ruby Time objects

Start with the object type

case T_DATA:

It means you have Ruby object and you can specifically check using

if (CLASS_OF(current_arg) == rb_cTime)

OSC Timestamps (NTP)

#define JAN_1970 2208988800.0     /* 2208988800 time from 1900 to 1970 in seconds */

uint64_t ruby_time_to_osc_timetag(VALUE rubytime) {  
  uint64_t timetag;
  double floattime;
  uint32_t sec;
  uint32_t frac;

  switch(TYPE(rubytime)) {
    case T_NIL:
      timetag = 1;
      break;
    default:
      // convert Time object to ntp
      floattime = JAN_1970 + NUM2DBL(rb_funcall(rubytime, rb_intern("to_f"), 0));

      sec = floor(floattime);
      frac = (uint32_t)(fmod(floattime, 1.0) * 4294967296); // * (2 ** 32)
      // printf("\nsec: %04x\n", sec);
      // printf("\nfrac: %04x\n", frac);
      timetag = (uint64_t)((uint64_t)sec << 32 | (uint64_t)frac);
      // printf("\ntimetag: %08llx\n", timetag);
      break;
  }

  return timetag;
}

further reading

Encoding  
-------------------------------------------------
            fast_osc    909.680k (±21.4%) i/s -      4.328M

             samsosc    271.908k (±19.3%) i/s -      1.327M

             oscruby     94.678k (±14.5%) i/s -    468.968k

Decoding  
-------------------------------------------------
            fast_osc      2.635M (±22.2%) i/s -     12.435M

             samsosc    264.614k (±16.1%) i/s -      1.304M

             oscruby     36.362k (±16.3%) i/s -    179.622k

oscruby non-optimised library for Ruby.

samosc is hand rolled optimization for pure Ruby version and fast_osc is this C extension. So it is like 4x improvement in Encoding to OSC and a 10x improvement in Decoding over the fastest pure Ruby implementation. It was done completely in a successful way.

The code is available here: https://github.com/xavriley/fast_osc.

Inspired to write your own extension, or have any valuable feedback for my C extension. I would love to hear from you. I am @bacancytech Twitter. Thanks for reading.


Expand Your Digital Horizons With Us.

Start a new project or take an existing one to the next level. Get in touch to start small, scale-up, and go Agile.


Or
E-mail us : solutions@bacancy.com

Your Success Is Guaranteed !

Related Articles

Viral Parekh

April 14, 2026

Ruby on Rails

Integrating LLMs with Ruby on Rails: How Tech Leaders Can Build AI-Powered Systems

By : Viral Parekh

This blog provides a practical guide to integrating LLMs with Ruby on Rails for developing AI applications. It covers the...

Read More
Darshan Joshi

August 25, 2025

Web Development

Top Web Development Trends Not To Miss in 2026

By : Darshan Joshi

Web development is constantly undergoing transformative changes. Whether we talk about AI-driven coding tools, serverless architectures, or sustainable web practices,...

Read More
Darshan Joshi

June 23, 2025

Web Development

13 Top Web Development Challenges and How To Solve Them

By : Darshan Joshi

Web development can be considered an exciting, but intense ride of a rollercoaster – fast-paced and full of challenges. It...

Read More

Offices and Development Centers

Bacancy Ahmedabad Ahmedabad

15-16, Times Corporate Park, Thaltej, Ahmedabad, 380059

Bacancy Gandhinagar Gandhinagar

422-A, 4th Floor, Pragya Tower Road 11, Block 15, Zone 1, SEZ-PA Gandhinagar, 382355

Bacancy Hyderabad Hyderabad

Awfis, Level 1, N Heights, Plot No 38, Phase 2, Hitech City Hyderabad, 500081

Bacancy Mumbai Mumbai

18th Floor, Cyberone, opp. CIDCO Exhibition Centre, Sector 30, Vashi, Navi Mumbai, 400703

Bacancy Pune Pune

2nd FloorMarisoft-1, Marigold IT Park, Pune - 411014

Bacancy Bengaluru Bengaluru

Raheja Towers, 26/27, Mahatma Gandhi Rd, East Wing, Craig Park Layout, Ashok Nagar, Bengaluru, 560001

Global Presence

Bacancy New Jersey New Jersey

33 South Wood Ave, Suite 600, Iselin NJ 08830

Bacancy California California

535 Mission St 14th floor, San Francisco, CA 94105

Bacancy Massachusetts Massachusetts

501 Boylston St, Boston, MA 02116

Bacancy Florida Florida

4995 NW, 72nd Avenue, Suite 307, Miami, FL, 33166

Bacancy London London

90 York Wy, London N1 9AG, United Kingdom

Bacancy Ontario Ontario

71 Dawes Road, Brampton, On L6X 5N9, Toronto

Bacancy Australia Australia

351A Hampstead Rd, Northfield SA 5085

Bacancy UAE UAE

One Central 8th and 9th Floor - Trade Centre - Trade Centre 2 - Dubai - United Arab Emirates

Bacancy Sweden Sweden

Junkergatan 4, 126 53 Hagersten

Get in Touch

Great Place to Work

Get in Touch

cal-icon

Looking for expert advice?

Schedule a Expert Call


  • Brochure
  • Quality Assurance
  • Resources
  • Tutorials
  • Customer Reviews
  • Privacy Policy
  • FAQs
  • Press Room
  • Contact Us
  • Sitemap
  • Employee

bacancy google review 4.6
bacancy google review
bacancy clutch review 4.8
bacancy clutch review
bacancy glassdoor review 4.5
bacancy glassdoor review
bacancy goodfirms review 4.8
bacancy goodfirms review
iso
  • Bacancy Behance
  • Bacancy Pinterest

Copyright © 2026 BACANCY SERVICES PRIVATE LIMITED All rights reserved.