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

speed up OrderedPSet.minus() #98

Merged
merged 1 commit into from
Sep 16, 2022
Merged

speed up OrderedPSet.minus() #98

merged 1 commit into from
Sep 16, 2022

Conversation

hrldcpr
Copy link
Owner

@hrldcpr hrldcpr commented Sep 16, 2022

As reported in #95 OrderedPSet.minus() was linear, so this makes it logarithmic, by switching from a vector to a map.

Benchmarks shows no performance regression for other methods, while ordered_pset_plus_minus_reverse is sped up significantly (and this is just for 1000 elements, for more elements the speedup would be even better):

=Old=
contains                                LINEAR     61.398  ops/s
contains                                RANDOM     80.600  ops/s
iterator                                LINEAR  62685.475  ops/s
iterator                                RANDOM  65744.581  ops/s
notContains                             LINEAR     60.679  ops/s
notContains                             RANDOM     59.028  ops/s
ordered_pset_plus                       LINEAR   1498.706  ops/s
ordered_pset_plus                       RANDOM   1537.080  ops/s
ordered_pset_plus_minus                 LINEAR    737.462  ops/s
ordered_pset_plus_minus                 RANDOM    760.693  ops/s
ordered_pset_plus_minus_reverse         LINEAR     72.744  ops/s
ordered_pset_plus_minus_reverse         RANDOM     72.104  ops/s

=New=
contains                                LINEAR     55.332  ops/s
contains                                RANDOM    109.640  ops/s
iterator                                LINEAR  67770.126  ops/s
iterator                                RANDOM  71336.923  ops/s
notContains                             LINEAR     63.026  ops/s
notContains                             RANDOM     63.748  ops/s
ordered_pset_plus                       LINEAR   1702.410  ops/s
ordered_pset_plus                       RANDOM   1804.350  ops/s
ordered_pset_plus_minus                 LINEAR    839.788  ops/s
ordered_pset_plus_minus                 RANDOM    827.643  ops/s
ordered_pset_plus_minus_reverse         LINEAR    898.002  ops/s
ordered_pset_plus_minus_reverse         RANDOM    873.489  ops/s

(Note these ops/s are actually thousands of operations per second.)

This change means get() and indexOf() can't be efficient anymore, but they should probably have never been there in the first place, since the analogous class LinkedHashSet doesn't have them, so this removes them. Thus this is a breaking change, so it can be part of the upcoming major version release.

Closes #95

…t() and indexOf() can't be efficient anymore, but they should probably have never been there in the first place, since the analogous class LinkedHashSet doesn't have them. increment major version since this is a breaking change
@hrldcpr
Copy link
Owner Author

hrldcpr commented Sep 16, 2022

fyi @prdoyle

last thing is to add an OrderedPMap, which I can do in a separate PR

@hrldcpr hrldcpr merged commit 57153b6 into master Sep 16, 2022
@hrldcpr hrldcpr deleted the better-ordered-set branch September 16, 2022 04:33
@prdoyle
Copy link
Contributor

prdoyle commented Sep 17, 2022

@hrldcpr nice! Are you already working on the rest of #93? I was thinking of doing that this weekend but didn't want to duplicate effort.

@prdoyle
Copy link
Contributor

prdoyle commented Sep 17, 2022

Oh wow, you already did #93. Never mind!

if (contents.contains(e)) return this;
return new OrderedPSet<E>(contents.plus(e), order.plus(e));
if (ids.containsKey(e)) return this;
final Long id = elements.isEmpty() ? -Long.MIN_VALUE : (elements.lastKey() + 1);
Copy link
Contributor

@prdoyle prdoyle Sep 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hrldcpr -Long.MIN_VALUE equals just Long.MIN_VALUE due to overflow. But I think you meant MIN_VALUE anyway, so I suppose this "bug" is harmless?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha whoops, good find, thanks! I'm glad it still works but yeah that was definitely not what I intended

@hrldcpr hrldcpr mentioned this pull request Sep 20, 2022
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 this pull request may close these issues.

OrderedPSet.minus is linear time
2 participants