-
-
Notifications
You must be signed in to change notification settings - Fork 677
feat: optimize the active init operation of zero-initialized memory of makeFieldInitializationInConstructor
#2948
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ under the licensing terms detailed in LICENSE: | |
* Fabián Heredia Montiel <[email protected]> | ||
* Jonas Minnberg <[email protected]> | ||
* Kam Chehresa <[email protected]> | ||
* Rui Jin <[email protected]> | ||
|
||
Portions of this software are derived from third-party works licensed under | ||
the following terms: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,8 @@ import { | |
isConstExpressionNaN, | ||
ensureType, | ||
createType, | ||
getConstValueInteger | ||
getConstValueInteger, | ||
isConstZero | ||
} from "./module"; | ||
|
||
import { | ||
|
@@ -10025,6 +10026,14 @@ export class Compiler extends DiagnosticEmitter { | |
|
||
// === Specialized code generation ============================================================== | ||
|
||
/** Check if possible to optimize the active initialization away if it's zero */ | ||
canOptimizeZeroInitialization(valueExpr: ExpressionRef): bool { | ||
if ((this.options.runtime != Runtime.Incremental) && (this.options.runtime != Runtime.Stub)) { | ||
return false; | ||
} | ||
return isConstZero(valueExpr); | ||
} | ||
|
||
/** Makes a constant zero of the specified type. */ | ||
makeZero(type: Type): ExpressionRef { | ||
let module = this.module; | ||
|
@@ -10372,6 +10381,7 @@ export class Compiler extends DiagnosticEmitter { | |
let parameterIndex = fieldPrototype.parameterIndex; | ||
|
||
// Defer non-parameter fields until parameter fields are initialized | ||
// Since non-parameter may depend on parameter fields | ||
if (parameterIndex < 0) { | ||
if (!nonParameterFields) nonParameterFields = new Array(); | ||
nonParameterFields.push(property); | ||
|
@@ -10407,16 +10417,25 @@ export class Compiler extends DiagnosticEmitter { | |
let initializerNode = fieldPrototype.initializerNode; | ||
assert(fieldPrototype.parameterIndex < 0); | ||
let setterInstance = assert(field.setterInstance); | ||
let expr = this.makeCallDirect(setterInstance, [ | ||
module.local_get(thisLocalIndex, sizeTypeRef), | ||
initializerNode // use initializer if present, otherwise initialize with zero | ||
? this.compileExpression(initializerNode, fieldType, Constraints.ConvImplicit) | ||
: this.makeZero(fieldType) | ||
], field.identifierNode, true); | ||
if (this.currentType != Type.void) { // in case | ||
expr = module.drop(expr); | ||
|
||
if (initializerNode) { | ||
|
||
// Explicit initializer | ||
// Check if we need to initialize this field | ||
const valueExpr: ExpressionRef = this.compileExpression(initializerNode, fieldType, Constraints.ConvImplicit); | ||
// Memory will be filled with 0 on itcms.__new | ||
// Memory grow will default to initialized with 0 as wasm spec | ||
// So, optimize the active initialization away if it's zero | ||
if (!this.canOptimizeZeroInitialization(valueExpr)) { | ||
let expr = this.makeCallDirect(setterInstance, [ | ||
module.local_get(thisLocalIndex, sizeTypeRef), | ||
valueExpr | ||
], field.identifierNode, true); | ||
if (this.currentType != Type.void) { // in case | ||
expr = module.drop(expr); | ||
} | ||
stmts.push(expr); | ||
} | ||
} | ||
stmts.push(expr); | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.