SlideShare une entreprise Scribd logo
1  sur  39
Télécharger pour lire hors ligne
GoFFIng around with Ruby
@gautamrege
@joshsoftware
FFI
Foreign Function
Interface
*In stdlib since Ruby 1.9.3
Why do we need FFI?
• Resque / Sidekiq / delayed_job
• 3rd party API requests
• Sending newsletters
• Heavy computation
• Fast searches, aggregation.
Why Go?
• Power of multi-core.
• Goroutines to the rescue
• Language is restricted focussed.
• Programming Ethics
• Compiled
If it compiles, it
works
What is FFI anyway?
• Language interoperability!
• Calling convention.
• libffi.so
• ruby-ffi
• Fiddle (in Stdlib since 1.9.3)
FFI or Good ol’ C-ext?
• FFI Easier to write and maintain.
• Gems are now easier to install -
don’t need build dependencies.
• Portable code
• No more “Building native
extensions…” while installing
gems.
Simple Ruby Code
def race(legs, benchmark)
tids = []
legs.times do |i|
tids << Thread.new(i) do |leg|
array = (1..benchmark).map { |i| i }
p "Done leg #{leg} benchmark #{benchmark}"
end
end
tids.each { |t| t.join }
end
race(4, 50_000_000)
Benchmark - Ruby
$ time ruby run1.rb
Done leg 1 benchmark 50000000
Done leg 0 benchmark 50000000
Done leg 3 benchmark 50000000
Done leg 2 benchmark 50000000
real 0m18.242s
user 0m16.626s
sys 0m1.292s
Simple Go Code
package main
import "fmt"
func race(leg int, benchmark int) {
arr := []int{}
for i := 0; i < benchmark; i++ {
arr = append(arr, i)
}
fmt.Println("Done leg:", leg)
}
func main() {
for i := 0; i < 4; i++ {
race(i, 50000000)
}
}
Simple Go Code
package main
import "fmt"
func race(leg int, benchmark int) {
arr := []int{}
for i := 0; i < benchmark; i++ {
arr = append(arr, i)
}
fmt.Println("Done leg:", leg)
}
func main() {
for i := 0; i < 4; i++ {
race(i, 50000000)
}
}
Simple Go Code
package main
import "fmt"
func race(leg int, benchmark int) {
arr := []int{}
for i := 0; i < benchmark; i++ {
arr = append(arr, i)
}
fmt.Println("Done leg:", leg)
}
func main() {
for i := 0; i < 4; i++ {
race(i, 50000000)
}
}
Simple Go Code
package main
import "fmt"
func race(leg int, benchmark int) {
arr := []int{}
for i := 0; i < benchmark; i++ {
arr = append(arr, i)
}
fmt.Println("Done leg:", leg)
}
func main() {
for i := 0; i < 4; i++ {
race(i, 50000000)
}
}
Benchmark - Go
$ time go run run1.go
Done leg: 0
Done leg: 1
Done leg: 2
Done leg: 3
real 0m2.774s
user 0m2.564s
sys 0m0.929s
Calling Go from Ruby
• Build a C library using Go code.
• Build a FFI extension in Ruby
• Call it !
c-shared Go library
package main
import (
"C"
"fmt"
)
func race(leg int, benchmark int) {
arr := []int{}
for i := 0; i < benchmark; i++ {
arr = append(arr, i)
}
fmt.Println("Done leg:", leg)
}
//export churn
func churn(leg C.int, benchmark C.int) {
for i := 0; i < int(leg); i++ {
race(i, int(benchmark))
}
}
func main() {
}
c-shared Go library
package main
import (
"C"
"fmt"
)
func race(leg int, benchmark int) {
arr := []int{}
for i := 0; i < benchmark; i++ {
arr = append(arr, i)
}
fmt.Println("Done leg:", leg)
}
//export churn
func churn(leg C.int, benchmark C.int) {
for i := 0; i < int(leg); i++ {
race(i, int(benchmark))
}
}
func main() {
}
c-shared Go library
package main
import (
"C"
"fmt"
)
func race(leg int, benchmark int) {
arr := []int{}
for i := 0; i < benchmark; i++ {
arr = append(arr, i)
}
fmt.Println("Done leg:", leg)
}
//export churn
func churn(leg C.int, benchmark C.int) {
for i := 0; i < int(leg); i++ {
race(i, int(benchmark))
}
}
func main() {
}
$ go build -o librun.so -buildmode=c-shared run2.go
Ruby FFI extension
require 'fiddle'
module Fun
def self.race(leg, benchmark)
librun = Fiddle.dlopen(‘./librun.so')
race = Fiddle::Function.new(
librun['churn'],
[Fiddle::TYPE_INT, Fiddle::TYPE_INT],
Fiddle::TYPE_VOID
)
race.call(leg, benchmark)
end
end
Call it!
require './fun'
Fun.race(4, 50_000_000)
$ time ruby run2.rb
Done leg: 0 Benchmark 50000000
Done leg: 1 Benchmark 50000000
Done leg: 2 Benchmark 50000000
Done leg: 3 Benchmark 50000000
real 0m2.776s
user 0m2.719s
sys 0m0.969s
Practical Example
Practical Example
Make Model Variant
Practical Example
Make Model Variant
Practical Example
Practical Example
Insurance
Vendor
Practical Example
Insurance
Vendor
68 columns
Doing the Math!
18 STATES
428 CARS
68 VENDORS
= 523,872 cells
If that was not enough …
For each of the
523,872 cells
Calculate Depreciation
Index in ElasticSearch
for cmake, v := range segments {
for model, j := range v {
for submodel, k := range j {
for insurance, segment := range k {
wg.Add(1)
go func(cmake string, model string,
submodel string, insurance string,
segment string) {
defer wg.Done()
// data := es_data{...}
// prepare_es_data(...)
es_add_depreciation(&data)
}(cmake, model, submodel, insurance, segment)
}
}
}
}
Practical Example
About 350,000 go-routines
for cmake, v := range segments {
for model, j := range v {
for submodel, k := range j {
for insurance, segment := range k {
wg.Add(1)
go func(cmake string, model string,
submodel string, insurance string,
segment string) {
defer wg.Done()
// data := es_data{...}
// prepare_es_data(...)
es_add_depreciation(&data)
}(cmake, model, submodel, insurance, segment)
}
}
}
}
Practical Example
About 350,000 go-routines
Performance Benefits
Performance Benefits
• Sidekiq: 2 GB memory & ~18 minutes
Performance Benefits
• Sidekiq: 2 GB memory & ~18 minutes
• Go FFI: 135 MB memory & 1 minute 13 seconds.
Performance Benefits
• Sidekiq: 2 GB memory & ~18 minutes
• Go FFI: 135 MB memory & 1 minute 13 seconds.
ElasticSearch
indexing took
48 seconds
Complexities of FFI
• Memory Management (alloc / free)
• Pointer management
• Fiddle::Pointer
• Fiddle::Closure
Don’t use them unless you HAVE to!
Resources
• Fiddle http://ruby-doc.org/stdlib-2.0.0/libdoc/fiddle/rdoc/
Fiddle.html
• FFI Core Concepts https://github.com/ffi/ffi/wiki/Core-
Concepts
• Github: ffi/ffi
• Effective Go ( https://golang.org/doc/effective_go.html )
• A Tour of Go ( https://tour.golang.org/welcome/1 )
@joshsoftware
@gautamrege
http://www.codecuriosity.org

Contenu connexe

Tendances

Hourglass Interfaces for C++ APIs - CppCon 2014
Hourglass Interfaces for C++ APIs - CppCon 2014Hourglass Interfaces for C++ APIs - CppCon 2014
Hourglass Interfaces for C++ APIs - CppCon 2014Stefanus Du Toit
 
Connecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with EmbindConnecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with EmbindChad Austin
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloadingRai University
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaN Masahiro
 
Take advantage of C++ from Python
Take advantage of C++ from PythonTake advantage of C++ from Python
Take advantage of C++ from PythonYung-Yu Chen
 
Mono + .NET Core = ❤️
Mono + .NET Core =  ❤️Mono + .NET Core =  ❤️
Mono + .NET Core = ❤️Egor Bogatov
 
Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020Yung-Yu Chen
 
[GSoC 2017] gopy: Updating gopy to support Python3 and PyPy
[GSoC 2017] gopy: Updating gopy to support Python3 and PyPy[GSoC 2017] gopy: Updating gopy to support Python3 and PyPy
[GSoC 2017] gopy: Updating gopy to support Python3 and PyPyDong-hee Na
 
Lambda Expressions in C++
Lambda Expressions in C++Lambda Expressions in C++
Lambda Expressions in C++Patrick Viafore
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12Jadavsejal
 
Porting legacy apps to Griffon
Porting legacy apps to GriffonPorting legacy apps to Griffon
Porting legacy apps to GriffonJames Williams
 
Machine learning with py torch
Machine learning with py torchMachine learning with py torch
Machine learning with py torchRiza Fahmi
 
Take Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play FrameworkTake Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play FrameworkAsher Glynn
 
PyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimePyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimeNational Cheng Kung University
 
Happy Go Programming Part 1
Happy Go Programming Part 1Happy Go Programming Part 1
Happy Go Programming Part 1Lin Yo-An
 
Php 7.x 8.0 and hhvm and
Php 7.x 8.0 and hhvm and Php 7.x 8.0 and hhvm and
Php 7.x 8.0 and hhvm and Pierre Joye
 

Tendances (20)

Hourglass Interfaces for C++ APIs - CppCon 2014
Hourglass Interfaces for C++ APIs - CppCon 2014Hourglass Interfaces for C++ APIs - CppCon 2014
Hourglass Interfaces for C++ APIs - CppCon 2014
 
Connecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with EmbindConnecting C++ and JavaScript on the Web with Embind
Connecting C++ and JavaScript on the Web with Embind
 
Blazing Fast Windows 8 Apps using Visual C++
Blazing Fast Windows 8 Apps using Visual C++Blazing Fast Windows 8 Apps using Visual C++
Blazing Fast Windows 8 Apps using Visual C++
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
 
D vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoyaD vs OWKN Language at LLnagoya
D vs OWKN Language at LLnagoya
 
Take advantage of C++ from Python
Take advantage of C++ from PythonTake advantage of C++ from Python
Take advantage of C++ from Python
 
Mono + .NET Core = ❤️
Mono + .NET Core =  ❤️Mono + .NET Core =  ❤️
Mono + .NET Core = ❤️
 
Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020Notes about moving from python to c++ py contw 2020
Notes about moving from python to c++ py contw 2020
 
[GSoC 2017] gopy: Updating gopy to support Python3 and PyPy
[GSoC 2017] gopy: Updating gopy to support Python3 and PyPy[GSoC 2017] gopy: Updating gopy to support Python3 and PyPy
[GSoC 2017] gopy: Updating gopy to support Python3 and PyPy
 
Lambda Expressions in C++
Lambda Expressions in C++Lambda Expressions in C++
Lambda Expressions in C++
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12
 
Porting legacy apps to Griffon
Porting legacy apps to GriffonPorting legacy apps to Griffon
Porting legacy apps to Griffon
 
Machine learning with py torch
Machine learning with py torchMachine learning with py torch
Machine learning with py torch
 
Take Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play FrameworkTake Flight - Using Fly with the Play Framework
Take Flight - Using Fly with the Play Framework
 
PyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtimePyPy's approach to construct domain-specific language runtime
PyPy's approach to construct domain-specific language runtime
 
Lecture05
Lecture05Lecture05
Lecture05
 
Happy Go Programming Part 1
Happy Go Programming Part 1Happy Go Programming Part 1
Happy Go Programming Part 1
 
Flamingo Core Concepts
Flamingo Core ConceptsFlamingo Core Concepts
Flamingo Core Concepts
 
Php 7.x 8.0 and hhvm and
Php 7.x 8.0 and hhvm and Php 7.x 8.0 and hhvm and
Php 7.x 8.0 and hhvm and
 
Queue oop
Queue   oopQueue   oop
Queue oop
 

En vedette

インフラ野郎AzureチームOCP Summit US 2017号外
インフラ野郎AzureチームOCP Summit US 2017号外インフラ野郎AzureチームOCP Summit US 2017号外
インフラ野郎AzureチームOCP Summit US 2017号外Toru Makabe
 
Qualité, bonnes pratiques et CMS - WordCamp Bordeaux - 18 mars 2017
Qualité, bonnes pratiques et CMS - WordCamp Bordeaux - 18 mars 2017Qualité, bonnes pratiques et CMS - WordCamp Bordeaux - 18 mars 2017
Qualité, bonnes pratiques et CMS - WordCamp Bordeaux - 18 mars 2017Elie Sloïm
 
[GUIDE] Vigilance sommeil - Guide prévention et santé
[GUIDE] Vigilance sommeil - Guide prévention et santé [GUIDE] Vigilance sommeil - Guide prévention et santé
[GUIDE] Vigilance sommeil - Guide prévention et santé AG2R LA MONDIALE
 
Secure development environment @ Meet Magento Croatia 2017
Secure development environment @ Meet Magento Croatia 2017Secure development environment @ Meet Magento Croatia 2017
Secure development environment @ Meet Magento Croatia 2017Anna Völkl
 
Diagnóstico SEO Técnico con Herramientas #TheInbounder
Diagnóstico SEO Técnico con Herramientas #TheInbounderDiagnóstico SEO Técnico con Herramientas #TheInbounder
Diagnóstico SEO Técnico con Herramientas #TheInbounderMJ Cachón Yáñez
 
B2B Marketing and The Power of Twitter
B2B Marketing and The Power of TwitterB2B Marketing and The Power of Twitter
B2B Marketing and The Power of TwitterSteve Yanor
 
Deploying Immutable infrastructures with RabbitMQ and Solr
Deploying Immutable infrastructures with RabbitMQ and SolrDeploying Immutable infrastructures with RabbitMQ and Solr
Deploying Immutable infrastructures with RabbitMQ and SolrJordi Llonch
 
TensroFlow XLA : JIT編 (r1.3版)
TensroFlow XLA : JIT編 (r1.3版)TensroFlow XLA : JIT編 (r1.3版)
TensroFlow XLA : JIT編 (r1.3版)Mr. Vengineer
 
Jeudis du Libre - MySQL comme Document Store
Jeudis du Libre - MySQL comme Document StoreJeudis du Libre - MySQL comme Document Store
Jeudis du Libre - MySQL comme Document StoreFrederic Descamps
 
第5回ue4ハンズオンセミナー
第5回ue4ハンズオンセミナー第5回ue4ハンズオンセミナー
第5回ue4ハンズオンセミナーMasahiko Nakamura
 
Understanding P2P
Understanding P2PUnderstanding P2P
Understanding P2Purbanlabs
 
DNSの運用を考える
DNSの運用を考えるDNSの運用を考える
DNSの運用を考えるaeoe
 
Master the flow of microservices - because your business is more complex than...
Master the flow of microservices - because your business is more complex than...Master the flow of microservices - because your business is more complex than...
Master the flow of microservices - because your business is more complex than...Bernd Ruecker
 
Solvent Effects on Chemical Reaction
Solvent Effects on Chemical ReactionSolvent Effects on Chemical Reaction
Solvent Effects on Chemical Reactionrita martin
 
Fortune 1000 HR Leader Survey Results
Fortune 1000 HR Leader Survey ResultsFortune 1000 HR Leader Survey Results
Fortune 1000 HR Leader Survey ResultsChuck Solomon
 
Startups & Gamification (LeanCamp Tehran 2016)
Startups & Gamification (LeanCamp Tehran 2016)Startups & Gamification (LeanCamp Tehran 2016)
Startups & Gamification (LeanCamp Tehran 2016)Alireza Ranjbar SHourabi
 
Discovery, Reuse, Research and Crowdsourcing: IIIF experiences from the NLW
Discovery, Reuse, Research and Crowdsourcing: IIIF experiences from the NLWDiscovery, Reuse, Research and Crowdsourcing: IIIF experiences from the NLW
Discovery, Reuse, Research and Crowdsourcing: IIIF experiences from the NLWGlen Robson
 

En vedette (20)

インフラ野郎AzureチームOCP Summit US 2017号外
インフラ野郎AzureチームOCP Summit US 2017号外インフラ野郎AzureチームOCP Summit US 2017号外
インフラ野郎AzureチームOCP Summit US 2017号外
 
Qualité, bonnes pratiques et CMS - WordCamp Bordeaux - 18 mars 2017
Qualité, bonnes pratiques et CMS - WordCamp Bordeaux - 18 mars 2017Qualité, bonnes pratiques et CMS - WordCamp Bordeaux - 18 mars 2017
Qualité, bonnes pratiques et CMS - WordCamp Bordeaux - 18 mars 2017
 
[GUIDE] Vigilance sommeil - Guide prévention et santé
[GUIDE] Vigilance sommeil - Guide prévention et santé [GUIDE] Vigilance sommeil - Guide prévention et santé
[GUIDE] Vigilance sommeil - Guide prévention et santé
 
Secure development environment @ Meet Magento Croatia 2017
Secure development environment @ Meet Magento Croatia 2017Secure development environment @ Meet Magento Croatia 2017
Secure development environment @ Meet Magento Croatia 2017
 
Diagnóstico SEO Técnico con Herramientas #TheInbounder
Diagnóstico SEO Técnico con Herramientas #TheInbounderDiagnóstico SEO Técnico con Herramientas #TheInbounder
Diagnóstico SEO Técnico con Herramientas #TheInbounder
 
B2B Marketing and The Power of Twitter
B2B Marketing and The Power of TwitterB2B Marketing and The Power of Twitter
B2B Marketing and The Power of Twitter
 
Deploying Immutable infrastructures with RabbitMQ and Solr
Deploying Immutable infrastructures with RabbitMQ and SolrDeploying Immutable infrastructures with RabbitMQ and Solr
Deploying Immutable infrastructures with RabbitMQ and Solr
 
Charla control parental e IoT v2. Etek.ppsx
Charla control parental e IoT v2. Etek.ppsxCharla control parental e IoT v2. Etek.ppsx
Charla control parental e IoT v2. Etek.ppsx
 
TensroFlow XLA : JIT編 (r1.3版)
TensroFlow XLA : JIT編 (r1.3版)TensroFlow XLA : JIT編 (r1.3版)
TensroFlow XLA : JIT編 (r1.3版)
 
Jeudis du Libre - MySQL comme Document Store
Jeudis du Libre - MySQL comme Document StoreJeudis du Libre - MySQL comme Document Store
Jeudis du Libre - MySQL comme Document Store
 
第5回ue4ハンズオンセミナー
第5回ue4ハンズオンセミナー第5回ue4ハンズオンセミナー
第5回ue4ハンズオンセミナー
 
Understanding P2P
Understanding P2PUnderstanding P2P
Understanding P2P
 
DNSの運用を考える
DNSの運用を考えるDNSの運用を考える
DNSの運用を考える
 
Master the flow of microservices - because your business is more complex than...
Master the flow of microservices - because your business is more complex than...Master the flow of microservices - because your business is more complex than...
Master the flow of microservices - because your business is more complex than...
 
Solvent Effects on Chemical Reaction
Solvent Effects on Chemical ReactionSolvent Effects on Chemical Reaction
Solvent Effects on Chemical Reaction
 
Fortune 1000 HR Leader Survey Results
Fortune 1000 HR Leader Survey ResultsFortune 1000 HR Leader Survey Results
Fortune 1000 HR Leader Survey Results
 
Startups & Gamification (LeanCamp Tehran 2016)
Startups & Gamification (LeanCamp Tehran 2016)Startups & Gamification (LeanCamp Tehran 2016)
Startups & Gamification (LeanCamp Tehran 2016)
 
Scale Model Humans
Scale Model HumansScale Model Humans
Scale Model Humans
 
Fun Core Gym Pdf
Fun Core Gym PdfFun Core Gym Pdf
Fun Core Gym Pdf
 
Discovery, Reuse, Research and Crowdsourcing: IIIF experiences from the NLW
Discovery, Reuse, Research and Crowdsourcing: IIIF experiences from the NLWDiscovery, Reuse, Research and Crowdsourcing: IIIF experiences from the NLW
Discovery, Reuse, Research and Crowdsourcing: IIIF experiences from the NLW
 

Similaire à GoFFIng around with Ruby #RubyConfPH

Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Andreas Dewes
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docxeugeniadean34240
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoRodolfo Carvalho
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain DorgueilSimple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain DorgueilPôle Systematic Paris-Region
 
Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011Jimmy Schementi
 
Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot CampTroy Miles
 
Node.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizationsNode.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizationsDawid Rusnak
 
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012rivierarb
 
SharePoint Saturday Belgium 2014 - Production debugging of SharePoint applica...
SharePoint Saturday Belgium 2014 - Production debugging of SharePoint applica...SharePoint Saturday Belgium 2014 - Production debugging of SharePoint applica...
SharePoint Saturday Belgium 2014 - Production debugging of SharePoint applica...BIWUG
 
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013Gregg Donovan
 
carrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-APIcarrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-APIYoni Davidson
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
The Ring programming language version 1.5.3 book - Part 189 of 194
The Ring programming language version 1.5.3 book - Part 189 of 194The Ring programming language version 1.5.3 book - Part 189 of 194
The Ring programming language version 1.5.3 book - Part 189 of 194Mahmoud Samir Fayed
 
XilinxのxsimでSoftware Driven Verification.pdf
XilinxのxsimでSoftware  Driven Verification.pdfXilinxのxsimでSoftware  Driven Verification.pdf
XilinxのxsimでSoftware Driven Verification.pdfMr. Vengineer
 
A Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to GoA Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to GoMatt Stine
 

Similaire à GoFFIng around with Ruby #RubyConfPH (20)

Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...Code is not text! How graph technologies can help us to understand our code b...
Code is not text! How graph technologies can help us to understand our code b...
 
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
 
Go 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX GoGo 1.10 Release Party - PDX Go
Go 1.10 Release Party - PDX Go
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
Golang
GolangGolang
Golang
 
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain DorgueilSimple ETL in python 3.5+ with Bonobo, Romain Dorgueil
Simple ETL in python 3.5+ with Bonobo, Romain Dorgueil
 
Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011
 
Node Boot Camp
Node Boot CampNode Boot Camp
Node Boot Camp
 
Node.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizationsNode.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizations
 
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012Ruby C extensions at the Ruby drink-up of Sophia, April 2012
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
 
SharePoint Saturday Belgium 2014 - Production debugging of SharePoint applica...
SharePoint Saturday Belgium 2014 - Production debugging of SharePoint applica...SharePoint Saturday Belgium 2014 - Production debugging of SharePoint applica...
SharePoint Saturday Belgium 2014 - Production debugging of SharePoint applica...
 
CGI.ppt
CGI.pptCGI.ppt
CGI.ppt
 
Gcrc talk
Gcrc talkGcrc talk
Gcrc talk
 
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
 
carrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-APIcarrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-API
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
The Ring programming language version 1.5.3 book - Part 189 of 194
The Ring programming language version 1.5.3 book - Part 189 of 194The Ring programming language version 1.5.3 book - Part 189 of 194
The Ring programming language version 1.5.3 book - Part 189 of 194
 
XilinxのxsimでSoftware Driven Verification.pdf
XilinxのxsimでSoftware  Driven Verification.pdfXilinxのxsimでSoftware  Driven Verification.pdf
XilinxのxsimでSoftware Driven Verification.pdf
 
A Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to GoA Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to Go
 

Plus de Gautam Rege

RubyConf India 2019 - Confessions of a rubypreneur
RubyConf India 2019 - Confessions of a rubypreneurRubyConf India 2019 - Confessions of a rubypreneur
RubyConf India 2019 - Confessions of a rubypreneurGautam Rege
 
Agile india 2017 - Rewarding OpenSource with $$$
Agile india 2017 - Rewarding OpenSource with $$$Agile india 2017 - Rewarding OpenSource with $$$
Agile india 2017 - Rewarding OpenSource with $$$Gautam Rege
 
WIDS - Gamifying Open Source
WIDS - Gamifying Open SourceWIDS - Gamifying Open Source
WIDS - Gamifying Open SourceGautam Rege
 
Gamifying Open Source
Gamifying Open SourceGamifying Open Source
Gamifying Open SourceGautam Rege
 
Affordable Smart Housing - The new revolution
Affordable Smart Housing - The new revolutionAffordable Smart Housing - The new revolution
Affordable Smart Housing - The new revolutionGautam Rege
 
WebSummit 2015 - Gopher it
WebSummit 2015 - Gopher itWebSummit 2015 - Gopher it
WebSummit 2015 - Gopher itGautam Rege
 
Dont test your code
Dont test your codeDont test your code
Dont test your codeGautam Rege
 
Art of speaking at tech conferences
Art of speaking at tech conferencesArt of speaking at tech conferences
Art of speaking at tech conferencesGautam Rege
 
RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!Gautam Rege
 
Ruby and rails - Advanced Training (Cybage)
Ruby and rails - Advanced Training (Cybage)Ruby and rails - Advanced Training (Cybage)
Ruby and rails - Advanced Training (Cybage)Gautam Rege
 
RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby Gautam Rege
 
ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyGautam Rege
 
GCRC 2014 - The Dark Side of Ruby
GCRC 2014 - The Dark Side of RubyGCRC 2014 - The Dark Side of Ruby
GCRC 2014 - The Dark Side of RubyGautam Rege
 
Rails Vs CakePHP
Rails Vs CakePHPRails Vs CakePHP
Rails Vs CakePHPGautam Rege
 

Plus de Gautam Rege (15)

RubyConf India 2019 - Confessions of a rubypreneur
RubyConf India 2019 - Confessions of a rubypreneurRubyConf India 2019 - Confessions of a rubypreneur
RubyConf India 2019 - Confessions of a rubypreneur
 
Agile india 2017 - Rewarding OpenSource with $$$
Agile india 2017 - Rewarding OpenSource with $$$Agile india 2017 - Rewarding OpenSource with $$$
Agile india 2017 - Rewarding OpenSource with $$$
 
WIDS - Gamifying Open Source
WIDS - Gamifying Open SourceWIDS - Gamifying Open Source
WIDS - Gamifying Open Source
 
Gamifying Open Source
Gamifying Open SourceGamifying Open Source
Gamifying Open Source
 
Affordable Smart Housing - The new revolution
Affordable Smart Housing - The new revolutionAffordable Smart Housing - The new revolution
Affordable Smart Housing - The new revolution
 
WebSummit 2015 - Gopher it
WebSummit 2015 - Gopher itWebSummit 2015 - Gopher it
WebSummit 2015 - Gopher it
 
Dont test your code
Dont test your codeDont test your code
Dont test your code
 
Art of speaking at tech conferences
Art of speaking at tech conferencesArt of speaking at tech conferences
Art of speaking at tech conferences
 
RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!
 
Ruby and rails - Advanced Training (Cybage)
Ruby and rails - Advanced Training (Cybage)Ruby and rails - Advanced Training (Cybage)
Ruby and rails - Advanced Training (Cybage)
 
RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby
 
ScotRuby - Dark side of ruby
ScotRuby - Dark side of rubyScotRuby - Dark side of ruby
ScotRuby - Dark side of ruby
 
GCRC 2014 - The Dark Side of Ruby
GCRC 2014 - The Dark Side of RubyGCRC 2014 - The Dark Side of Ruby
GCRC 2014 - The Dark Side of Ruby
 
Rails Vs CakePHP
Rails Vs CakePHPRails Vs CakePHP
Rails Vs CakePHP
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 

Dernier

SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Anthony Dahanne
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profileakrivarotava
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingShane Coughlan
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Cizo Technology Services
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolsosttopstonverter
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 

Dernier (20)

SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profile
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
Global Identity Enrolment and Verification Pro Solution - Cizo Technology Ser...
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 
eSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration toolseSoftTools IMAP Backup Software and migration tools
eSoftTools IMAP Backup Software and migration tools
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 

GoFFIng around with Ruby #RubyConfPH

  • 1. GoFFIng around with Ruby @gautamrege
  • 3.
  • 4. FFI
  • 6. Why do we need FFI? • Resque / Sidekiq / delayed_job • 3rd party API requests • Sending newsletters • Heavy computation • Fast searches, aggregation.
  • 7. Why Go? • Power of multi-core. • Goroutines to the rescue • Language is restricted focussed. • Programming Ethics • Compiled If it compiles, it works
  • 8. What is FFI anyway? • Language interoperability! • Calling convention. • libffi.so • ruby-ffi • Fiddle (in Stdlib since 1.9.3)
  • 9. FFI or Good ol’ C-ext? • FFI Easier to write and maintain. • Gems are now easier to install - don’t need build dependencies. • Portable code • No more “Building native extensions…” while installing gems.
  • 10. Simple Ruby Code def race(legs, benchmark) tids = [] legs.times do |i| tids << Thread.new(i) do |leg| array = (1..benchmark).map { |i| i } p "Done leg #{leg} benchmark #{benchmark}" end end tids.each { |t| t.join } end race(4, 50_000_000)
  • 11. Benchmark - Ruby $ time ruby run1.rb Done leg 1 benchmark 50000000 Done leg 0 benchmark 50000000 Done leg 3 benchmark 50000000 Done leg 2 benchmark 50000000 real 0m18.242s user 0m16.626s sys 0m1.292s
  • 12. Simple Go Code package main import "fmt" func race(leg int, benchmark int) { arr := []int{} for i := 0; i < benchmark; i++ { arr = append(arr, i) } fmt.Println("Done leg:", leg) } func main() { for i := 0; i < 4; i++ { race(i, 50000000) } }
  • 13. Simple Go Code package main import "fmt" func race(leg int, benchmark int) { arr := []int{} for i := 0; i < benchmark; i++ { arr = append(arr, i) } fmt.Println("Done leg:", leg) } func main() { for i := 0; i < 4; i++ { race(i, 50000000) } }
  • 14. Simple Go Code package main import "fmt" func race(leg int, benchmark int) { arr := []int{} for i := 0; i < benchmark; i++ { arr = append(arr, i) } fmt.Println("Done leg:", leg) } func main() { for i := 0; i < 4; i++ { race(i, 50000000) } }
  • 15. Simple Go Code package main import "fmt" func race(leg int, benchmark int) { arr := []int{} for i := 0; i < benchmark; i++ { arr = append(arr, i) } fmt.Println("Done leg:", leg) } func main() { for i := 0; i < 4; i++ { race(i, 50000000) } }
  • 16. Benchmark - Go $ time go run run1.go Done leg: 0 Done leg: 1 Done leg: 2 Done leg: 3 real 0m2.774s user 0m2.564s sys 0m0.929s
  • 17. Calling Go from Ruby • Build a C library using Go code. • Build a FFI extension in Ruby • Call it !
  • 18. c-shared Go library package main import ( "C" "fmt" ) func race(leg int, benchmark int) { arr := []int{} for i := 0; i < benchmark; i++ { arr = append(arr, i) } fmt.Println("Done leg:", leg) } //export churn func churn(leg C.int, benchmark C.int) { for i := 0; i < int(leg); i++ { race(i, int(benchmark)) } } func main() { }
  • 19. c-shared Go library package main import ( "C" "fmt" ) func race(leg int, benchmark int) { arr := []int{} for i := 0; i < benchmark; i++ { arr = append(arr, i) } fmt.Println("Done leg:", leg) } //export churn func churn(leg C.int, benchmark C.int) { for i := 0; i < int(leg); i++ { race(i, int(benchmark)) } } func main() { }
  • 20. c-shared Go library package main import ( "C" "fmt" ) func race(leg int, benchmark int) { arr := []int{} for i := 0; i < benchmark; i++ { arr = append(arr, i) } fmt.Println("Done leg:", leg) } //export churn func churn(leg C.int, benchmark C.int) { for i := 0; i < int(leg); i++ { race(i, int(benchmark)) } } func main() { } $ go build -o librun.so -buildmode=c-shared run2.go
  • 21. Ruby FFI extension require 'fiddle' module Fun def self.race(leg, benchmark) librun = Fiddle.dlopen(‘./librun.so') race = Fiddle::Function.new( librun['churn'], [Fiddle::TYPE_INT, Fiddle::TYPE_INT], Fiddle::TYPE_VOID ) race.call(leg, benchmark) end end
  • 22. Call it! require './fun' Fun.race(4, 50_000_000) $ time ruby run2.rb Done leg: 0 Benchmark 50000000 Done leg: 1 Benchmark 50000000 Done leg: 2 Benchmark 50000000 Done leg: 3 Benchmark 50000000 real 0m2.776s user 0m2.719s sys 0m0.969s
  • 29. Doing the Math! 18 STATES 428 CARS 68 VENDORS = 523,872 cells
  • 30. If that was not enough … For each of the 523,872 cells Calculate Depreciation Index in ElasticSearch
  • 31. for cmake, v := range segments { for model, j := range v { for submodel, k := range j { for insurance, segment := range k { wg.Add(1) go func(cmake string, model string, submodel string, insurance string, segment string) { defer wg.Done() // data := es_data{...} // prepare_es_data(...) es_add_depreciation(&data) }(cmake, model, submodel, insurance, segment) } } } } Practical Example About 350,000 go-routines
  • 32. for cmake, v := range segments { for model, j := range v { for submodel, k := range j { for insurance, segment := range k { wg.Add(1) go func(cmake string, model string, submodel string, insurance string, segment string) { defer wg.Done() // data := es_data{...} // prepare_es_data(...) es_add_depreciation(&data) }(cmake, model, submodel, insurance, segment) } } } } Practical Example About 350,000 go-routines
  • 34. Performance Benefits • Sidekiq: 2 GB memory & ~18 minutes
  • 35. Performance Benefits • Sidekiq: 2 GB memory & ~18 minutes • Go FFI: 135 MB memory & 1 minute 13 seconds.
  • 36. Performance Benefits • Sidekiq: 2 GB memory & ~18 minutes • Go FFI: 135 MB memory & 1 minute 13 seconds. ElasticSearch indexing took 48 seconds
  • 37. Complexities of FFI • Memory Management (alloc / free) • Pointer management • Fiddle::Pointer • Fiddle::Closure Don’t use them unless you HAVE to!
  • 38. Resources • Fiddle http://ruby-doc.org/stdlib-2.0.0/libdoc/fiddle/rdoc/ Fiddle.html • FFI Core Concepts https://github.com/ffi/ffi/wiki/Core- Concepts • Github: ffi/ffi • Effective Go ( https://golang.org/doc/effective_go.html ) • A Tour of Go ( https://tour.golang.org/welcome/1 )