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

Replace node assert with normal error handling #1485

Merged
merged 45 commits into from
Aug 14, 2022
Merged

Replace node assert with normal error handling #1485

merged 45 commits into from
Aug 14, 2022

Conversation

birkskyum
Copy link
Member

@birkskyum birkskyum commented Aug 13, 2022

Hint: chrome extension "Refined Github" allow you to hide/unhide resolved comments.

Instead of having the node assert, which are only in the debug build, this PR is either removing the statement if it's unnecessary, or replacing it with throwing a normal error. It would potentially move us quite far towards supporting ecmascript modules.

Instead of having a big 5kb node assert library bundled to the browser with browserify in debug mode, and transpiling everything out of the codebase for prodcution builds, I was thinking about what we needed. Turns out all our assert statements are very simple boolean checks, so we can readily replace them with if statements.

This would mean that it's not server specific code anymore, so browserify is out. unassert-rollup-plugin is uncessesary too. These checks now stay in the production code, adding only ~1kB, and they improve our error handling quite a lot.

This is obviously adding some lines of code to production, but the benchmarks shown here (left is 2.3, middle is main, right is this branch) says that we don't loose performance because of it.

Screenshot 2022-08-13 at 20 30 56

@github-actions
Copy link
Contributor

github-actions bot commented Aug 13, 2022

Bundle size report:

Size Change: +1.47 kB
Total Size Before: 203 kB
Total Size After: 204 kB

Output file Before After Change
maplibre-gl.js 193 kB 195 kB +1.47 kB
maplibre-gl.css 9.65 kB 9.65 kB 0 B
ℹ️ View Details No major changes

@birkskyum birkskyum marked this pull request as ready for review August 13, 2022 17:08
@birkskyum birkskyum changed the title WIP: Change asserts Replace node assert with small utility function Aug 13, 2022
@birkskyum birkskyum changed the title Replace node assert with small utility function Replace node assert etc. with small utility function Aug 13, 2022
@birkskyum birkskyum requested review from HarelM and wipfli August 13, 2022 17:09
@birkskyum birkskyum changed the title Replace node assert etc. with small utility function Replace node assert etc. with small utility function and keep them in prod. Aug 13, 2022
@birkskyum birkskyum changed the title Replace node assert etc. with small utility function and keep them in prod. Replace node assert etc. with small utility function and keep them Aug 13, 2022
@HarelM
Copy link
Collaborator

HarelM commented Aug 13, 2022

The only main "issue" I see work this approach is that there is a functionally change in this commit: before this code simply didn't exist in production so if there was an assert that might be triggered but didn't cause any actual error the user wouldn't know as opposed to now where there might be "throws" that cause actual problems.
Not sure how to address this though besides maybe a global variable that indicate of this is a debug build... Maybe...

@birkskyum
Copy link
Member Author

birkskyum commented Aug 13, 2022

All the debug pages seem to just work, so for the most part it is likely a trivial update. But because it technically is more strict, to comply with semver we could make it a 3.0.0-pre.1 and get some feedback.

Currently I distinguish if it's a debug build by looking at the filename, checking this expect(minBundle.includes('canary debug run')).toBeFalsy(); or peeking if the code has been through terser, but there could be an easier way similar to how this map.version is exposed. But is it more important for this release than the prior somehow - maybe I'm overlooking something.

package.json Outdated Show resolved Hide resolved
@HarelM
Copy link
Collaborator

HarelM commented Aug 14, 2022

I would like to keep functionality as much as possible and not create a breaking change version just for this.
Is there's a way to declare a global variable somehow using rollup or something similar so that this will not run in production but only in development it would be nice...
We could also look at the usage themselves and see which are actually needed and which will give an error anyway and reduce the assertion usage... IDK...

src/gl/index_buffer.ts Outdated Show resolved Hide resolved
src/gl/vertex_buffer.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
src/render/image_manager.ts Outdated Show resolved Hide resolved
src/render/image_manager.ts Outdated Show resolved Hide resolved
@birkskyum
Copy link
Member Author

birkskyum commented Aug 14, 2022

@HarelM , phew, I think that was all of them and the naiveAssert is removed as well. To me it now points more in the direction of a 2.3.1-pre.1 , because it's not intended to be breaking, api-changing or adding any features.

@birkskyum birkskyum changed the title Replace node assert etc. with small utility function and keep them Remove node assert in favour or normal error handling Aug 14, 2022
@birkskyum birkskyum changed the title Remove node assert in favour or normal error handling Replace node assert with normal error handling Aug 14, 2022
@wipfli
Copy link
Contributor

wipfli commented Aug 14, 2022

I am really impressed by the number of comments. You already are at like 50 percent of #209...

@HarelM
Copy link
Collaborator

HarelM commented Aug 14, 2022

:-) I'll make a final review later on tonight. This will need a pre-release to allow other to test it...

src/render/image_manager.ts Outdated Show resolved Hide resolved
src/style/properties.ts Outdated Show resolved Hide resolved
src/util/struct_array.ts Outdated Show resolved Hide resolved
@HarelM
Copy link
Collaborator

HarelM commented Aug 14, 2022

I've added a few last comment.

@birkskyum
Copy link
Member Author

I've added a few last comment.

Thanks! These adjustment are in place now.

Copy link
Collaborator

@HarelM HarelM left a comment

Choose a reason for hiding this comment

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

Great work!

@HarelM HarelM merged commit 9d54963 into main Aug 14, 2022
@HarelM HarelM deleted the change-asserts branch August 14, 2022 19:00
@birkskyum
Copy link
Member Author

For good measure, the benchmark didn't show any regression over the past 40 commits. Ignore the first column 2.3.0 (bug in benchmark).

Screenshot 2022-08-14 at 21 01 42

@wipfli
Copy link
Contributor

wipfli commented Aug 14, 2022

Cool, thanks for removing these dependencies @birkskyum

@birkskyum birkskyum mentioned this pull request Aug 14, 2022
@birkskyum
Copy link
Member Author

Thank you, but I was merely following @HarelM 's guidance throughout :) Hope this will give us some interesting side benefits.

@birkskyum
Copy link
Member Author

Thanks, but it was merely following @HarelM 's guidance throughout (thank!) :) Hope this will give us some interesting side benefit like the option to use an unbundled dev workflow should we want to.

@wipfli
Copy link
Contributor

wipfli commented Aug 19, 2022

Needed for maplibre/maplibre-gl-js-docs#230 I think.

This was referenced Aug 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.

3 participants