Comments (5)
Have you tried benchmarks just using a plain hash, instead of an OrderedHash? If OrderedHash is slowing things down, we could easily figure out a way to get rid of it. For example, calling all
doesn't necessarily need to return things in the order added, so we could add sorts where necessary for all.
In that case another thing we should benchmark is the all
method to make sure we're not making it too slow.
from active_hash.
Here are results for a plain hash. Ruby 1.9:
~/code/active_hash 1.9.3@active_hash [hash_finder]$ ruby benchmark.rb
user system total real
10th record id 0.000000 0.000000 0.000000 ( 0.000980)
10th record where 9.990000 0.010000 10.000000 ( 10.007341)
1000th record id 0.000000 0.000000 0.000000 ( 0.000793)
1000th record where 10.000000 0.010000 10.010000 ( 10.008452)
and Ruby 1.8:
~/code/active_hash 1.8.7@active_hash [hash_finder]$ ruby benchmark.rb
user system total real
10th record id 0.000000 0.000000 0.000000 ( 0.001573)
10th record where 20.420000 0.010000 20.430000 ( 20.430843)
1000th record id 0.010000 0.000000 0.010000 ( 0.001702)
1000th record where 21.140000 0.010000 21.150000 ( 21.146878)
No difference in find_by_id
, but where
improves to its original state. I'm surprised by the results in 1.9 since OrderedHash just passes everything up to its parent class, Hash. Maybe the extra requires are slowing it down?
I ran the same benchmark script, calling all
1000 times and the hash_finder branch is much slower than master:
~/code/active_hash 1.9.3@active_hash [hash_finder]$ ruby benchmark.rb
user system total real
all 0.810000 0.010000 0.820000 ( 0.821074)
vs.
~/code/active_hash 1.9.3@active_hash [master]$ ruby benchmark.rb
user system total real
all 0.000000 0.000000 0.000000 ( 0.000187)
Note that this is for returning 10,000 records. It's commensurately faster with a smaller dataset. But is it too slow?
from active_hash.
Turns out it was indeed too slow. I've taken another stab at it, this time retaining the @records
array but converting the ids
set to a full-fledged Hash that indexes a record's position in @records
. Some additional memory is required, but benchmarks are speedy across the board. Here's Ruby 1.9:
~/code/active_hash 1.9.2@active_hash [index_finder]$ ruby benchmark.rb
user system total real
10th record id 0.000000 0.000000 0.000000 ( 0.000743)
10th record where 9.930000 0.020000 9.950000 ( 9.945236)
1000th record id 0.000000 0.000000 0.000000 ( 0.000820)
1000th record where 10.010000 0.030000 10.040000 ( 10.037525)
all 0.000000 0.000000 0.000000 ( 0.000273)
and Ruby 1.8:
~/code/active_hash 1.8.7@active_hash [index_finder]$ ruby benchmark.rb
user system total real
10th record id 0.000000 0.000000 0.000000 ( 0.002309)
10th record where 19.330000 0.020000 19.350000 ( 19.373773)
1000th record id 0.000000 0.000000 0.000000 ( 0.001875)
1000th record where 19.710000 0.030000 19.740000 ( 19.762334)
all 0.000000 0.000000 0.000000 ( 0.000690)
I've pushed a new branch of master called index_finder
with these changes. It's currently passing all specs. Let me know what you think!
from active_hash.
Rock on! I'll take a look tonight to make sure it works with a few of my existing projects as a final check, and release a new version as long as everything looks good. Thanks for putting in that extra work at the end - it seems a lot better to not regress on performance.
from active_hash.
I merged that branch and released a new version (0.9.12). And I really like this fix.
In fact, I think you just opened up an interesting new feature, which is indexes - it wouldn't take long to create a declarative syntax like:
indexes :id, :name
And then creates separate indexes for each. But that's for later....
Thanks for putting in all the effort.
from active_hash.
Related Issues (20)
- Using ActiveHash::Associations and JSONAPISerializer at the same time will result NoMethodError. HOT 1
- Can a class that inherits from ActiveHash::Base be associated with another class that inherits from ActiveHash::Base? HOT 1
- The has_many :through Association does not get record HOT 6
- QUESTION: What's the performance of "find_by"? HOT 2
- Scopes are not chainable HOT 1
- Rails 7 finding a valid model association. HOT 6
- Rails 7 Compatibility HOT 4
- Make releases more often? HOT 3
- ActiveHash scope affecting self HOT 3
- Reflection Inheritance Issue HOT 4
- 3.2.0: Regression in `.where`? HOT 7
- No Method Error for find_by and find HOT 1
- `ActiveHash::Relation` returned by `ActiveHash#where` should be reported like as an array of `ActiveHash` items selected by `where` method, in Rails Console. HOT 3
- ActiveHash::Relation can not use respond_to? method HOT 3
- find_by with nil value will return an object with an empty string
- undefined method `find_by_key' for ActiveYamlBaseClass:Class Did you mean? find_by_id HOT 3
- Accessing belongs_to association without a key creates a useless query HOT 4
- "`has_many': wrong number of arguments (given 2, expected 1) (ArgumentError)" error is raised since 3.3.0 HOT 4
- Add column_names Method to ActiveHash
- `count` should support a block
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from active_hash.