Route params no longer symbols

33 views
Skip to first unread message

Adam Meehan

unread,
Apr 2, 2008, 1:51:00 AM4/2/08
to sinatrarb
Hi,

Just getting into using Sinatra and playing around. Thanks for great
work.

Using the current master branch README as the current guide I was
tripped up by the route params now being string keys and not symbols
in the params hash.

I noticed your log entry mentioned that symbols are not garbage
collected as the reason to move to string keys. But I find that the
use of the symbol syntax in the route and then using a string key in
the params hash grates somewhat.

Given that symbols are reused once defined, I would have thought that
the amount of symbols used in route params for typical Sinatra app
would be small and add little memory overhead.

Is this not what you have found?

Adam

Blake Mizerany

unread,
Apr 2, 2008, 3:36:25 AM4/2/08
to sina...@googlegroups.com
Adam,

It's about 2 things:

  1.  Converting every params hash to symbolize slows down request times.  Not a biggie for most of us but a benifit of not using them
  2.  Because symbols are not garbage collected, blindly symbolizing keys means that your open to an attack where someone sends random params keys and eats up memory.  

I'm looking into another way to get them back without using the bulky HWIA.

Please let me know any ideas.

-blake
--
Blake Mizerany
blake.m...@gmail.com

Adam Meehan

unread,
Apr 2, 2008, 4:14:18 AM4/2/08
to sinatrarb
Thanks Blake.

1. Given the intended use for Sinatra I certainly understand the speed
bias.

2. Interesting point. I wonder what Rails does. Will have a look.

Agreed HWIA would nice to avoid.

Cheers,
Adam.
> blake.mizer...@gmail.com

Blake Mizerany

unread,
Apr 2, 2008, 2:37:25 PM4/2/08
to sina...@googlegroups.com
Rails uses HWIA.
--
Blake Mizerany
blake.m...@gmail.com

Adam Meehan

unread,
Apr 2, 2008, 4:24:22 PM4/2/08
to sinatrarb
Yes of course. derr!
> blake.mizer...@gmail.com

Jeremy Evans

unread,
Apr 3, 2008, 5:47:53 PM4/3/08
to sinatrarb
On Apr 2, 12:36 am, "Blake Mizerany" <blake.mizer...@gmail.com> wrote:
> Adam,
> It's about 2 things:
>
> 1. Converting every params hash to symbolize slows down request times.
> Not a biggie for most of us but a benifit of not using them
> 2. Because symbols are not garbage collected, blindly symbolizing keys
> means that your open to an attack where someone sends random params keys and
> eats up memory.
>
> I'm looking into another way to get them back without using the bulky HWIA.
>
> Please let me know any ideas.

You can get a cheap version of a hash that accepts symbols without
HWIA:

hwia = Hash.new{|h,k| h[k.to_s] if Symbol === k}

Jeremy

Blake Mizerany

unread,
Apr 3, 2008, 10:57:20 PM4/3/08
to sina...@googlegroups.com
sweet. I'll get that in.  thx
--
Blake Mizerany
blake.m...@gmail.com

Blake Mizerany

unread,
Apr 8, 2008, 7:42:28 PM4/8/08
to sina...@googlegroups.com
K.  Symbol params are back in thanks to Jeremy and his helpful tidbit.

http://github.com/bmizerany/sinatra/commit/98572b4d0d406c7f1e7a1d9100887c4b2fb07f9e

On Thu, Apr 3, 2008 at 2:47 PM, Jeremy Evans <jeremy...@gmail.com> wrote:



--
Blake Mizerany
blake.m...@gmail.com

Adam Meehan

unread,
Apr 8, 2008, 7:46:46 PM4/8/08
to sinatrarb
Good work. Thanks Jeremy!

On Apr 9, 9:42 am, "Blake Mizerany" <blake.mizer...@gmail.com> wrote:
> K. Symbol params are back in thanks to Jeremy and his helpful tidbit.
>
> http://github.com/bmizerany/sinatra/commit/98572b4d0d406c7f1e7a1d9100...
>
>
>
> On Thu, Apr 3, 2008 at 2:47 PM, Jeremy Evans <jeremyeva...@gmail.com> wrote:
>
> > On Apr 2, 12:36 am, "Blake Mizerany" <blake.mizer...@gmail.com> wrote:
> > > Adam,
> > > It's about 2 things:
>
> > > 1. Converting every params hash to symbolize slows down request
> > times.
> > > Not a biggie for most of us but a benifit of not using them
> > > 2. Because symbols are not garbage collected, blindly symbolizing
> > keys
> > > means that your open to an attack where someone sends random params keys
> > and
> > > eats up memory.
>
> > > I'm looking into another way to get them back without using the bulky
> > HWIA.
>
> > > Please let me know any ideas.
>
> > You can get a cheap version of a hash that accepts symbols without
> > HWIA:
>
> > hwia = Hash.new{|h,k| h[k.to_s] if Symbol === k}
>
> > Jeremy
>
> --
> Blake Mizerany
> blake.mizer...@gmail.com
Reply all
Reply to author
Forward
0 new messages