Git Product home page Git Product logo

fast_open_struct's Introduction

FastOpenStruct

FastOpenStruct is a reimplementation of Ruby's OpenStruct that avoids invalidating MRI's method caches on every instantiation.

It should be a drop in replacement for OpenStruct.

Build Status via Travis CI

Performance

FastOpenStruct is very fast when your OpenStructs tend to have static sets of attributes:

λ ruby -I./lib bench/attribute_lookup.rb
                     user     system      total        real
OpenStruct       0.730000   0.000000   0.730000 (  0.732052)
FastOpenStruct   0.220000   0.000000   0.220000 (  0.224770)
λ ruby -I./lib bench/attribute_assignment.rb
Attribute assignment:
                     user     system      total        real
OpenStruct       1.200000   0.000000   1.200000 (  1.196867)
FastOpenStruct   0.210000   0.000000   0.210000 (  0.210626)
λ ruby -I./lib bench/10_000_instantiations.rb
                     user     system      total        real
OpenStruct       0.630000   0.010000   0.640000 (  0.645598)
FastOpenStruct   0.240000   0.000000   0.240000 (  0.239307)

However, if you're using dynamically set attributes heavily, it's significantly slower:

λ ruby -I./lib bench/dynamic_attribute_lookup.rb
Dynamic attribute lookup:
                     user     system      total        real
OpenStruct       0.660000   0.000000   0.660000 (  0.661448)
FastOpenStruct   2.170000   0.010000   2.180000 (  2.174698)
λ ruby -I./lib bench/dynamic_attribute_assignment.rb
Dynamic attribute assignment:
                     user     system      total        real
OpenStruct       1.190000   0.000000   1.190000 (  1.198603)
FastOpenStruct   6.020000   0.010000   6.030000 (  6.015700)

Licence

Simplified BSD

Copyright (C) 2013 Charlie Somerville. All rights reserved.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: 

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

fast_open_struct's People

Contributors

haileys avatar judofyr avatar kachick avatar kimptoc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

fast_open_struct's Issues

Missing attribute raises error, whereas OpenStruct returns nil

Hi,

Guess this is one of those 'undocumented' features:

 def test_missing_key
    h = {FullName: "John Smith"}
    assert_equal nil, FastOpenStruct.new(h).UnknownName
  end

This throws an Exception with FastOpenStruct, but returns nil on OpenStruct.

Which do you think is correct?

As my code currently uses OpenStruct and relies on this, you can guess my preference... :)

Cheers,
Chris

Fast

I don't want to be a troll, but why is the gem called fast_open_struct, if it is only faster if a given set of conditions is met and otherwise it is significantly slower?

How am I supposed to use it as a drop in replacement when it's likely going to perform slower in some scenarios? Or maybe I am wrong and this is only an experiment?

Structs should be serializable

It would be very useful to be able to serialize fast open structs, since usual open structs allow this. For example, Marshal can be used with regular structs, but not with open ones.

$ irb
irb(main):001:0> require 'ostruct'
=> true
irb(main):002:0> Marshal.dump(OpenStruct.new)
=> "\x04\bU:\x0FOpenStruct{\x00"
irb(main):003:0> require 'fast_open_struct'
=> true
irb(main):004:0> Marshal.dump(FastOpenStruct.new)
TypeError: can't dump anonymous class #<Class:0x20055030>
        from (irb):4:in `dump'
        from (irb):4
        from /usr/bin/irb:12:in `<main>'

Fails when Inheritance is used

class B < FastOpenStruct

end

B.new :a => 1, :b =>2 

This returns:

NoMethodError: undefined method `[]' for nil:NilClass
from /Users/kashyapkmbc/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/fast_open_struct-0.1.0/lib/fast_open_struct.rb:24:in `new'

Am I missing something?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.