cloudspokes / arena-web

Web arena for SRMs
44 stars 32 forks source link

Practice Problem BinaryCode takes over 2 minutes to load. #1099

Closed Goodwine closed 9 years ago

Goodwine commented 9 years ago

The problem BinaryCode is taking way too long to load, I tried the other problems, and I don't see an issue with them. I pinpointed the issue by profiling the JS code, and it showed that the function updateCoderPlacement in resolvers.js is causing the bottleneck.

this is how the code looks like:

updateCoderPlacement = function (coders) {
   coders.forEach(function (item) {
        item.roomPlace = 1;
        coders.forEach(function (other) {
            if (item.userName !== other.userName && item.totalPoints < other.totalPoints) {
                item.roomPlace += 1;
            }
        });
    });
};

This is the bottleneck. If I'm not wrong, this solution is bubbling down every coder according to their points. BinaryCode has 34760 coders, and the function takes 1.4 ~ 2.1 minutes, which is the result of a O(n2) bubbling algorithm.

skyhit commented 9 years ago

any suggestion for improvement?

Goodwine commented 9 years ago

I have 2, actually:

  1. If the original order of the array doesn't matter, perhaps sort by totalPoints on descendant order, and then for every element, assign the roomPlace. (if the order matters, we could "go back" to it's original order if we wanted to, with a little extra work).
  2. This is not even being used anywhere on the Practice Problems, so why load them? Will it be a feature implemented later at the web arena? (my guess is that it will be used later, but if not... we could just remove it)

If the first suggestion is ok, could I send a pull request? And if so.. Is the order important? (I haven't found anywhere that suggests that)

mdesiderio commented 9 years ago

This issue was moved to appirio-tech/arena-web#8