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

[v3.8.5] Support inline enum and mangle private variables to reduce engine size. #17703

Merged
merged 60 commits into from
Oct 15, 2024

Conversation

dumganhar
Copy link
Contributor

@dumganhar dumganhar commented Oct 12, 2024

Re: #15287
#17715

After merging this PR,package size could reduce nearly 180KB.

Inline enum

// Original code

export enum TileFlag {
    HORIZONTAL = 0x80000000,
    VERTICAL = 0x40000000,
    DIAGONAL = 0x20000000,
    FLIPPED_ALL = (0x80000000 | 0x40000000 | 0x20000000 | 0x10000000) >>> 0,
    FLIPPED_MASK = (~(0x80000000 | 0x40000000 | 0x20000000 | 0x10000000)) >>> 0
}

console.log(TileFlag.HORIZONTAL);
console.log(TileFlag.VERTICAL);
console.log(TileFlag.DIAGONAL);
console.log(TileFlag.FLIPPED_ALL);
console.log(TileFlag.FLIPPED_MASK);

// Inlined

var TileFlag = exports("TileFlag", {
    "HORIZONTAL": 2147483648,
    "VERTICAL": 1073741824,
    "DIAGONAL": 536870912,
    "FLIPPED_ALL": 4026531840,
    "FLIPPED_MASK": 268435455
  });
  console.log(2147483648);
  console.log(1073741824);
  console.log(536870912);
  console.log(4026531840);
  console.log(268435455);

Mangle variables using terser configuration

The variables with name ends with $ will be mangled to shorter name.

For example, _myVariable$ may be mangled to ae.

Changelog


Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

@dumganhar
Copy link
Contributor Author

@cocos-robot run test cases

Copy link

@dumganhar, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL Goblins,BuildTimeConstantsTest
ios FAIL FAIL
mac PASS PASS FAIL Goblins,BuildTimeConstantsTest

Copy link

@dumganhar, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
windows PASS PASS FAIL Goblins,BuildTimeConstantsTest
android PASS PASS FAIL Goblins,BuildTimeConstantsTest
wechatgame PASS FAIL FAIL

@@ -105,19 +125,19 @@ class ModelBakeSettings extends EventTarget {
* @en The event which will be triggered when the useLightProbe is changed.
* @zh useLightProbe属性修改时触发的事件
*/
public static readonly USE_LIGHT_PROBE_CHANGED = 'use_light_probe_changed';
public static readonly USE_LIGHT_PROBE_CHANGED = ModelBakeSettingsEvent.USE_LIGHT_PROBE_CHANGED;
Copy link
Contributor

Choose a reason for hiding this comment

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

Will it break compatibility?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. ModelBakeSettingsEvent.USE_LIGHT_PROBE_CHANGED is assigned to 'use_light_probe_changed'

enum ModelBakeSettingsEvent {
    /**
     * @en The event which will be triggered when the useLightProbe is changed.
     * @zh useLightProbe属性修改时触发的事件
     */
    USE_LIGHT_PROBE_CHANGED = 'use_light_probe_changed',

    /**
     * @en The event which will be triggered when the reflectionProbe is changed.
     * @zh reflectionProbe 属性修改时触发的事件
     */
    REFLECTION_PROBE_CHANGED = 'reflection_probe_changed',

    /**
     * @en The event which will be triggered when the bakeToReflectionProbe is changed.
     * @zh bakeToReflectionProbe 属性修改时触发的事件
     */
    BAKE_TO_REFLECTION_PROBE_CHANGED = 'bake_to_reflection_probe_changed',
}

Copy link
Contributor

Choose a reason for hiding this comment

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

If the value is not changed, then any benefit to use it?

Copy link
Contributor Author

@dumganhar dumganhar Oct 14, 2024

Choose a reason for hiding this comment

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

inline enum plugin only deals with enums, not the static const variables, so static const variables will be very long here, they take many bytes.
So add enum definition to make all places use the enums will make the enums inlined.
Keeping the static variables here is only for compatibilty. We should not use them (static const variables) in our engine code for the best practice of smaller code size.

const CUSTOM_PIXEL_FORMAT = 1024;
enum CustomPixelFormat {
VALUE = 1024,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the benefit to change to enum?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For inlining value defined in enums.
For example:

// old code
const CUSTOM_PIXEL_FORMAT = 1024;
enum PixelFormat {
    RGB_A_PVRTC_2BPPV1 = CUSTOM_PIXEL_FORMAT,
}

RGB_A_PVRTC_2BPPV1 will not be able to be inlined, and inline enum plugin will also complain that since it could not find where CUSTOM_PIXEL_FORMAT is defined.
Currently, inline enum rollup plugin only search all enum values, and will not search const variables so it doesn't know what CUSTOM_PIXEL_FORMAT is.

And if we define the private const value in another enum CustomPixelFormat, inline enum plugin will know that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good to know. Does the plugin plan to support const variables?

Copy link
Contributor Author

@dumganhar dumganhar Oct 14, 2024

Choose a reason for hiding this comment

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

There is no plan for that, it will make the plugin more complex. There are too few usages like this in our engine code. So it's no in high priority. Keep the plugin code simpler is more important.
BTW, it plugin code is at https://github.com/cocos/cocos-ccbuild/tree/master/modules/build-engine/src/engine-js/rollup-plugins/inline-enum

@@ -749,14 +750,15 @@ export class Mat3 extends ValueType {
m06 = 0, m07 = 0, m08 = 1,
) {
super();
const self = this;
Copy link
Contributor

Choose a reason for hiding this comment

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

Any benefit to use self?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Answered below.

cocos/core/math/mat4.ts Outdated Show resolved Hide resolved
@@ -57,7 +57,7 @@ ccenum(DefaultAnimsEnum);
enum DefaultCacheMode {
REALTIME = 0,
}
ccenum(DefaultAnimsEnum);
ccenum(DefaultCacheMode);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why modify it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a bug. ccenum here is decorating DefaultCacheMode rather than DefaultAnimsEnum.
Normally, in our engine code, ccenum invocation is after enum definition.

@dumganhar dumganhar merged commit f4ccf3a into cocos:v3.8.5 Oct 15, 2024
26 checks passed
cocos-robot pushed a commit to cocos-robot/engine that referenced this pull request Oct 15, 2024
dumganhar pushed a commit that referenced this pull request Oct 15, 2024
W-MTZing pushed a commit to W-MTZing/engine that referenced this pull request Nov 15, 2024
dumganhar added a commit to dumganhar/cocos-engine that referenced this pull request Dec 3, 2024
It's a bug starts from v3.8.5 branch: cocos#17703
dumganhar added a commit that referenced this pull request Dec 3, 2024
It's a bug starts from v3.8.5 branch: #17703
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