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

Implement bisect #2971

Merged
merged 7 commits into from
Oct 21, 2020
Merged

Implement bisect #2971

merged 7 commits into from
Oct 21, 2020

Conversation

Riju19
Copy link
Contributor

@Riju19 Riju19 commented Oct 17, 2020

This pull request is to add an implementation of bisection under utils and remove the usage of bisection from the node-bisection library.
It is the first part of the issue #2139

@codecov
Copy link

codecov bot commented Oct 17, 2020

Codecov Report

Merging #2971 into main will increase coverage by 0.00%.
The diff coverage is 91.42%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2971   +/-   ##
=======================================
  Coverage   87.58%   87.59%           
=======================================
  Files         235      236    +1     
  Lines       18800    18823   +23     
  Branches     4784     4787    +3     
=======================================
+ Hits        16466    16488   +22     
- Misses       2158     2159    +1     
  Partials      176      176           
Impacted Files Coverage Δ
src/components/shared/Reorderable.js 66.34% <ø> (ø)
src/profile-logic/profile-data.js 89.46% <25.00%> (-0.10%) ⬇️
src/components/shared/thread/ActivityGraphFills.js 91.11% <100.00%> (ø)
src/components/shared/thread/StackGraph.js 93.81% <100.00%> (ø)
src/components/timeline/TrackEventDelayGraph.js 98.09% <100.00%> (ø)
src/components/timeline/TrackMemoryGraph.js 93.44% <100.00%> (ø)
...rc/components/timeline/TrackVisualProgressGraph.js 96.84% <100.00%> (ø)
src/profile-logic/symbol-store.js 92.94% <100.00%> (ø)
src/test/fixtures/fake-symbol-store.js 100.00% <100.00%> (ø)
src/utils/bisect.js 100.00% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 75ac93c...6943c86. Read the comment docs.

Copy link
Contributor

@julienw julienw left a comment

Choose a reason for hiding this comment

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

Thanks, this is pretty good work! The split in several functional commits makes this especially easy to review.

I just have a few small suggestions, tell me what you think! After that I think this will be good to go!

it('returns index of the first number greater than x, occuring after low', function() {
expect(bisectionRight(array, 4, 8)).toBe(8);
expect(bisectionRight(array, 3, 9)).toBe(9);
expect(bisectionLeft(array, 2, -100)).toBe(2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at this test, I wonder if we shouldn't guard against specifying bad low and high values.
For example, at the start of the function:

if (low < 0) {
  // throw error
 }

The python implementation throws an error for this case: https://github.com/python/cpython/blob/3fc7080220b8dd2e1b067b3224879133d895ea80/Lib/bisect.py#L26-L27
But doesn't check other cases. Then maybe we should do the same, with a TypeError (this is the equivalent in JavaScript to the Python's ValueError), what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree we should handle bad low and high values, because especially in case when low is greater than the array length or both low and high are greater, we are getting absurd values, trying to refer to an element with that large index.
I believe both low and high should be in the range of the array and anything apart from that should throw an error.
Thanks for the link to the python implementation, I will check it out and add the error handling aspect to this.

Comment on lines 28 to 33
it('returns 0 if all elements are greater than x', function() {
expect(bisectionLeft(array, -5)).toBe(0);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please also add a test with a x that's bigger than all other numbers?
(same for bisectionRight).
It should return array.length in both cases.

@@ -14,6 +13,8 @@ import {
extractDomRectValue,
} from 'firefox-profiler/utils/css-geometry-tools';

import { bisectionRight } from '../../utils/bisect';
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please import using the alias firefox-profiler for consistency?

 import { bisectionRight } from 'firefox-profiler/utils/bisect';

@Riju19 Riju19 force-pushed the implement-bisect branch 2 times, most recently from 6dcbb8b to b9a63d2 Compare October 20, 2020 22:00
Export two functions using ES6 modules: bisectionLeft and bisectionRight. Add comments about the original source of this code and the description of the two functions. Add flow typing.
For the bisection functions, low and high values should lie within the range of the array. This commit adds a check to the bisection functions to throw TypeError in case the low or high values passed are negative or greater than the array length.
This commit also adds relevant testcases to bisect.test.js to check these conditions.
Copy link
Contributor

@julienw julienw left a comment

Choose a reason for hiding this comment

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

Thanks, this looks very good!

@julienw julienw merged commit 01ecd20 into firefox-devtools:main Oct 21, 2020
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.

2 participants