The document discusses improving the performance of Ruby extensions written in C++. It proposes using C++ as the language for extensions rather than C, as C++ has convenient features like type inference, lambda expressions, and default arguments that make extensions easier to write. It recommends providing a convenient API like Ext++ that allows defining Ruby classes and methods directly in C++ code similar to how it is done in Ruby. This avoids the verbosity of raw C APIs and allows leveraging modern C++ features for high performance extension code that stays within C++ as much as possible without moving between C++ and Ruby.
To hit Ruby3x3, we must first figure out **what** we're going to measure, **how** we're going to measure it, in order to get what we actually want. I'll cover some standard definitions of benchmarking in dynamic languages, as well as the tradeoffs that must be made when benchmarking. I'll look at some of the possible benchmarks that could be considered for Ruby 3x3, and evaluate them for what they're good for measuring, and what they're less good for measuring, in order to help the Ruby community decide what the 3x goal is going to be measured against.
How to develop the Standard Libraries of Ruby?Hiroshi SHIBATA
I maintain the RubyGems, Bundler and the standard libraries of the Ruby language. So, I've been extract many of the standard libraries to default gems and GitHub at Ruby 3.0. But the some of libraries still remains in only Ruby repository. I will describe these situation.
This talk covers several techniques for extending Ruby programs using C and C++. Examples are shown for foreign function interface techniques, inline native code and a gem that wraps Ruby's C API with C++ classes and templates.
To hit Ruby3x3, we must first figure out **what** we're going to measure, **how** we're going to measure it, in order to get what we actually want. I'll cover some standard definitions of benchmarking in dynamic languages, as well as the tradeoffs that must be made when benchmarking. I'll look at some of the possible benchmarks that could be considered for Ruby 3x3, and evaluate them for what they're good for measuring, and what they're less good for measuring, in order to help the Ruby community decide what the 3x goal is going to be measured against.
How to develop the Standard Libraries of Ruby?Hiroshi SHIBATA
I maintain the RubyGems, Bundler and the standard libraries of the Ruby language. So, I've been extract many of the standard libraries to default gems and GitHub at Ruby 3.0. But the some of libraries still remains in only Ruby repository. I will describe these situation.
This talk covers several techniques for extending Ruby programs using C and C++. Examples are shown for foreign function interface techniques, inline native code and a gem that wraps Ruby's C API with C++ classes and templates.
The Present and The Future of Functional Programming in C++Alexander Granin
Keynote talk for C++ Siberia 2019.
I'm speaking about why Functional Programming is important in C++ world, what is the philosophy of FP in C++, and what features do we have. I'm presenting a connection of constexpr and template metaprogramming to pure FP, and talking about why monads are inevitable. I'm also discussing an upcoming features in C++.
Types of C++ functions:
Standard functions
User-defined functions
C++ function structure
Function signature
Function body
Declaring and Implementing C++ functions
APIs can be challenging to understand and consume from the command line, a utility most developers use daily. Tools like cURL facilitate command line API calls, but don't present the API surface. In this session we will look at an approach to generating intuitive command line utilities with Go & Cobra for gRPC APIs.
In this WebHack talk I shared my experience about microservices, Docker, Kubernetes and Kong, an API gateway by Mashape. Since they are based on a real working system, this slides is majorly for how to build the whole thing up, not about detailed internal implementation. Although I included some details and reference in order to make it more comprehensive.
Have you ever left out a comma in your payload calling an API on the command line? Have you cURL’d a REST API only to realize you forgot that crucial query parameter? If only the command line could take advantage of some structured description of the API model…
APIs can be challenging to understand and consume from the command line, a utility most developers use daily. In this session we will look at an approach to generating intuitive command line utilities for gRPC APIs.
The talk focuses on the processes and requirements to ship a software, which was written with MacRuby, to the end user. I present libraries and tools, that are helpful for this purpose and show how to use them.
RubyKaigi 2022 - Fast data processing with Ruby and Apache ArrowKouhei Sutou
I introduced Ruby and Apache Arrow integration including the "super fast large data interchange and processing" Apache Arrow feature at RubyKaigi Takeout 2021.
This talk introduces how we can use the "super fast large data interchange and processing" Apache Arrow feature in Ruby. Here are some use cases:
* Fast data retrieval (fast pluck) from DB such as MySQL and PostgreSQL for batch processes in a Ruby on Rails application
* Fast data interchange with JavaScript for dynamic visualization in a Ruby on Rails application
* Fast OLAP with in-process DB such as DuckDB and Apache Arrow DataFusion in a Ruby on Rails application or irb session
RubyKaigi Takeout 2021 - Red Arrow - Ruby and Apache ArrowKouhei Sutou
To use Ruby for data processing widely, Apache Arrow support is important. We can do the followings with Apache Arrow:
* Super fast large data interchange and processing
* Reading/writing data in several famous formats such as CSV and Apache Parquet
* Reading/writing partitioned large data on cloud storage such as Amazon S3
This talk describes the followings:
* What is Apache Arrow
* How to use Apache Arrow with Ruby
* How to integrate with Ruby 3.0 features such as MemoryView and Ractor
Apache Arrow 1.0 - A cross-language development platform for in-memory dataKouhei Sutou
Apache Arrow is a cross-language development platform for in-memory data. You can use Apache Arrow to process large data effectively in Python and other languages such as R. Apache Arrow is the future of data processing. Apache Arrow 1.0, the first major version, was released at 2020-07-24. It's a good time to know Apache Arrow and start using it.
Apache Arrow - A cross-language development platform for in-memory dataKouhei Sutou
Apache Arrow is the future for data processing systems. This talk describes how to solve data sharing overhead in data processing system such as Spark and PySpark. This talk also describes how to accelerate computation against your large data by Apache Arrow.
csv, one of the standard libraries, in Ruby 2.6 has many improvements:
* Default gemified
* Faster CSV parsing
* Faster CSV writing
* Clean new CSV parser implementation for further improvements
* Reconstructed test suites for further improvements
* Benchmark suites for further performance improvements
These improvements are done without breaking backward compatibility.
This talk describes details of these improvements by a new csv maintainer.
PGroonga 2 – Make PostgreSQL rich full text search system backend!Kouhei Sutou
PGroonga 2.0 has been released with 2 years development since PGroonga 1.0.0. PGroonga 1.0.0 just provides fast full text search with all languages support. It's important because it's a lacked feature in PostgreSQL. PGroonga 2.0 provides more useful features to implement rich full text search system with PostgreSQL. This session shows how to implement rich full text search system with PostgreSQL!
This talk describes about PGroonga that resolves these problems.
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
Keynote at DIGIT West Expo, Glasgow on 29 May 2024.
Cheryl Hung, ochery.com
Sr Director, Infrastructure Ecosystem, Arm.
The key trends across hardware, cloud and open-source; exploring how these areas are likely to mature and develop over the short and long-term, and then considering how organisations can position themselves to adapt and thrive.
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
Are you looking to streamline your workflows and boost your projects’ efficiency? Do you find yourself searching for ways to add flexibility and control over your FME workflows? If so, you’re in the right place.
Join us for an insightful dive into the world of FME parameters, a critical element in optimizing workflow efficiency. This webinar marks the beginning of our three-part “Essentials of Automation” series. This first webinar is designed to equip you with the knowledge and skills to utilize parameters effectively: enhancing the flexibility, maintainability, and user control of your FME projects.
Here’s what you’ll gain:
- Essentials of FME Parameters: Understand the pivotal role of parameters, including Reader/Writer, Transformer, User, and FME Flow categories. Discover how they are the key to unlocking automation and optimization within your workflows.
- Practical Applications in FME Form: Delve into key user parameter types including choice, connections, and file URLs. Allow users to control how a workflow runs, making your workflows more reusable. Learn to import values and deliver the best user experience for your workflows while enhancing accuracy.
- Optimization Strategies in FME Flow: Explore the creation and strategic deployment of parameters in FME Flow, including the use of deployment and geometry parameters, to maximize workflow efficiency.
- Pro Tips for Success: Gain insights on parameterizing connections and leveraging new features like Conditional Visibility for clarity and simplicity.
We’ll wrap up with a glimpse into future webinars, followed by a Q&A session to address your specific questions surrounding this topic.
Don’t miss this opportunity to elevate your FME expertise and drive your projects to new heights of efficiency.
GraphRAG is All You need? LLM & Knowledge GraphGuy Korland
Guy Korland, CEO and Co-founder of FalkorDB, will review two articles on the integration of language models with knowledge graphs.
1. Unifying Large Language Models and Knowledge Graphs: A Roadmap.
https://arxiv.org/abs/2306.08302
2. Microsoft Research's GraphRAG paper and a review paper on various uses of knowledge graphs:
https://www.microsoft.com/en-us/research/blog/graphrag-unlocking-llm-discovery-on-narrative-private-data/
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualityInflectra
In this insightful webinar, Inflectra explores how artificial intelligence (AI) is transforming software development and testing. Discover how AI-powered tools are revolutionizing every stage of the software development lifecycle (SDLC), from design and prototyping to testing, deployment, and monitoring.
Learn about:
• The Future of Testing: How AI is shifting testing towards verification, analysis, and higher-level skills, while reducing repetitive tasks.
• Test Automation: How AI-powered test case generation, optimization, and self-healing tests are making testing more efficient and effective.
• Visual Testing: Explore the emerging capabilities of AI in visual testing and how it's set to revolutionize UI verification.
• Inflectra's AI Solutions: See demonstrations of Inflectra's cutting-edge AI tools like the ChatGPT plugin and Azure Open AI platform, designed to streamline your testing process.
Whether you're a developer, tester, or QA professional, this webinar will give you valuable insights into how AI is shaping the future of software delivery.
JMeter webinar - integration with InfluxDB and GrafanaRTTS
Watch this recorded webinar about real-time monitoring of application performance. See how to integrate Apache JMeter, the open-source leader in performance testing, with InfluxDB, the open-source time-series database, and Grafana, the open-source analytics and visualization application.
In this webinar, we will review the benefits of leveraging InfluxDB and Grafana when executing load tests and demonstrate how these tools are used to visualize performance metrics.
Length: 30 minutes
Session Overview
-------------------------------------------
During this webinar, we will cover the following topics while demonstrating the integrations of JMeter, InfluxDB and Grafana:
- What out-of-the-box solutions are available for real-time monitoring JMeter tests?
- What are the benefits of integrating InfluxDB and Grafana into the load testing stack?
- Which features are provided by Grafana?
- Demonstration of InfluxDB and Grafana using a practice web application
To view the webinar recording, go to:
https://www.rttsweb.com/jmeter-integration-webinar
State of ICS and IoT Cyber Threat Landscape Report 2024 previewPrayukth K V
The IoT and OT threat landscape report has been prepared by the Threat Research Team at Sectrio using data from Sectrio, cyber threat intelligence farming facilities spread across over 85 cities around the world. In addition, Sectrio also runs AI-based advanced threat and payload engagement facilities that serve as sinks to attract and engage sophisticated threat actors, and newer malware including new variants and latent threats that are at an earlier stage of development.
The latest edition of the OT/ICS and IoT security Threat Landscape Report 2024 also covers:
State of global ICS asset and network exposure
Sectoral targets and attacks as well as the cost of ransom
Global APT activity, AI usage, actor and tactic profiles, and implications
Rise in volumes of AI-powered cyberattacks
Major cyber events in 2024
Malware and malicious payload trends
Cyberattack types and targets
Vulnerability exploit attempts on CVEs
Attacks on counties – USA
Expansion of bot farms – how, where, and why
In-depth analysis of the cyber threat landscape across North America, South America, Europe, APAC, and the Middle East
Why are attacks on smart factories rising?
Cyber risk predictions
Axis of attacks – Europe
Systemic attacks in the Middle East
Download the full report from here:
https://sectrio.com/resources/ot-threat-landscape-reports/sectrio-releases-ot-ics-and-iot-security-threat-landscape-report-2024/
"Impact of front-end architecture on development cost", Viktor TurskyiFwdays
I have heard many times that architecture is not important for the front-end. Also, many times I have seen how developers implement features on the front-end just following the standard rules for a framework and think that this is enough to successfully launch the project, and then the project fails. How to prevent this and what approach to choose? I have launched dozens of complex projects and during the talk we will analyze which approaches have worked for me and which have not.
Neuro-symbolic is not enough, we need neuro-*semantic*Frank van Harmelen
Neuro-symbolic (NeSy) AI is on the rise. However, simply machine learning on just any symbolic structure is not sufficient to really harvest the gains of NeSy. These will only be gained when the symbolic structures have an actual semantics. I give an operational definition of semantics as “predictable inference”.
All of this illustrated with link prediction over knowledge graphs, but the argument is general.
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...UiPathCommunity
💥 Speed, accuracy, and scaling – discover the superpowers of GenAI in action with UiPath Document Understanding and Communications Mining™:
See how to accelerate model training and optimize model performance with active learning
Learn about the latest enhancements to out-of-the-box document processing – with little to no training required
Get an exclusive demo of the new family of UiPath LLMs – GenAI models specialized for processing different types of documents and messages
This is a hands-on session specifically designed for automation developers and AI enthusiasts seeking to enhance their knowledge in leveraging the latest intelligent document processing capabilities offered by UiPath.
Speakers:
👨🏫 Andras Palfi, Senior Product Manager, UiPath
👩🏫 Lenka Dulovicova, Product Program Manager, UiPath
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
The publishing industry has been selling digital audiobooks and ebooks for over a decade and has found its groove. What’s changed? What has stayed the same? Where do we go from here? Join a group of leading sales peers from across the industry for a conversation about the lessons learned since the popularization of digital books, best practices, digital book supply chain management, and more.
Link to video recording: https://bnctechforum.ca/sessions/selling-digital-books-in-2024-insights-from-industry-leaders/
Presented by BookNet Canada on May 28, 2024, with support from the Department of Canadian Heritage.
Let's dive deeper into the world of ODC! Ricardo Alves (OutSystems) will join us to tell all about the new Data Fabric. After that, Sezen de Bruijn (OutSystems) will get into the details on how to best design a sturdy architecture within ODC.
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
91mobiles recently conducted a Smart TV Buyer Insights Survey in which we asked over 3,000 respondents about the TV they own, aspects they look at on a new TV, and their TV buying preferences.
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Improve extension API: C++ as better language for extension
1. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Improve extension API
C++ as better language for extension
Kouhei Sutou
ClearCode Inc.
RubyKaigi 2017
2017-09-19
3. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Ad2: Silver sponsor
4. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Ad3: Red Data Tools
Project that provides data
processing tools for Ruby
Ruby用のデータ処理ツール群を提供するプロジェクト
https://red-data-tools.github.io/✓
✓
Workshop during the afternoon
break on the 2nd day (today!)
2日目(今日!)の午後休憩中にワークショップがあるよ!
✓
5. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Ad4: OSS Gate
Project that increases people
who join OSS development
OSSの開発に継続的に参加する人を継続的に増やす取り組み
https://oss-gate.github.io/✓
✓
6. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Ad4: OSS Gate
Ruby is OSS
RubyもOSS
OSS Gate wants to increase people
to join Ruby itself and RubyGems
development!
OSS GateではRuby本体の開発や各種RubyGemの開発に参加
する人も増やしたい!
✓
✓
7. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Ad4: OSS Gate
Now, in Tokyo, Sapporo, Osaka
and Kyoto
現在は東京・札幌・大阪・京都で活動中
If you live near by, join "OSS
Gate workshop"!
これらの地域に住んでいる人は「OSS Gateワークショッ
プ」に参加しよう!
✓
✓
8. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Ad4: OSS Gate
Want to expand to Hiroshima
and other areas all over the
world!
広島や世界中のいろんな地域で活動したい!
If you're interested in increasing
people who join OSS development,
talk to me!
OSSの開発に参加する人が増えることに興味のある人は私
に声をかけて!
✓
✓
9. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
What I want to do
やりたいこと
Improve performance
with C/C++ libraries
C/C++のライブラリーを使った高速化
Not create binding
バインディングを作りたいわけじゃない
✓
10. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Point of improving perf
高速化するために大事なこと
Done in C/C++
as much as possible
できるだけC/C++内で完結させる
Don't move between
C/C++ and Ruby
C/C++とRuby間でいったりきたりしない
✓
11. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Example: #sum
例:#sum
numbers = (1..100000).to_a
# Move between C and Ruby: 25.1ms
numbers.inject(&:+)
# Done in C: 0.5ms
# 50x faster(50倍速い)
numbers.sum
12. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
FYI: #inject(symbol)
参考情報:#inject(symbol)
numbers = (1..100000).to_a
# Move between C and Ruby: 25.1ms
numbers.inject(&:+)
# Done in C: 0.5ms
# 50x faster(50倍速い)
numbers.inject(:+)
13. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Extension and binding
拡張ライブラリーとバインディング
Extension(拡張ライブラリー)
Ruby library implemented in C
Cで実装されたRubyのライブラリー
✓
✓
Binding(バインディング)
Ruby library to use library
implemented in other languages
他言語実装のライブラリを使うためのRubyのライブラリ
✓
✓
14. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Binding usage
バインディングの使い方
Export each API to Ruby
それぞれのAPIをRubyで使えるようにする
✓
Combine exported APIs in Ruby
バインディングが提供するAPIをRubyレベルで組み合わせる
✓
15. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Binding usage example
バインディングの使い方例
require "cairo"
include Cairo
# Combine APIs
s = PDFSurface.new("red.pdf", 10, 10) # API
context = Context.new(s) # API
context.set_source_color(:red) # API
context.paint # API
context.show_page # API
s.finish # API
16. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Point of improving perf
高速化するために大事なこと
Done in C/C++
as much as possible
できるだけC/C++内で完結させる
Don't move between
C/C++ and Ruby
C/C++とRuby間でいったりきたりしない
✓
17. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Perf improvement example
高速化例
# Don't combine APIs in Ruby
# RubyレベルでAPIを組み合わせない
## context.set_source_color(:red) # API
## context.paint # API
## context.show_page # API
# Just call higher level API in Ruby
# Rubyからはもっと高レベルなAPIを呼び出す
context.show_red_page # Implemented in C
# ここはCで実装
18. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
What I want to do
やりたいこと
Improve performance
with C/C++ libraries
C/C++のライブラリーを使った高速化
Not create binding
バインディングを作りたいわけじゃない
✓
19. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Use case
高速化したい場面
Machine learning
機械学習
Combine array/matrix operations
配列・行列に対する演算をまとめる
✓
✓
20. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Raw C API for extension
拡張ライブラリー用の生のC API
Not bad but can be verbose
because of C
悪くないんだけどCなので冗長
Need better approach
もっといい感じの方法を使いたい
✓
21. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Requirements
要件
Easy to use C/C++ libraries
C/C++のライブラリーを簡単に使えること
✓
Easy to write as much as possible
できるだけ書きやすいこと
✓
Easy to debug as much as possible
できるだけデバッグしやすいこと
✓
22. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Approaches(実現方法)
Extend language to support
writing extension
拡張ライブラリーを書けるように言語を拡張
✓
Not based on C
C以外の言語を使う
✓
Provide convenient API
便利APIを提供
✓
23. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Recommended approach
オススメの実現方法
Extend language to support
writing extension
拡張ライブラリーを書けるように言語を拡張
✓
Not based on C
C以外の言語を使う
✓
Provide convenient API
便利APIを提供
✓
24. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Provide convenient API
便利なAPIを提供
Rice: C++ + Ruby✓
Ext++: C++11 + Ruby✓
Boost.Python: C++ + Python✓
pybind11: C++11 + Python✓
25. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Useful C++ properties
C++の便利の性質
C++ can use C API directory
C++ではCのAPIを直接使える
No wrapper API or libffi
ラッパーAPIもlibffiもいらない
✓
✓
C++11 or later has many
convenient features
C++11以降には便利機能がたくさんある
✓
26. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ convenient feature1
C++の便利機能1
Type detection with "auto"
autoで型推論(C++11)
int n = 10;
auto square = n * n;
// square's type is "int"
// squareの型は「int」
27. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ convenient feature2
C++の便利機能2
Lambda expression (C++11)
ラムダ式(C++11)
// In Ruby: ->(n) {n * n}
auto square = [](int n) {
return n * n; // Return type is detected
};
square(10); // => 100
28. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ convenient feature3
C++の便利機能3
Default argument
デフォルト引数
// In Ruby: def square(n=10)
int square(int n=10) {
return n * n;
}
// square() == square(10)
29. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Convenient API example1
便利なAPI例1
# Ruby: Normal
class Sample
def hello
"Hello"
end
end
30. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Convenient API example1
便利なAPI例1
# Ruby: No syntax sugar
Sample = Class.new do
define_method(:hello) do
"Hello"
end
end
31. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Convenient API example1
便利なAPI例1
// C++: Ext++
#include <ruby.hpp>
extern "C" void Init_sample(void) {
// Parent class (rb_cObject) is omittable
rb::Class("Sample"). // class Sample in Ruby
define_method("hello", // def hello in Ruby
[](VALUE self) { // ->() {"Hello"} in Ruby
return rb_str_new_static("Hello");
});
}
32. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Convenient API example1
便利なAPI例1
/* C */
#include <ruby.h>
static VALUE rb_sample_hello(VALUE self) {
return rb_str_new_static("Hello");
} /* ↑Definition. */
void Init_sample(void) {
/* ↓Must specify parent class. */
VALUE sample = rb_define_class("Sample", rb_cObject);
/* ↓Registration. They are separated. */
rb_define_method(sample, "hello", rb_sample_hello, 0);
}
33. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ convenient feature4
C++の便利機能4
Custom type conversion
型変換のカスタマイズ
"Cast" is customizable
「キャスト」をカスタマイズできるということ
✓
34. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Custom type conversion
型変換のカスタマイズ
// Wrapper class of VALUE
class Object {
public:
// def initialize(rb_object)
// @rb_object_ = rb_object
// end
Object(VALUE rb_object) : rb_object_(rb_object) {}
private:
VALUE rb_object_;
};
35. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Custom type conversion
型変換のカスタマイズ
class Object {
operator bool() const {
return RTEST(rb_object_);
}
};
// Object nil(Qnil); // Qnil wrapper
// if (nil) { → if (RTEST(Qnil)) {
36. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Custom type conversion
型変換のカスタマイズ
// Trap1(罠1)
// bool→int cast is available
// Implicit Object→bool→ int cast
// bool→intのキャストができるので
// 暗黙的にObject→bool→intとキャスト
Object(Qture) + 1; // → RTEST(Qtrue) + 1
// → 1 + 1
// → 2
37. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Custom type conversion
型変換のカスタマイズ
class Object {
// Deny explicit cast (C++11)
// 暗黙のキャストを禁止(C++11)
explicit operator bool() const {
return RTEST(rb_object_);
}
};
// Object(Qtrue) + 1; // → Compile error
38. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Custom type conversion
型変換のカスタマイズ
// Trap2(罠2)
class Object {
public:
// Used as implicit VALUE→Object cast
// 暗黙のVALUE→Objectキャストに使われる
Object(VALUE rb_object);
};
39. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Custom type conversion
型変換のカスタマイズ
// Trap2(罠2)
// VALUE is just a number(VALUEはただの数値)
typedef unsigned long VALUE;
Object identify(Object x) {return x;}
// Implicit VALUE→Object cast
// 暗黙的にVALUE→Objectとキャスト
identify(100); // == identify(Object(100));
40. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Custom type conversion
型変換のカスタマイズ
class Object {
// Deny explicit cast
// 暗黙のキャストを禁止
explicit Object(VALUE rb_object);
};
// identify(100); // → Compile error
41. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Custom type conversion
型変換のカスタマイズ
class Object {
operator VALUE() const {return rb_object_;}
};
// Convenient(便利)
rb_p(Object(Qnil)); // → rb_p(Qnil);
// Not compile error. Hmm...
Object(Qnil) + 1; // → Qnil + 1;
// → 8 + 1;
// → 9;
42. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ convenient feature5
C++の便利機能5
Template and specialization
テンプレートと特殊化
Consistent cast API
一貫性のあるキャストAPIに使える
✓
43. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Consistent cast API
一貫性のあるキャストAPI
namespace rb {
// Consistent cast API
// Example: rb::cast<int32_t>(Object(NUM2INT(10));
// Example: rb::cast<Object>(10);
// FYI: C++ cast syntax: static_cast<TYPE>(expression)
template <typename RETURN_TYPE,
typename ARGUMENT_TYPE>
inline RETURN_TYPE cast(ARGUMENT_TYPE object);
}
44. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Consistent cast API
一貫性のあるキャストAPI
template <> // rb::cast<int32_t>(Object(NUM2INT(10));
inline int32_t cast<int32_t, Object>(Object rb_object) {
return NUM2INT(rb_object);
}
template <> // rb::cast<Object>(10);
inline Object cast<Object, int32_t>(int32_t n) {
return Object(INT2NUM(n));
}
45. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Consistent cast API
一貫性のあるキャストAPI
// rb::cast<const char *>(Object(rb_str_new_cstr("X"));
template <> inline
const char *cast<const char *, Object>(Object rb_object) {
VALUE rb_object_raw = rb_object;
return StringValueCStr(rb_object_raw);
}
template <> inline // rb::cast<Object>("hello");
Object cast<Object, const char *>(const char *c_string) {
return Object(rb_str_new_cstr(c_string));
}
46. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ convenient feature6
C++の便利機能6
Initializer list (C++11)
初期化リスト(C++11)
"{A,B,...}" is customizable
「{A, B, ...}」をカスタマイズできる
✓
47. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Initializer list
初期化リスト
Object Object::send(
ID name_id,
std::initializer_list<VALUE> args);
// Ruby: "hello".send(:tr, "e", "E")
Object(rb_str_new_cstr("hello")).
send(rb_intern("tr"),
{rb_str_new_cstr("e"),
rb_str_new_cstr("E")});
48. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Initializer list
初期化リスト
Object Object::send(
ID name_id,
std::initializer_list<VALUE> args,
VALUE (*block)(VALUE data));
// Ruby: array.send(:collect) {true}
Object(array).
send(rb_intern("collect"),
{}, // Clear API than variable arguments
[](VALUE data) {return Qtrue;});
49. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Convenient API example2
便利なAPI例2
// C++: Ext++
rb::Object(rb_n).
send("step",
// Implicit Object→VALUE cast
{rb::cast<rb::Object>(10)},
[](VALUE i) {return rb_p(i)});
// n.step(10) {|i| p i}
50. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Convenient API example3
便利なAPI例3
// C++: Rice
#include <rice/Class.hpp>
static const char * // Not VALUE! (Auto convert)
rb_sample_hello(Rice::Object self) {
return "Hello";
}
extern "C" void Init_sample() {
Rice::define_class("Sample").
define_method("hello", &rb_sample_hello);
}
51. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ based API: Pros1
C++ベースのAPI:長所1
Easier to write than C
Cより書きやすい
Require C++11 or later
C++11以降なら
✓
52. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ based API: Pros2
C++ベースのAPI:長所2
Easy to use for C API users
既存のC APIを使っている人なら使いやすい
Use C directly incl macro
マクロも含めてCの機能を直接使える
✓
Don't need to migrate to all
convenient API at once
一気に書き換えるのではなく徐々に移行できる
✓
53. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ based API: Pros3
C++ベースのAPI:長所3
Easy to debug for C API users
既存のC APIを使っている人ならデバッグしやすい
Can use GDB/LLDB directly
GDB/LLDBを直接使える
GDB/LLDB have built-in C++ support
GDB/LLDBは組み込みでC++をサポート
✓
✓
54. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ based API: Pros4
C++ベースのAPI:長所4
Easy to optimize
最適化しやすい
[Feature #13434] better
method definition in C API
Cのメソッド定義APIの改良
✓
55. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Better method definition
メソッド定義の改良
Metadata for optimization
最適化のためにメタデータを付与
e.g.: Reduce memory allocations
例:メモリーアロケーションを減らす
✓
✓
Lazy method definition
必要になるまでメソッド定義を遅らせる
e.g.: Reduce start-up time
例:起動時間の高速化
✓
✓
56. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Argument metadata
引数のメタデータ
class Hello
# Default argument is just for
# example. Other metadata will
# be more useful for optimization.
def hello(name, message="world")
end
end
57. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Argument metadata
引数のメタデータ
// C++: Rice
cHello.
define_method(
"hello",
&hello,
(Rice::Arg("name"), // ↓Default
Rice::Arg("message")="world"));
58. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Lazy method definition
遅延メソッド定義
class X
def a; end # Not define yet
def b; end # Not define yet
end
x = X.new
x.a # Define #a and #b
59. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Lazy method definition
遅延メソッド定義
/* One of new C API ideas */
struct rb_method_entries entries[] = {
"a", ...,
"b", ...,
};
/* The definitions aren't defined at once. */
/* They are defined when the next method call. */
rb_define_method_with_table(rb_cX, entries);
60. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Lazy method definition
遅延メソッド定義
// C++ implementation sample
{
rb::Class("X").
// Don't call rb_define_method() yet.
define_method("a", ...).
// Don't call rb_define_method() yet.
define_method("b", ...);
// Destructor is called.
// Call rb_define_method_with_table()
// in destructor.
}
61. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Lazy method definition
遅延メソッド定義
// Ext++ implementation is just for test
rb::Class("X").
// Call rb_define_method() immediately.
define_method("a", ...).
// Don't call rb_define_method() in
// the following define_method()s.
enable_lazy_define_method().
// Don't call rb_define_method() yet.
define_method("b", ...);
62. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Lazy method definition
遅延メソッド定義
# Define only benchmark code in Ruby.
# Benchmark target code is in C++.
n = 10000
Bench = Class.new do
n.times do |i|
define_method("method#{i}") do
end
end
end
63. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Lazy method definition
遅延メソッド定義
# Call benchmark code in Ruby.
# Benchmark target code is in C++.
n = 10000
bench = Bench.new
n.times do |i|
bench.__send__("method#{i}")
end
64. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Lazy method definition
遅延メソッド定義
Type Define only Called
Normal 5ms 5ms
Lazy 1ms 5ms
5x faster when any methods
aren't called
メソッドが呼ばれなければ5倍速い
65. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ based API: Cons1
C++ベースのAPI:短所1
C++ is difficult
C++は難しい
e.g.: Template
たとえばテンプレート
✓
Easy to write unreadable code
簡単にリーダブルじゃないコードを書ける
✓
66. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ based API: Cons2
C++ベースのAPI:短所2
Slower build
ビルドが遅い
It may reduce try&error cycle
試行錯誤しにくくなるかも
✓
67. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
C++ based API: Problem
C++ベースのAPI:課題
Exception
例外
Ruby exception breaks C++
RAII (destructor)
(Resource Acquisition Is Initialization)
Rubyの例外発生→C++のRAII(デストラクター)が動かない
Because it uses setjmp/longjmp
Rubyの例外はsetjmp/longjmpを使っているから
✓
✓
68. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Exception: Solution
例外:解決法
Rescue Ruby exception
Rubyの例外をrescue
1.
Throw C++ exception
C++の例外にしてthrow
2.
Re-raise the Ruby exception
安全な場所でRubyの例外を再raise
3.
69. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Conclusion
まとめ
C++ based API is useful
C++ベースのAPIは便利
For writing ext uses C/C++ library
C/C++のライブラリを使う拡張ライブラリを書くとき
✓
For optimizing w/ easy to use API
例:使いやすいAPIを維持したまま最適化するとき
✓
✓
70. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Appendix
付録
Introduce easy to write
extension approaches
拡張ライブラリーを簡単に実装する方法を紹介
The following contents are
used only when time is
remained
以降の内容は時間が残っている場合だけ使う
✓
71. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Approaches(実現方法)
Extend language to support
writing extension
拡張ライブラリーを書けるように言語を拡張
✓
Not based on C
C以外の言語を使う
✓
Provide convenient API
便利APIを提供
✓
72. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Extend language
言語を拡張
Rubex: Extended Ruby
Rubex:Rubyを拡張
✓
Cython: Extended Python
Cython:Pythonを拡張
✓
73. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
How to run
動かし方
Translate extension code to C
拡張言語で書かれたコードをCにコンパイル
✓
Compile C code
コンパイルされたCコードをビルド
✓
Load the built extension
ビルドした拡張ライブラリーを読み込む
✓
74. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Extended syntax
拡張された構文
Type information
型情報を書ける
✓
C code snippet
Cのコードを書ける
✓
75. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
How to run: Rubex
Rubexの動かし方
# fibonacci.rubex
class Fibonacci
# "int" is type information
def compute(int n)
# ...
end
end
76. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
How to run: Rubex
Rubexの動かし方
% rubex fibonacci.rubex
% cd fibonacci
% ruby extconf.rb
% make
77. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
How to run: Rubex
Rubexの動かし方
require_relative "fibonacci.so"
p Fibonacci.new.compute(100)
78. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Extend language: Pros1
拡張言語:長所1
Friendly syntax for
base language users
ベースの言語のユーザーにはなじみやすい構文
Most syntax is the same
構文の大部分は同じだから
✓
79. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Extend language: Pros2
拡張言語:長所2
Easy to migrate from base lang
ベースの言語からの移行が容易
Because upward compatibility
上位互換だから
✓
Code for base language works
without modification
ベースの言語で書かれたコードは変更なしで動く
✓
80. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Extend language: Pros3
拡張言語:長所3
Doesn't require much C knowledge
そんなにCの知識は必要ない
Most code can be written with
base language knowledge
コードの大部分はベースの言語の知識で書ける
✓
81. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Extend language: Cons1
拡張言語:短所1
You realize that it's not
friendly syntax when you use it
使うとそんなになじみやすい文法ではないと気づく
Small differences will
confuse you
小さな違いがいろいろあってわかりにくい
✓
82. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Extend language: Cons2
拡張言語:短所2
Difficult to debug
デバッグが難しい
Need base language, extend
language and C knowledge
ベースの言語の知識も拡張言語の知識もCの知識も必要
✓
Cython has GDB integration to solve this problem
Cythonはこの問題を解決するためにGDB用の便利機能を提供
83. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Extend language: Cons3
拡張言語:短所2
Hard to maintain
(For maintainers, not for users)
(ユーザーではなくメンテナーが)メンテナンスが大変
Base language introduces a
new syntax then extend
language should implement it
ベースの言語が新しい構文を導入→拡張言語でも実装
✓
84. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Not based on C
C言語以外をベースにする
JRuby: Java + Ruby✓
Helix: Rust + Ruby✓
85. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
How to run: JRuby
JRubyでの動かし方
// Fibonacci.java
public class Fibonacci {
public long[] compute(int n) {
// ...
}
}
86. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
How to run: JRuby
JRubyでの動かし方
% javac Fibonacci.java
% jar cf fibonacci.jar Fibonacci.class
87. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
How to run: JRuby
JRubyでの動かし方
require "fibonacci.jar"
java_import "Fibonacci"
p Fibonacci.new.compute(100)
88. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Not based on C: Pros1
C言語以外をベースにする:長所1
Easier to write than C
Cより書きやすい
Simpler syntax
洗練された構文
✓
Rich features compared to C
Cより機能が多い
✓
89. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Not based on C: Pros2
C言語以外をベースにする:長所2
Can use libraries
in base language
ベース言語のライブラリーを使える
Major languages have many libs
広く使われている言語はライブラリーも多い
✓
90. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Not based on C: Cons1
C言語以外をベースにする:短所1
Need base language knowledge
ベース言語の知識が必要
Java for JRuby(JRubyならJava)✓
Rust for Helix(HelixならRust)✓
91. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Not based on C: Cons2
C言語以外をベースにする:短所2
May need C knowledge
(When Ruby implementation is MRI)
Rubyの実装がMRIならCの知識が必要かもしれない
Base language wraps Ruby C API
ベースの言語はRubyのC APIをラップしている
e.g.: sys::RSTRING_PTR on Helix
例:Helixならsys::RSTRING_PTRがラップしたAPI
✓
✓
92. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Not based on C: Cons3
C言語以外をベースにする:短所3
Hard to maintain
(When Ruby implementation is MRI)
Rubyの実装がMRIならメンテナンスが大変かも
Ruby introduces a new API
then base language may need
to implement it
Rubyが新しいAPIを追加→ベース言語でも実装?
e.g.: rb_gc_adjust_memory_usage()✓
✓
93. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Provide convenient API
便利なAPIを提供
Rice: C++ + Ruby✓
Ext++: C++11 + Ruby✓
Boost.Python: C++ + Python✓
pybind11: C++11 + Python✓
94. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
How to run: Rice
Riceの動かし方
#include <rice/Class.hpp>
static const char * // Not VALUE!
rb_sample_hello(Rice::Object self) {
return "Hello";
}
extern "C" void Init_sample() {
Rice::define_class("Sample").
define_method("hello", &rb_sample_hello);
}
95. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
How to run: Rice
Riceの動かし方
# extconf.rb
require "mkmf-rice"
create_makefile("sample")
96. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
How to run: Rice
Riceの動かし方
% ruby extconf.rb
% make
97. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
How to run: Rice
Riceの動かし方
require_relative "sample.so"
p Sample.new.hello
# => "Hello"
98. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Provide C++ API: Pros
C++ APIを提供:長所
Omit
省略
99. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Provide C++ API: Cons
C++ APIを提供:短所
Omit
省略
100. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
Provide C++ API: ConsN
C++ APIを提供:短所N
Conv from Ruby may be a bother
Ruby実装の移植が面倒
C++ with convenient Ruby C
API needs more code than Ruby
RubyのC APIにC++の便利APIがあっても
Rubyよりもたくさんコードが必要
✓
101. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
From Ruby: Rice
RubyからRiceに移植
def fib(n)
prev = 1
current = 1
1.step(n - 1).collect do
prev, current = current, current + prev
prev
end
end
102. Improve extension API - C++ as better language for extension Powered by Rabbit 2.2.2
From Ruby: Rice
RubyからRiceに移植
std::vector<uint64_t> fib(Rice::Object self, int n) {
uint64_t prev = 1, current = 1;
std::vector<uint64_t> numbers;
for (int i = 1; i < n; ++i) {
auto temp = current; current += prev; prev = temp;
numbers.push_back(prev);
}
return numbers;
}