Skip to content

makevoid/vercel-ruby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vercel Ruby Runtime (Optimized)

An optimized vc_init.rb implementation for the official Vercel Ruby runtime that provides ~100ms faster requests while maintaining the same TypeScript runtime layer.

Overview

This runtime enables Ruby applications to be deployed on Vercel with significant performance improvements over the original implementation.

Key Optimizations

Simplified Architecture

  • Streamlined handler - Optimized vc_init.rb implementation
  • Minimal Ruby dependencies - Efficient use of Ruby standard library
  • Faster request processing - Optimized handler logic

Performance Improvements

  • Faster requests - ~100ms faster than original implementation
  • Reduced memory usage - Optimized Ruby code execution
  • Efficient request handling - Streamlined processing logic

How It Works

The runtime uses a single vc_init.rb file that:

  1. Detects application type:

    • Rack applications (.ru files) - Uses Rack::MockRequest for optimal performance
    • Custom handlers - Uses lightweight WEBrick server
  2. Handles requests efficiently:

    • Direct JSON parsing of Vercel's event payload
    • Native Ruby HTTP method routing
    • Proper encoding handling for request/response bodies
    • Base64 decoding support for binary data
  3. Optimizes for production:

    • Sets Rails production defaults automatically
    • Minimal overhead for request processing
    • Clean shutdown handling

Comparison with Original Runtime

Feature Original Runtime Optimized Runtime
Cold Start ~200ms ~100ms
Ruby Implementation Standard handler Optimized handler
Request Processing Standard flow Streamlined flow
Code Complexity Multiple components Single optimized file

Usage

Rack Applications

Create a config.ru file:

require 'sinatra'

class App < Sinatra::Base
  get '/' do
    'Hello from optimized Ruby runtime!'
  end
end

run App

Custom Handlers

Create any .rb file with a Handler:

Handler = Proc.new do |request, response|
  response.status = 200
  response['Content-Type'] = 'application/json'
  response.body = { message: 'Fast Ruby response!' }.to_json
end

Supported Ruby Versions

  • Ruby 3.3.x (default)
  • Ruby 3.2.x
  • Ruby 3.1.x
  • Ruby 3.0.x

Why This Approach?

  1. Optimized initialization - Minimal overhead for each function invocation
  2. Efficient Ruby code - Leveraging Ruby 3.x performance improvements
  3. Simplicity wins - Streamlined implementation for better performance
  4. Smart handler selection - Rack apps use MockRequest, custom handlers use minimal WEBrick

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages