[#385051] How to order Structs based on two fields — Iñaki Baz Castillo <ibc@...>

Hi, I've a struct like this:

20 messages 2011/07/01
[#385052] Re: How to order Structs based on two fields — Iñaki Baz Castillo <ibc@...> 2011/07/01

2011/7/1 I=C3=B1aki Baz Castillo <ibc@aliax.net>:

[#385057] Re: How to order Structs based on two fields — Robert Klemme <shortcutter@...> 2011/07/01

On Fri, Jul 1, 2011 at 12:55 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrote=

[#385069] Offering Help — Joe Peric <peric.joe@...>

I'm new to Ruby and I am willing to learn. Anyone need help on a

18 messages 2011/07/01
[#385072] Re: Offering Help — Jeremy Heiler <jeremyheiler@...> 2011/07/01

On Fri, Jul 1, 2011 at 11:43 AM, Joe Peric <peric.joe@gmail.com> wrote:

[#385073] Re: Offering Help — Joe Peric <peric.joe@...> 2011/07/01

Never heard of it, but I'll get right on it. Thanks.

[#385082] Re: Offering Help — Joe Peric <peric.joe@...> 2011/07/02

Just finished this. I had to find some more info on exceptions to get

[#385083] Re: Offering Help — Jeremy Heiler <jeremyheiler@...> 2011/07/02

On Sat, Jul 2, 2011 at 1:39 PM, Joe Peric <peric.joe@gmail.com> wrote:

[#385102] symbol vs string for hash keys — Intransition <transfire@...>

What are the important factors to consider when deciding whether to

12 messages 2011/07/03

[#385160] Fatal Error with Ruby/GTK2, Homebrew, Green Shoes, and OSX Snow Leopard — Timothy Barnes <barnes7td@...>

I posted the below question to the Shoes mailing list and got back the

10 messages 2011/07/09

[#385194] RubyInstaller Extras — Jon <jon.forums@...>

Since 2009, we've been pleased to include Huw Collingbourne's The Book of Ruby with RubyInstaller. Well now, Huw has a newly updated version, and he's published it with No Starch Press.

11 messages 2011/07/12

[#385207] Creating a range of directories — Simon Harrison <simon@...>

I'm wanting to create a range of directories with a prefix, like the

13 messages 2011/07/13

[#385276] Is there a more efficient way to remove data from a string? — Eddie Catflap <catflaporama@...>

Hi

9 messages 2011/07/15

[#385297] Help needed for a new release of text-hyphen — Austin Ziegler <halostatue@...>

I've had folks asking me for a release of text-hyphen that works with

12 messages 2011/07/15

[#385326] Comparing hashes based on their keys — Stefano Mioli <stefano.mioli@...>

Hi list,

13 messages 2011/07/15

[#385342] moving a 1.8.6 install? — Roggie Boone <rogboone@...>

Is it possible to move the gems from a Ruby 1.8.6 install on an (old)

10 messages 2011/07/15

[#385410] Better name for #zip_with — Intransition <transfire@...>

Any ideas for a better name for:

13 messages 2011/07/18

[#385452] Ruby + USB + CCTV Camera Driver? — "Bob Carpenter." <rgc3679@...>

I want to write a Ruby driver that handles the feed from A CCTV camera.

23 messages 2011/07/18
[#385459] Re: Ruby + USB + CCTV Camera Driver? — William Rutiser <wruyahoo05@...> 2011/07/18

On 2011-07-18 1:00 PM, Bob Carpenter. wrote:

[#385461] Re: Ruby + USB + CCTV Camera Driver? — "Bob Carpenter." <rgc3679@...> 2011/07/18

William Rutiser wrote in post #1011466:

[#385462] Re: Ruby + USB + CCTV Camera Driver? — Hassan Schroeder <hassan.schroeder@...> 2011/07/18

On Mon, Jul 18, 2011 at 11:51 AM, Bob Carpenter. <rgc3679@yahoo.com> wrote:

[#385464] Re: Ruby + USB + CCTV Camera Driver? — "Bob Carpenter." <rgc3679@...> 2011/07/18

Hassan Schroeder wrote in post #1011472:

[#385471] Re: Ruby + USB + CCTV Camera Driver? — Hassan Schroeder <hassan.schroeder@...> 2011/07/18

On Mon, Jul 18, 2011 at 12:58 PM, Bob Carpenter. <rgc3679@yahoo.com> wrote:

[#385474] Re: Ruby + USB + CCTV Camera Driver? — Phillip Gawlowski <cmdjackryan@...> 2011/07/18

On Tue, Jul 19, 2011 at 12:18 AM, Hassan Schroeder

[#385477] Re: Ruby + USB + CCTV Camera Driver? — "Bob Carpenter." <rgc3679@...> 2011/07/19

Phillip Gawlowski wrote in post #1011503:

[#385509] A beginner question. — "Cajus A." <cajus_@...>

Hi i'm trying to write and example program where you speak to a

12 messages 2011/07/19

[#385542] Splitting strings — Ryan Mckenzie <ryan@...>

Hi all,

18 messages 2011/07/19

[#385565] error building ruby p290 — Dev Guy <devguy.ca@...>

I get the following build error on my Linux box while trying to build

12 messages 2011/07/20

[#385629] Are Job Ads OK? — Lee Hughes <lee@...>

Is it OK to post Ruby job ads on this forum?

12 messages 2011/07/21

[#385847] String manuplation example — kotin 76 <kotin76@...>

Hi,

15 messages 2011/07/27

[#385860] Counting lines of code in a ruby code file ignoring method code lines in the process — Harry Truax <htruax@...>

Hello,

8 messages 2011/07/27

[#385936] Help in Pointer — Tridib Bandopadhyay <tridib04@...>

Hello

18 messages 2011/07/29

[#386012] Ruby a "4th generation language"? Credible sources? — Albert Schlef <albertschlef@...>

I've noticed that Wikipedia says that "C#, Java" are 3'rd generation

13 messages 2011/07/31

[#386050] system stack error in ObjectSpace.each_object — luke gruber <luke.gru@...>

Hey guys, I'm just starting to mess around with metaprogramming in Ruby,

14 messages 2011/07/31
[#386052] Re: system stack error in ObjectSpace.each_object — Josh Cheek <josh.cheek@...> 2011/07/31

On Sun, Jul 31, 2011 at 4:29 PM, luke gruber <luke.gru@gmail.com> wrote:

[#386055] Re: system stack error in ObjectSpace.each_object — luke gruber <luke.gru@...> 2011/08/01

> Typically, SystemStackError is due to recursing for too long, perhaps I

[#386058] Re: system stack error in ObjectSpace.each_object — Robert Klemme <shortcutter@...> 2011/08/01

On Mon, Aug 1, 2011 at 3:56 AM, luke gruber <luke.gru@gmail.com> wrote:

Re: Why aren't string literals treated similarly to symbols?

From: Jeremy Bopp <jeremy@...>
Date: 2011-07-26 14:31:59 UTC
List: ruby-talk #385821
On 7/26/2011 02:28, Robert Klemme wrote:
> Jeremy Bopp wrote in post #1013041:
>> Explaining the difference between symbols and strings got me wondering
>> why string literals aren't treated similarly to symbols by the
>> interpreter.  To be clear it seems that string literals could be
>> allocated 1 time each, where duplicates simply reference the first
>> created instance.
> 
> Actually this is what happens with COW underneath.  Still you get 
> multiple instances which is correct and needed (see below).

Thanks for your response, Robert.

Yes, this makes perfect sense because a string literal is mutable like
any other string.  Each string literal could be modified at any time, so
the safe and possibly only solution is to treat each on as if it *will*
be modified and give it its own instance.

>> The trick would be to make each reference to the literal a dup of the
>> singular, hidden instance.  With the COW semantics that dup'd strings
>> have (or should have), this should be a more memory efficient way to
>> deal with programs that have many instances of a string literal.
> 
> This is what happens.  Note, it's not a dup of the reference but a dup 
> of the instance (i.e. like CONSTANT_STRING.dup was called).

Right.  Thanks for clarifying.  It's good to know that the string data
itself isn't being duplicated, even if it may typically be rare for
anything aside from small strings to be used repeatedly at literals.

>> One such possibly common place that could benefit from this optimization
>> would be within frequently called methods or iterated loops that
>> directly use string literals for read-only purposes.  If a developer
>> wants to avoid the allocation problem of string literals as things
>> currently stand, he/she would have to be aware of this problem first and
>> then find ways to push his/her literals into constants or other exterior
>> references.
>>
>> For example:
>>
>> 1.upto(2) do
>>   string = "example"
>>   puts string
>> end
>>
>> Unless I misunderstand the current implementation in MRI, a loop such as
>> the one above would needlessly create a new instance of "good" for every
>> pass through the loop.
> 
> You probably meant "example" instead of "good".

Yes.  There was a little of my earlier explanation message leaking
through.  Late night emails...

> And you are wrong about 
> "needless".  To elaborate what Matz said: your suggestion will break 
> with this code
> 
> irb(main):004:0> a=[]
> => []
> irb(main):005:0> 3.times {|i| a << ("foo" << i.to_s)}
> => 3
> irb(main):006:0> a
> => ["foo0", "foo1", "foo2"]
> 
> It will behave like this
> 
> irb(main):007:0> a=[]
> => []
> irb(main):008:0> s="foo"
> => "foo"
> irb(main):009:0> 3.times {|i| a << (s << i.to_s)}
> => 3
> irb(main):010:0> a
> => ["foo012", "foo012", "foo012"]

My actual suggestion, whether I conveyed it clearly or not, was to work
with dups. :-)  However, even dups require allocation, so we wouldn't
save much at all in the end.  I should have slept on my suggestion a bit
before sending it out.

In my example above, though, the dup *is* needless because the string is
not modified, and that was the point.  That code could safely be trusted
to use the original string literal instance, but handling this in the
general case where the literal might be modified would probably be
non-trivial and maybe even impossible without some cheating by the
interpreter.

>> One possible fix would be something like:
>>
>> string = "example"
>> 1.upto(2) do
>>   puts string
>> end
>>
>> In this trivial case, the solution is good enough, but the general case
>> requires more knowledge about how to optimize for the interpreter than
>> should be necessary.  I'm probably missing something obvious, so if
>> anyone knows what it is, please point it out.  Maybe MRI already does
>> this obvious thing.
> 
> It's doing COW already internally.
> http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_9_2/string.c?revision=31809&view=markup
> 
> See e.g. lines 1984ff.  Then functions
> 
> rb_str_resize
>   rb_str_modify
>     str_independent
>     str_make_independent

Thanks for the pointer here.  I'll try to set aside some time to study
this code.

I'm trying to think of a way to allow the interpreter to cheat with
regard to string literals such that until the program attempts to modify
the literal or otherwise pierce the veil (calling object_id or similar)
the program actually uses the literal's object instance directly.  This
would allow the interpreter to avoid allocating extra string objects for
literals until they actually need to be modified or attributes such as
the object identity need to be known.  I'm not sure this is possible or
would even yield any performance benefits.  It's more of a curiosity now.

Thanks again for your help.

-Jeremy

In This Thread