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 AB92D19C02F7 for ; Wed, 25 Nov 2015 05:09:46 +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 81098B5D86C for ; Wed, 25 Nov 2015 05:40:50 +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 A5DC618CC7B8 for ; Wed, 25 Nov 2015 05:40:50 +0900 (JST) Received: from [221.186.184.76] (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id AE92A1204A5; Wed, 25 Nov 2015 05:40:48 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from dcvr.yhbt.net (dcvr.yhbt.net [64.71.152.64]) by neon.ruby-lang.org (Postfix) with ESMTP id 580C6120478 for ; Wed, 25 Nov 2015 05:40:45 +0900 (JST) Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 9992D633807; Tue, 24 Nov 2015 20:40:43 +0000 (UTC) Date: Tue, 24 Nov 2015 20:40:43 +0000 From: Eric Wong To: Ruby developers Message-ID: <20151124204043.GA7356@dcvr.yhbt.net> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-ML-Name: ruby-core X-Mail-Count: 71663 Subject: [ruby-core:71663] Re: [Ruby trunk - Feature #11735] [Open] Porting String#squish and String#squish! from Ruby on Rails' Active Support 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: , Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" > +static VALUE > +rb_str_squish_bang(VALUE str) > +{ > + static const char before_regex_source[] = "\\A[[:space:]]+"; > + static const char after_regex_source[] = "[[:space:]]+\\z"; > + static const char between_regex_source[] = "[[:space:]]+"; > + VALUE before_argv[] = { > + rb_reg_new(before_regex_source, sizeof before_regex_source - 1, 0), > + rb_str_new_cstr("") > + }; > + VALUE after_argv[] = { > + rb_reg_new(after_regex_source, sizeof after_regex_source - 1, 0), > + rb_str_new_cstr("") > + }; > + VALUE between_argv[] = { > + rb_reg_new(between_regex_source, sizeof between_regex_source - 1, 0), > + rb_str_new_cstr(" ") > + }; You could memoize these Regexps as static variables and use rb_gc_register_mark_object to keep them around so GC won't eat them. Allocating 3 regexps and 3 strings every call seems like a waste. You may also use the same Writing the equivalent Ruby code would only allocate the Regexps once. You can also auto-dedupe "" and " " strings with the magic "frozen_string_literal: true" comment in Ruby or rb_fstring_cstr function in C. > By the way, this is my first patch and my first time writing something > in C, so there might be something that does not look right to you. > I'll happy to revise this patch (and learn about C in the process!) > from your feedback. No worries; but personally (not speaking for the rest of ruby-core); I would prefer we use prelude.rb more and implement more things in Ruby rather than C. Also (definitely not speaking for anybody else in ruby-core); but the Redmine <-> ruby-core ML integration is the only reason I've been willing to participate in Ruby development. I'm not touching proprietary websites or running any GUI/JavaScript at all to work on Ruby or any other Free Software.