Skip to content

Enhance Update Functions #25

@PauSolerValades

Description

@PauSolerValades

Hello :)

I needed a set implementation for a Discrete Event Simulation and this is an amazing library, thank you for your work :)

In the process I've detected an improvement to the actionUpdate functions, specifically symmetricDifferenceUpdate, intersectionUpdate and differenceUpdate. All of those functions allocate memory for a copy of a given set due to the inability to modify the an element of the set while iterating over it. I rewrote the three functions changing the strategy:

  • differenceUpdate: Instead of iterating over the original set, by iterating over the other you can remove from the original, which eliminates the duplication completely, allowing to not use an allocator!
  • intersectionUpdate: Here the allocator cannot be removed but it can be done slightly more efficient. By using an ArrayList with the elements to remove (not in the intersection) you minimize the memory footprint. Then, you iterate over the arraylist to remove them from the original set, once you've finished iterating over it.
  • symmetricDifferenceUpdate: Same idea from differenceUpdate, iterating over the other set allows to modify the original.

I've tested the implementation performance wise with two sets $A = { 1, \ldots, 100000}, B = { 1, \ldots, 50000 }$, and the improvements in my machine are the following, ran 1000 times, tho I am quite noob at benchmarking so I might have fucked up something xd

Original:
Difference (ms) : 11.0510 +/- 0.037741 (95% CI)
Sym Difference (ms) : 12.2240 +/- 0.045730 (95% CI)
Intersection (ms) : 9.1530 +/- 0.037801 (95% CI)

New
Difference (ms) : 1.0090 +/- 0.005856 (95% CI)
Sym Difference (ms) : 1.0750 +/- 0.017906 (95% CI)
Intersection (ms) : 6.1710 +/- 0.030364 (95% CI)

I would gladly open a pull request to contribute the changes or show the benchmarking code. I forked the repo and changed some things, but I can port this changes to keep them in order :) have a nice day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions