-
-
Notifications
You must be signed in to change notification settings - Fork 783
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
Use const enum more often to improve readability and reduce generated script size as byproduct #3879
Conversation
I found enum in type typescript and javascript a total miss in terms of ease of use and ergonomics in general. |
First of all we don't need change public API. Just the internal ones and when exposed, translate back to previous strings as needed. Second, yes totally agree enum is very bad and hard to use. /** Parameters for a vertex */ const enum VertexParameter { /** Each vertex position data has 4 numbers: xyzw, */ PositionElements = 4, compiled JS looks like for (var j = 0; j < 4 /* VertexParameter.PositionElements */; j++) { so IMO it is the best for both worlds. Agree after gzip, the changes look very trivial and negligible ... I am thinking everything adds up though, never know when it will suddenly reach a tipping point... |
I don't object in general as long as we are not changing public APIs. |
By applying this change, the final script size is reduced by 644 bytes. "isolatedModules": true, This flag is intended for "...other transpilers only operate on a single file at a time". see more: |
I think we tried to yse swc to improve build time at some point in time, but it was too buggy. |
* ut1 * ut2 * worker * ut2 * ut4
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3879 +/- ##
========================================
Coverage 86.74% 86.74%
========================================
Files 241 242 +1
Lines 32355 32479 +124
Branches 1978 1992 +14
========================================
+ Hits 28065 28174 +109
- Misses 3360 3368 +8
- Partials 930 937 +7 ☔ View full report in Codecov by Sentry. |
Can you check how this change affects the generated d.ts file? |
|
I don't disagree, but the d.ts generation and the typedoc are not easy to please when it comes to internal and public stuff... If the main driving force is dev experience then I'm all for it. Globe PRs: |
Thanks. Separately I will probably investigate @internal. |
please merge at your conveniences. thanks |
Launch Checklist
I noticed that MapLibre in general prefers plain strings for message/status/state. This can result in repetitive strings everywhere and eventually the generated JS is more bloated than necessary.
Readibility is certainly a concern, but we can solve both problems with const enum, which is descriptive in TS and at compile time replaced with abbreviations in favor of script size.
In fact, plain strings are less friendly for readability and code searching -- for example the common word 'loaded' is being used by both tile state and RTLPluginState. If one were to search 'loaded', they both show up. This can be addressed by const enum as well
This draft PR is a staring point --- I only defined the const enum as an illustration. Will finish when everyone is ok with this direction.
Rough calculation: current MessageType has 21 keys, average length is 13. These keys are being repeated 46 times in the code base, which means 598 bytes.
With const enum of 2 or 3 chars, we can reduce script size by 500 bytes with just MessageType.
If this practice is widely adopted throughout the code base, noticeable size reduction can be archived. Thanks.
CHANGELOG.md
under the## main
section.