Bacancy Bacancy
      • About Company
      • Resources

      About Company

      About Us Leadership Team Customer Reviews Awards & Recognition
      Infrastructure Our Locations Partnership

      Resources

      Press Room Blog Insights
      We are great place to work certified™

      Building and Sustaining High-Trust, High-Performance Culture

      Get Quote
    • Engagement Models

      Hiring Software Developers becomes easier with just a few clicks.

      Software Development Outsourcing

      End-to-end delivery of custom solutions aligned to your roadmap.

      Staff Augmentation

      Scale your in-house team with pre-vetted specialists on demand.

      Dedicated Teams

      Get dedicated engineers who work exclusively on your project.

      • Enterprise Services
      • IT Services
      • Data Analytics
      • Cloud Services
      • AI & ML
      • Platforms

      Enterprise Services

      Digital Transformation Business Process Automation Digital Product Engineering Enterprise App Development Custom Software Development

      IT Services

      Legacy App Modernization DevOps & SRE Full Stack Development AI Testing & QA Automation

      Data Analytics

      Data Visualization & Reporting Data Engineering & Pipelines Data Science & Predictive Analytics Business Intelligence

      Cloud Services

      Cloud Strategy & Consulting Cloud Migration & Modernization Multi Cloud Management

      AI & ML

      AI Development Agentic AI Generative AI Computer Vision Machine Learning & MLOps

      Platforms

      Salesforce SAP ServiceNow Microsoft Dynamics Snowflake
      High-quality, Cost-effective IT Outsourcing

      Schedule a free discovery session to explore your needs and find tailored solutions with no obligation.

      explore all services
    • Industries
      Healthcare Fintech Real Estate
      Logistics Education Retail & Ecommerce
      Let's Grow Together! Get Quote
      • Front End
      • Backend
      • Mobile
      • Databases
      • DevOps & Infra
      • AI & Data Stack
      • Vibe Coding

      Front End

      React.js Next.js Angular Vue.js TypeScript
      Your Very Own UI/UX Architects

      Experience smooth navigation and user-friendly designs with our front-end expertise.

      Hire Frontend Developer

      Backend

      Node.js Python Java Spring Boot Laravel .NET C# Golang FastAPI
      Server Solutions To Change Power Dynamics

      Transform your data into digital experiences with optimized coding standards.

      Hire Backend Developer

      Mobile

      iOS Android Flutter React Native
      Innovating Mobile-Friendly App Solutions

      Create dynamic mobile apps that make your brand stand out from the crowd.

      Hire Mobile App Developer

      Databases

      PostgreSQL MongoDB MySQL Redis Supabase
      Dedicated Talent With Skilled Approach

      Bring your digital visions to life with a hired resource at your convenience.

      Hire Dedicated Developer

      DevOps & Infra

      AWS Azure Google Cloud Docker Kubernetes Terraform
      Redefining Scalable Digital Infrastructures

      Make your data accessible worldwide at will, and leave the stress behind.

      Get Quote

      AI & Data Stack

      OpenAI LangChain LlamaIndex Apache Spark Airflow Tableau PowerBI Databricks
      Guiding Decisions With Data-Driven Insights

      Transition from your gut calls to actionable insights with our rich Data Science expertise.

      Get Quote

      Vibe Coding

      Base44 Claude Code Cursor Lovable Github Copilot
      Your AI-Native Development Team

      Skip the boilerplate. Our vibe coding experts use AI-first tools to go from prompt to product, fast.

      Hire Vibe Coding Developer
  • Case Studies
  • Contact Us
Find a Developer book a 30 min call
      • About Us
      • Leadership Team
      • Customer Reviews
      • Awards & Recognition
      • Infrastructure
      • Our Locations
      • Partnership
      • Press Room
      • Blog
      • Insights
      • Digital Transformation
      • Business Process Automation
      • Digital Product Engineering
      • Enterprise App Development
      • Custom Software Development
      • Legacy App Modernization
      • DevOps & SRE
      • Full Stack Development
      • AI Testing & QA Automation
      • Data Visualization & Reporting
      • Data Engineering & Pipelines
      • Data Science & Predictive Analytics
      • Business Intelligence
      • Cloud Strategy & Consulting
      • Cloud Migration & Modernization
      • Multi Cloud Management
      • AI Development
      • Agentic AI
      • Generative AI
      • Computer Vision
      • Machine Learning & MLOps
      • Salesforce
      • SAP
      • ServiceNow
      • Microsoft Dynamics
      • Snowflake
    • Healthcare
    • Fintech
    • Real Estate
    • Logistics
    • Education
    • Retail & Ecommerce
      • React.js
      • Next.js
      • Angular
      • Vue.js
      • TypeScript
      • Hire Frontend Developer
      • Node.js
      • Python
      • Java
      • Spring Boot
      • Laravel
      • .NET
      • C#
      • Golang
      • FastAPI
      • Hire Backend Developer
      • iOS
      • Android
      • Flutter
      • React Native
      • Hire Mobile App Developer
      • PostgreSQL
      • MongoDB
      • MySQL
      • Redis
      • Supabase
      • Hire Dedicated Developer
      • AWS
      • Azure
      • Google Cloud
      • Docker
      • Kubernetes
      • Terraform
      • Get Quote
      • OpenAI
      • LangChain
      • LlamaIndex
      • Apache Spark
      • Airflow
      • Tableau
      • PowerBI
      • Databricks
      • Get Quote
      • Base44
      • Claude Code
      • Cursor
      • Lovable
      • Github Copilot
      • Hire Vibe Coding Developer
  • Case Studies
  • 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 goodfirms review 4.8
bacancy goodfirms review
iso
  • Bacancy Behance
  • Bacancy Pinterest

Copyright © 2026 BACANCY SERVICES PRIVATE LIMITED All rights reserved.