From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 1160419E0040 for ; Mon, 28 Dec 2015 07:27:54 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 3FA57B5D885 for ; Mon, 28 Dec 2015 08:00:28 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 6C17218CC7EB for ; Mon, 28 Dec 2015 08:00:28 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id DE7D31205AC; Mon, 28 Dec 2015 08:00:26 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o10.shared.sendgrid.net (o10.shared.sendgrid.net [173.193.132.135]) by neon.ruby-lang.org (Postfix) with ESMTPS id 87EED1205A2 for ; Mon, 28 Dec 2015 08:00:23 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=nHQcguqrN7P4wWXM3suKYIEQZoA=; b=G55fStk+cGMsLe109F isGQjajZSR64YT5+cJMh5lm/0ViSw3mAIWFfdbc5VRhf6nq7aCfy1ySojN+FlKTb AWVWroecJMcsFGP2Q6aA24SJ6iNiLydVdfw3Ddh8op/cPXvvfo10WuJM2FuiysQ/ mLUOXndo4fvCVPFm8UxOs9g4A= Received: by filter0609p1mdw1.sendgrid.net with SMTP id filter0609p1mdw1.3181.56806D7E45 2015-12-27 23:00:14.842030203 +0000 UTC Received: from herokuapp.com (ec2-54-158-159-53.compute-1.amazonaws.com [54.158.159.53]) by ismtpd0006p1iad1.sendgrid.net (SG) with ESMTP id dVoCtVURRiSKJqarrVX3Yg for ; Sun, 27 Dec 2015 23:00:14.788 +0000 (UTC) Date: Sun, 27 Dec 2015 23:00:14 +0000 From: headius@headius.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 47127 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11339 X-Redmine-Issue-Author: normalperson X-Redmine-Sender: headius X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS6SRaIiz4c5B2F8iQAXYT2Cio2gz30iPlK0D6 YCeKqY9AG4DdxCRmfl+pu3Trlq/82V6kat04uwkZwVWWXRVYrGAN+4V6a5b1Wq9gH8uHuwYDIz20N6 BySG7WSI3lJkH+1yqFOpFIjBWCS+XlM2YL3LRWbwSDez1jaNUTfmrZ/Igw== X-ML-Name: ruby-core X-Mail-Count: 72528 Subject: [ruby-core:72528] [Ruby trunk - Feature #11339] [PATCH] io.c: avoid kwarg parsing in C API X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #11339 has been updated by Charles Nutter. A suggestion for how to make kwarg-passing to C functions allocation-free: have a thread-local (or perhaps global, since CRuby doesn't run Ruby code in parallel) array you can populate with the key/value pairs coming out of the VM. Methods that want to receive opts directly can specify it through a new rb_define_method form, and the requirement is that they must process those keyword arguments before doing anything else, so that global store can be re-used. That is just a quick and dirty way to do it, but it would eliminate the need for hacks like this and make kwarg passing to C functions nearly free, as it is from Ruby to Ruby. ---------------------------------------- Feature #11339: [PATCH] io.c: avoid kwarg parsing in C API https://bugs.ruby-lang.org/issues/11339#change-55792 * Author: Eric Wong * Status: Closed * Priority: Normal * Assignee: ---------------------------------------- ~~~ rb_scan_args and hash lookups for kwargs in the C API are clumsy and slow. Instead of improving the C API for performance, use Ruby instead :) Implement IO#read_nonblock and IO#write_nonblock in prelude.rb to avoid argument parsing via rb_scan_args and hash lookups. This speeds up IO#write_nonblock and IO#read_nonblock benchmarks in both cases, including the original non-idiomatic case where the `exception: false' hash is pre-allocated to avoid GC pressure. Now, writing the kwargs in natural, idiomatic Ruby is fastest. I've added the noex2 benchmark to show this. target 0: a (ruby 2.3.0dev (2015-07-08 trunk 51190) [x86_64-linux]) at "a/ruby" target 1: b (ruby 2.3.0dev (2015-07-08 nonblock-kwarg 51190) [x86_64-linux]) at "b/ruby" ----------------------------------------------------------- raw data: [["io_nonblock_noex", [[2.5436805468052626, 2.5724728293716908, 2.4915440678596497], [2.478000810369849, 2.4285155069082975, 2.462410459294915]]], ["io_nonblock_noex2", [[3.012514788657427, 3.034533655270934, 2.9972082190215588], [2.135501991957426, 2.146781364455819, 2.0429874528199434]]]] Elapsed time: 30.348340944 (sec) ----------------------------------------------------------- benchmark results: minimum results in each 3 measurements. Execution time (sec) name a b io_nonblock_noex 2.492 2.429 io_nonblock_noex2 2.997 2.043 Speedup ratio: compare with the result of `a' (greater is better) name b io_nonblock_noex 1.026 io_nonblock_noex2 1.467 Note: I plan to followup commits for other *_nonblock methods Eventually, I even wish to deprecate rb_scan_args :D For what it's worth, I'm more excited about this change than usual and hope to use prelude.rb more. ~~~ ---Files-------------------------------- 0001-io.c-avoid-kwarg-parsing-in-C-API.patch (6.88 KB) -- https://bugs.ruby-lang.org/