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

Optimize code size for draw-batch.ts and batcher-2d.ts, remove needless setter/getter. #18238

Open
wants to merge 2 commits into
base: v3.8.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cocos/2d/renderer/batcher-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ export class Batcher2D implements IBatcher {
// by legacyCC.director.root.batcher2D._releaseDescriptorSetCache
/**
* @engineInternal
* @mangle
*/
public _releaseDescriptorSetCache (textureHash: number | Texture | null, sampler: Sampler | null = null): void {
if (JSB) {
Expand Down Expand Up @@ -962,6 +963,7 @@ export class Batcher2D implements IBatcher {
}
}

/** @mangle */
class LocalDescriptorSet {
private _descriptorSet: DescriptorSet | null = null;
private _transform: Node | null = null;
Expand All @@ -971,7 +973,11 @@ class LocalDescriptorSet {
private _transformUpdate = true;
private declare _localData: Float32Array | null;

public get descriptorSet (): DescriptorSet | null {
// NOTE: Internal modules should avoid using getter/setter accessors since we're using babel to convert TS to JS
// and terser minifier could not handle the getter/setter generated JS code correctly.
// See the issue: https://github.com/terser/terser/issues/322
// Change get descriptorSet() to getDescriptorSet() in v3.8.6.
public getDescriptorSet (): DescriptorSet | null {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why have to change 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 because we're using babel to compile ts to js.
babel has its own getter/setter accessor definition which could not be understood by terser minifier.

See this issue: terser/terser#322

Since LocalDescriptorSet is an internal module, change the get descriptSet to function getDescriptorSet is fine for us.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, it is better to add a description.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added comments.

return this._descriptorSet;
}

Expand Down Expand Up @@ -1063,6 +1069,7 @@ class LocalDescriptorSet {
}
}

/** @mangle */
class DescriptorSetCache {
private _descriptorSetCache = new Map<number, DescriptorSet>();
private _dsCacheHashByTexture = new Map<number, number>();
Expand All @@ -1080,13 +1087,13 @@ class DescriptorSetCache {
for (let i = 0, len = caches.length; i < len; i++) {
const cache: LocalDescriptorSet = caches[i];
if (cache.equals(batch.useLocalData, batch.textureHash, batch.samplerHash)) {
return cache.descriptorSet!;
return cache.getDescriptorSet()!;
}
}
const localDs = this._localCachePool.alloc();
localDs.initialize(batch);
this._localDescriptorSetCache.push(localDs);
return localDs.descriptorSet!;
return localDs.getDescriptorSet()!;
} else {
const hash = batch.textureHash ^ batch.samplerHash;
if (this._descriptorSetCache.has(hash)) {
Expand Down
35 changes: 7 additions & 28 deletions cocos/2d/renderer/draw-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,9 @@ import { IBatcher } from './i-batcher';
import type { Root } from '../../root';

const UI_VIS_FLAG = Layers.Enum.NONE | Layers.Enum.UI_3D;
export class DrawBatch2D {
public get inputAssembler (): InputAssembler | null {
return this._inputAssembler;
}

public set inputAssembler (ia: InputAssembler | null) {
this._inputAssembler = ia;
}

public get descriptorSet (): DescriptorSet | null {
return this._descriptorSet;
}

public set descriptorSet (ds: DescriptorSet | null) {
this._descriptorSet = ds;
}

public get visFlags (): number {
return this._visFlags;
}
public set visFlags (vis) {
this._visFlags = vis;
}

/** @mangle */
export class DrawBatch2D {
get passes (): Pass[] {
return this._passes;
}
Expand All @@ -77,9 +56,9 @@ export class DrawBatch2D {
public samplerHash = 0;
private _passes: Pass[] = [];
private _shaders: Shader[] = [];
private _visFlags: number = UI_VIS_FLAG;
private _inputAssembler: InputAssembler | null = null;
private _descriptorSet: DescriptorSet | null = null;
public visFlags: number = UI_VIS_FLAG;
public inputAssembler: InputAssembler | null = null;
public descriptorSet: DescriptorSet | null = null;
//private declare _nativeObj: any;

public destroy (ui: IBatcher): void {
Expand All @@ -88,8 +67,8 @@ export class DrawBatch2D {

public clear (): void {
// this.bufferBatch = null;
this._inputAssembler = null;
this._descriptorSet = null;
this.inputAssembler = null;
this.descriptorSet = null;
// this.camera = null;
this.texture = null;
this.sampler = null;
Expand Down
Loading