Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed improvement : performance of #atRandom: in class Bag #5392

Closed
bstjean opened this issue Dec 18, 2019 · 8 comments · Fixed by #5630
Closed

Proposed improvement : performance of #atRandom: in class Bag #5392

bstjean opened this issue Dec 18, 2019 · 8 comments · Fixed by #5630

Comments

@bstjean
Copy link

bstjean commented Dec 18, 2019

Whenever using Bag instances with a LARGE number of objects (like millions), #atRandom becomes less and less useable since it almost crawls to a halt. The problem resides in the fact that the Bag class inherits #atRandom: (which is called by #atRandom) from Collection and the current implementation of Collection>>#atRandom: uses #do: to iterate over EVERY item (virtual) of the bag. A faster method would be to use the occurrences of every object in the bag to iterate only over the keys in the dictionary instead of iterating over all the "virtual" items contained in the bag.

I have included a fix (see attached file).

Here are some example timings to show the performance difference:

Tests WITHOUT #atRandom: override!

Very good case : evaluated #atRandom 200 times in 17.27 seconds for 3000000 elements!
Average case : evaluated #atRandom 200 times in 193.38 seconds for 3000000 elements!
Worst case : evaluated #atRandom 200 times in 283.19 seconds for 3000000 elements!

Tests WITHOUT #atRandom: override!

Very good case : evaluated #atRandom 200 times in 16.26 seconds for 3000000 elements!
Average case : evaluated #atRandom 200 times in 185.52 seconds for 3000000 elements!
Worst case : evaluated #atRandom 200 times in 295.37 seconds for 3000000 elements!


Tests WITH #atRandom: override!

Very good case : evaluated #atRandom 200 times in 12.76 seconds for 3000000 elements!
Average case : evaluated #atRandom 200 times in 37.89 seconds for 3000000 elements!
Worst case : evaluated #atRandom 200 times in 44.79 seconds for 3000000 elements!

Tests WITH #atRandom: override!

Very good case : evaluated #atRandom 200 times in 12.61 seconds for 3000000 elements!
Average case : evaluated #atRandom 200 times in 37.93 seconds for 3000000 elements!
Worst case : evaluated #atRandom 200 times in 44.73 seconds for 3000000 elements!
Bag-atRandom.zip

@welcome
Copy link

welcome bot commented Dec 18, 2019

Thanks for opening your first issue! Please check the CONTRIBUTING documents for some tips about which information should be provided. You can find information of how to do a Pull Request here: https://github.com/pharo-project/pharo/wiki/Contribute-a-fix-to-Pharo

GitHub
Pharo is a dynamic reflective pure object-oriented language supporting live programming inspired by Smalltalk. - pharo-project/pharo

@svenvc
Copy link
Contributor

svenvc commented Dec 18, 2019

Why do you make it so hard to look at what you propose ? A zip file of a single method ?

Anyway, this is what you are proposing:

Bag>>#atRandom: aGenerator
	"Answer a random element of the receiver. Uses aGenerator which
    should be kept by the user in a variable and used every time. Use
    this instead of #atRandom for better uniformity of random numbers because 
	only you use the generator. Causes an error if self has no elements."
	| rand index |

	self emptyCheck.
	rand := aGenerator nextInt: self size.
	index := 0.
	self doWithOccurrences: [:key :count | 	index := index + count.
														rand <= index ifTrue: [^key]].
	^ self errorEmptyCollection! !

I don't think this would be correct: says a bag contains 1000 times 1 and 10 times 2. Your approach would return 1 and 2 with each a 50% change, which does not seem to be correct since 1 occurs 100 times more frequently, and should thus be picked 100 more at random.

@bstjean
Copy link
Author

bstjean commented Dec 18, 2019 via email

@bstjean
Copy link
Author

bstjean commented Dec 18, 2019 via email

@svenvc
Copy link
Contributor

svenvc commented Dec 18, 2019

OK, now I see. I was thrown off by the weird formatting ;-)

The final expression is not needed, since you started with an emptyCheck.

Reformatted then:

Bag>>#atRandom: aGenerator
	"Answer a random element of the receiver. Uses aGenerator which
	should be kept by the user in a variable and used every time. Use
	this instead of #atRandom for better uniformity of random numbers because 
	only you use the generator. Causes an error if self has no elements."

	| rand index |
	self emptyCheck.
	rand := aGenerator nextInt: self size.
	index := 0.
	self doWithOccurrences: [ :key :count | 
		index := index + count.
		rand <= index ifTrue: [ ^ key ] ]

Still, we need a PR as well. And maybe a specific test unless this is already covered by other tests.

@bstjean
Copy link
Author

bstjean commented Dec 18, 2019

True! I just picked the original and changed the #do: loop to use #doWithOccurrences: ! To really see a difference, you'd need a big bag (1M+ works really fine) to notice the significant improvement! And it would be difficult to create a test to compare a method that exists to a method that is no longer there. Anyone got suggestions besides compiling and removing code on the fly ?

@svenvc
Copy link
Contributor

svenvc commented Dec 18, 2019

I did not mean testing the performance, that is too hard.
I meant testing the correctness of your change (for example the fact that the loop always answers and does not fall through), to make sure your improvement does not break anything unforeseen.

@svenvc
Copy link
Contributor

svenvc commented Jan 6, 2020

#5445

MarcusDenker added a commit to MarcusDenker/pharo that referenced this issue Feb 5, 2020
…e in Pharo8, I will open a new issue for further suggested improvements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants