Skip to content

Commit

Permalink
reduce markHashAsInvalid calls
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed Aug 17, 2024
1 parent 84b43d2 commit 37fb867
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 25 deletions.
12 changes: 9 additions & 3 deletions packages/onchain/src/IR/IRNodes/IRApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Cloneable } from "@harmoniclabs/cbor/dist/utils/Cloneable";
import { IRParentTerm, isIRParentTerm } from "../utils/isIRParentTerm";
import { _modifyChildFromTo } from "../toUPLC/_internal/_modifyChildFromTo";
import { BaseIRMetadata } from "./BaseIRMetadata";
import { hashIrData, IRHash, isIRHash } from "../IRHash";
import { equalIrHash, hashIrData, IRHash, isIRHash } from "../IRHash";

export interface IRAppMeta extends BaseIRMetadata {
__src__?: string | undefined
Expand Down Expand Up @@ -109,7 +109,10 @@ export class IRApp
set: ( newFn: any ) => {
if( !isIRTerm( newFn ) ) return;

this.markHashAsInvalid();
if(!equalIrHash( fn.hash, newFn.hash )) this.markHashAsInvalid();

// keep the parent reference in the old child, useful for compilation
// fn.parent = undefined;
fn = newFn;
fn.parent = this;
},
Expand All @@ -123,7 +126,10 @@ export class IRApp
set: ( newArg: any ) => {
if( !isIRTerm( newArg ) ) return newArg;

this.markHashAsInvalid();
if(!equalIrHash( arg.hash, newArg.hash )) this.markHashAsInvalid();

// keep the parent reference in the old child, useful for compilation
// arg.parent = undefined;
arg = newArg;
arg.parent = this;
},
Expand Down
19 changes: 10 additions & 9 deletions packages/onchain/src/IR/IRNodes/IRCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { mapArrayLike } from "./utils/mapArrayLike";
import { isIRTerm } from "../utils";
import { makeArrayLikeProxy } from "./utils/makeArrayLikeProxy";
import { MutArrayLike } from "../utils/MutArrayLike";
import { hashIrData, IRHash, isIRHash } from "../IRHash";
import { equalIrHash, hashIrData, IRHash, isIRHash } from "../IRHash";

export interface IRCaseMeta extends BaseIRMetadata {}

Expand Down Expand Up @@ -55,10 +55,11 @@ export class IRCase
set: ( next: any ) => {
if( isIRTerm( next ) )
{
next.parent = self;
self.markHashAsInvalid();
constrTerm.parent = undefined;
if(!equalIrHash( constrTerm.hash, next.hash )) self.markHashAsInvalid();
// keep the parent reference in the old child, useful for compilation
// constrTerm.parent = undefined;
constrTerm = next;
constrTerm.parent = self;
}
return next;
},
Expand All @@ -69,22 +70,22 @@ export class IRCase

Object.defineProperty(
this, "continuations", {
value: makeArrayLikeProxy(
value: makeArrayLikeProxy<IRTerm>(
continuations,
isIRTerm,
// initModifyElem
// function called once for each element in the array
// only at element definition
newElem => {
// newElem = newElem.clone();
newElem.parent = self;
// self.markHashAsInvalid()
return newElem;
},
// modifyElem
(newElem, oldElem) => {
oldElem.parent = undefined;
// keep the parent reference in the old child, useful for compilation
// oldElem.parent = undefined;
newElem.parent = self;
self.markHashAsInvalid()
if(!equalIrHash( oldElem.hash, newElem.hash )) self.markHashAsInvalid();
return newElem;
}
),
Expand Down
13 changes: 8 additions & 5 deletions packages/onchain/src/IR/IRNodes/IRConstr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { mapArrayLike } from "./utils/mapArrayLike";
import { isIRTerm } from "../utils";
import { makeArrayLikeProxy } from "./utils/makeArrayLikeProxy";
import { MutArrayLike } from "../utils/MutArrayLike";
import { hashIrData, IRHash, isIRHash } from "../IRHash";
import { equalIrHash, hashIrData, IRHash, isIRHash } from "../IRHash";

export interface IRConstrMeta extends BaseIRMetadata {}

Expand Down Expand Up @@ -60,7 +60,7 @@ export class IRConstr

Object.defineProperty(
this, "fields", {
value: makeArrayLikeProxy(
value: makeArrayLikeProxy<IRTerm>(
fields,
isIRTerm,
// initModifyElem
Expand All @@ -71,10 +71,13 @@ export class IRConstr
return newElem;
},
// modifyElem
newElem => {
newElem = newElem.clone();
// called before setting the new value
// the return value is the value that will be set
(newElem, oldElem) => {
if(!equalIrHash(oldElem.hash, newElem.hash)) self.markHashAsInvalid()
// keep the parent reference in the old child, useful for compilation
// oldElem.parent = undefined;
newElem.parent = self;
self.markHashAsInvalid()
return newElem;
}
),
Expand Down
6 changes: 4 additions & 2 deletions packages/onchain/src/IR/IRNodes/IRDelayed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isIRTerm } from "../utils/isIRTerm";
import { IRParentTerm, isIRParentTerm } from "../utils/isIRParentTerm";
import { _modifyChildFromTo } from "../toUPLC/_internal/_modifyChildFromTo";
import { BaseIRMetadata } from "./BaseIRMetadata";
import { hashIrData, IRHash, isIRHash } from "../IRHash";
import { equalIrHash, hashIrData, IRHash, isIRHash } from "../IRHash";

export interface IRDelayedMetadata extends BaseIRMetadata {}

Expand Down Expand Up @@ -92,7 +92,9 @@ export class IRDelayed
"invalid IRTerm to be delayed"
);
}
this.markHashAsInvalid();
if(!equalIrHash(_delayed.hash, newDelayed.hash)) this.markHashAsInvalid();
// keep the parent reference in the old child, useful for compilation
// _delayed.parent = undefined;
_delayed = newDelayed;
_delayed.parent = this;
},
Expand Down
6 changes: 4 additions & 2 deletions packages/onchain/src/IR/IRNodes/IRForced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { isIRTerm } from "../utils/isIRTerm";
import { IRParentTerm, isIRParentTerm } from "../utils/isIRParentTerm";
import { _modifyChildFromTo } from "../toUPLC/_internal/_modifyChildFromTo";
import { BaseIRMetadata } from "./BaseIRMetadata";
import { hashIrData, IRHash, isIRHash } from "../IRHash";
import { equalIrHash, hashIrData, IRHash, isIRHash } from "../IRHash";

export interface IRForcedMetadata extends BaseIRMetadata {}

Expand Down Expand Up @@ -92,7 +92,9 @@ export class IRForced
"invalid IRTerm to be forced"
);
}
this.markHashAsInvalid();
if( !equalIrHash(_forced.hash, newForced.hash) ) this.markHashAsInvalid();
// keep the parent reference in the old child, useful for compilation
// _forced.parent = undefined;
_forced = newForced;
_forced.parent = this;
},
Expand Down
6 changes: 4 additions & 2 deletions packages/onchain/src/IR/IRNodes/IRFunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { defineReadOnlyProperty } from "@harmoniclabs/obj-utils";
import { IRParentTerm, isIRParentTerm } from "../utils/isIRParentTerm";
import { _modifyChildFromTo } from "../toUPLC/_internal/_modifyChildFromTo";
import { BaseIRMetadata } from "./BaseIRMetadata";
import { hashIrData, IRHash, isIRHash } from "../IRHash";
import { equalIrHash, hashIrData, IRHash, isIRHash } from "../IRHash";

export interface IRFuncMetadata extends BaseIRMetadata {}

Expand Down Expand Up @@ -122,7 +122,9 @@ export class IRFunc
);
}

this.markHashAsInvalid();
if(!equalIrHash(_body.hash, newBody.hash)) this.markHashAsInvalid();
// keep the parent reference in the old child, useful for compilation
// _body.parent = undefined;
_body = newBody;
_body.parent = this;
},
Expand Down
8 changes: 7 additions & 1 deletion packages/onchain/src/IR/IRNodes/IRHoisted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,14 @@ export class IRHoisted
throw new BasePlutsError(
"only closed terms can be hoisted"
);
this.markHashAsInvalid();
if(!!equalIrHash( _hoisted.hash, newHoisted.hash )) this.markHashAsInvalid();

// dependencies need to be updated EVEN if hash is the same
// since the terms might be the same but maybe cloned
_deps = undefined;

// keep the parent reference in the old child, useful for compilation
// _hoisted.parent = undefined;
_hoisted = newHoisted;
_hoisted.parent = this
}
Expand Down
9 changes: 8 additions & 1 deletion packages/onchain/src/IR/IRNodes/IRLetted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,15 @@ export class IRLetted
if( !isIRTerm( newVal ) )
throw new BasePlutsError("letted term was not IRTerm");

this.markHashAsInvalid();
if(!equalIrHash(_value.hash, newVal.hash)) this.markHashAsInvalid();

// remove deps even if the value is the same
// newValue might be a clone of the current value
// and so have different (new) objects
_deps = undefined;

// keep the parent reference in the old child, useful for compilation
// _value.parent = undefined;
_value = newVal;
_value.parent = this
},
Expand Down

0 comments on commit 37fb867

Please sign in to comment.