Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianoNaraku committed Nov 22, 2023
1 parent f0f39d3 commit 5ea0091
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 53 deletions.
2 changes: 1 addition & 1 deletion sandbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let data:any, node:any, view:any, component:any;
// - DONE: Constructors.setPtr, setExternalPtr
// - missing: in reducer

// search for this comment: "abababababab"




Expand Down
110 changes: 109 additions & 1 deletion src/debugtools/debug.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {GObject, RuntimeAccessible, SetRootFieldAction} from "../joiner";
import type {DClass, DModel, GObject, LClass, LModel} from "../joiner";
import {LPointerTargetable, RuntimeAccessible, SetRootFieldAction} from "../joiner";
let windoww = window as any;

@RuntimeAccessible
Expand Down Expand Up @@ -51,7 +52,114 @@ export class Debug {
}
console.log(windoww.GraphElementComponent.all);
}

// 16s 50 classi vuote
static benchmarkCreateElement(times: number = 100, disableConsole: boolean = true): BenchmarkOptions{
let state = windoww.s();

let checkDelayMax = 300;
let checkDelayMin = 50;
let diff = checkDelayMax - checkDelayMin;
let callbacks: any = {checkCompletionFunction, checkDelayMin, additionalDelayMax: diff, times, disableConsole};
callbacks.startTime = new Date();
if(disableConsole) windoww.Log.disableConsole();
let lmodel = (LPointerTargetable.wrap(state.idlookup[ state.models[0] ]) as LModel);
for (let i = 0; i < times; i++){
lmodel.addChild("Class");
}
function checkCompletionFunction() { return $(".DClass").length; }

Debug.timeMeasurer(callbacks);
return callbacks;
}
// 4s 10 istanze con 5 attributi
static benchmarkCreateInstance(metaclassName: string="Concept 1", times: number = 100, disableConsole: boolean = true): BenchmarkOptions{
let checkDelayMax = 300;
let checkDelayMin = 50;
let diff = checkDelayMax - checkDelayMin;
let callbacks: any = {checkCompletionFunction, checkDelayMin, additionalDelayMax: diff, times, disableConsole};
callbacks.startTime = new Date();
if(disableConsole) windoww.Log.disableConsole();

let state = windoww.s();
let lmodel = (LPointerTargetable.wrap(state.idlookup[ state.models[1] ]) as LModel);
let lclass = LPointerTargetable.wrap(state.classs.map((cid: string)=>state.idlookup[cid]).filter((c:DClass) => c.name === metaclassName)[0]) as LClass;
for (let i = 0; i < times; i++) {
lmodel.addObject(lclass?.id);
}
function checkCompletionFunction() { return $("[data-modelname=\"DObject\"]").length; }

Debug.timeMeasurer(callbacks);
return callbacks;
}


static timeMeasurer(callbacks0:Partial<BenchmarkOptions> | undefined): BenchmarkOptions {
let callbacks: BenchmarkOptions = callbacks0 as any;
if (!callbacks) callbacks = {} as any;
// if (!callbacks.startTime) callbacks.startTime = new Date();
if (!callbacks.maxStuckTime) callbacks.maxStuckTime = 10000;
if (!callbacks.checkDelayMin) callbacks.checkDelayMin = 300;
if (!callbacks.additionalDelayMax) callbacks.additionalDelayMax = 2000;
if (!callbacks.onStuck) callbacks.onStuck = (time:number, start: Date, end:Date, $complete: number) => {
console.log("Benchmarked operation stuck at same completion% for: " + callbacks.maxStuckTime/1000+" s. \n" +
"After " + time/100 + "s total time passed and " + $complete*100 + "% of the task was completed. \nBenchmark aborted."); }
if (!callbacks.onFinish) callbacks.onFinish = (time:number, start: Date, end:Date) => {
console.log("Benchmarked operation completed after: " + time/1000 + " s."); }

windoww.Log.exDev(!callbacks.times, ".times is a mandatory option");
windoww.Log.exDev(!callbacks.checkCompletionFunction, ".checkCompletionFunction is a mandatory option");
windoww.Log.exDev(!callbacks.checkCompletionFunction, ".startTime is a mandatory option. set it **before** doing the main task, then call the benchmark.");
(callbacks as any).completionHistory = [];

if(callbacks.disableConsole) windoww.Log.disableConsole();
Debug.timeMeasurer_inner(callbacks);
return callbacks;
}

private static timeMeasurer_inner(callbacks:BenchmarkOptions): void {
let completedTimes = callbacks.checkCompletionFunction();
(callbacks as any).completionHistory.push(completedTimes);
if (completedTimes === callbacks.times) {
callbacks.endTime = new Date();
callbacks.totTime = callbacks.endTime.getTime() - callbacks.startTime.getTime();
if(callbacks.disableConsole) windoww.Log.enableConsole();
callbacks.onFinish(callbacks.totTime, callbacks.startTime, callbacks.endTime);
callbacks.endStatus = "FINISH";
return;
}
let $complete = completedTimes / callbacks.times;
if (callbacks.completedTimes !== completedTimes) {
callbacks.stuckSince = new Date().getTime();
callbacks.completedTimes = completedTimes;
} else
if (callbacks.stuckSince > callbacks.maxStuckTime) {
callbacks.endTime = new Date();
let $complete = completedTimes / callbacks.times;
callbacks.totTime = callbacks.endTime.getTime() - callbacks.startTime.getTime();
if(callbacks.disableConsole) windoww.Log.enableConsole();
callbacks.onStuck(callbacks.totTime, callbacks.startTime, callbacks.endTime, $complete);
callbacks.endStatus = "STUCK";
return;
}
let delay = callbacks.checkDelayMin;
if ($complete !== 0) delay += callbacks.additionalDelayMax * (1-$complete);
setTimeout(()=>Debug.timeMeasurer_inner(callbacks), delay );

}
}
type BenchmarkOptions = {
onFinish:(time:number, start: Date, end:Date)=>void,
onStuck:(time:number, start: Date, end:Date, $complete: number)=>void,
disableConsole: boolean, endStatus: string,
// completionFunction returns how many steps are completed.
// times how many steps need to be completed to mark the test as finished.
checkCompletionFunction: () => number, times: number, completedTimes: number,
// those are automatically set
startTime:Date, endTime:Date, stuckSince: number, totTime: number,
// check how often completion or abortion is checked
checkDelayMin: number, additionalDelayMax: number, maxStuckTime:number
};

setTimeout(Debug.timeoutTasks, 500);
setTimeout(Debug.largeTimeoutTasks, 5000);
Expand Down
22 changes: 15 additions & 7 deletions src/graph/graphElement/graphElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import React, {Component, Dispatch, PureComponent, ReactElement, ReactNode,} fro
import {createPortal} from "react-dom";
import {connect} from "react-redux";
import './graphElement.scss';
import {EdgeStateProps, LGraphElement, store, VertexComponent} from "../../joiner";
import {
Pointers,
Selectors as Selectors_
} from "../../joiner";
import type {EdgeOwnProps} from "./sharedTypes/sharedTypes";
import {DefaultUsageDeclarations} from "./sharedTypes/sharedTypes";

import {EdgeStateProps, LGraphElement, store, VertexComponent,
BEGIN,
CreateElementAction, DClass, Debug,
DEdge, DEnumerator,
Expand Down Expand Up @@ -35,15 +41,14 @@ import {
Pointer,
RuntimeAccessible,
RuntimeAccessibleClass,
Selectors,
SetFieldAction,
SetRootFieldAction,
U,
UX,
windoww,
} from "../../joiner";
import {DefaultUsageDeclarations, EdgeOwnProps} from "./sharedTypes/sharedTypes";

const Selectors: typeof Selectors_ = windoww.Selectors;

export function makeEvalContext(view: LViewElement, state: DState, ownProps: GraphElementOwnProps, stateProps: GraphElementReduxStateProps): GObject {
let component = GraphElementComponent.map[ownProps.nodeid as Pointer<DGraphElement>];
Expand Down Expand Up @@ -132,13 +137,16 @@ export class GraphElementComponent<AllProps extends AllPropss = AllPropss, Graph
}

static mapViewStuff(state: DState, ret: GraphElementReduxStateProps, ownProps: GraphElementOwnProps) {
let dnode: DGraphElement | undefined = ownProps?.nodeid && DPointerTargetable.from(ownProps.nodeid, state) as any;
// let dnode: DGraphElement | undefined = ownProps?.nodeid && DPointerTargetable.from(ownProps.nodeid, state) as any;
if (ownProps.view) {
ret.views = [];
ret.view = LPointerTargetable.wrap(ownProps.view) as LViewElement;
ret.view = LPointerTargetable.fromD(Selectors.getViewByIDOrNameD(Pointers.from(ownProps.view), state) as DViewElement);
Log.w(!ret.view, "Requested view "+ownProps.view+" not found. Another view got assigned.", {requested: ownProps.view, props: ownProps, state: ret});
}
else {
const viewScores = Selectors.getAppliedViews(ret.data, ownProps.view || null, ownProps.parentViewId || null);
if (!ret.view) {
const viewScores = Selectors.getAppliedViews(ret.data,
(ownProps.view as LViewElement)?.id || (ownProps.view as string) || null,
ownProps.parentViewId || null);
ret.views = viewScores.map(e => LViewElement.fromD(e.element));
ret.view = ret.views[0];
(ret as any).viewScores = viewScores; // debug only
Expand Down
2 changes: 1 addition & 1 deletion src/joiner/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ export class Constructors<T extends DPointerTargetable = DPointerTargetable>{
}
}

static makeID(isUser:boolean=false): Pointer{ return new Date().getTime() + "_" + (isUser ? DUser.current : 'USER') + "_" + (DPointerTargetable.maxID++) }
static makeID(isUser:boolean=false): Pointer { return "Pointer" + new Date().getTime() + "_" + (isUser ? DUser.current : 'USER') + "_" + (DPointerTargetable.maxID++) }
private setID(id?: string, isUser:boolean = false){
this.thiss.id = id || Constructors.makeID(isUser);
}
Expand Down
4 changes: 2 additions & 2 deletions src/joiner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ export {DViewPoint, LViewPoint} from "../view/viewPoint/viewpoint";

export {Action, CreateElementAction, DeleteElementAction, SetFieldAction, SetRootFieldAction, CompositeAction, ParsedAction, LoadAction, CombineHistoryAction, RedoAction, UndoAction, TRANSACTION, BEGIN, ABORT, END} from "../redux/action/action";
export {DState, LState, ModelStore, ViewPointState, statehistory} from "../redux/store";
export {Selectors as Selectorss} from "../redux/selectors/selectors";
export var Selectors = windoww.Selectors as (GObjectt & typeof SelType);
export {Selectors} from "../redux/selectors/selectors";
// export var Selectors = windoww.Selectors as (GObjectt & typeof SelType);
export {reducer, jodelInit} from "../redux/reducer/reducer";
export {store} from "../redux/createStore";
export {Debug} from "../debugtools/debug";
Expand Down
6 changes: 3 additions & 3 deletions src/model/logicWrapper/LModelElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1863,19 +1863,19 @@ export class DClass extends DPointerTargetable { // extends DClassifier

public static new(name?: DNamedElement["name"], isInterface: DClass["interface"] = false, isAbstract: DClass["abstract"] = false, isPrimitive: DClass["isPrimitive"] = false, partial?: DClass["partial"],
partialDefaultName?: DClass["partialdefaultname"], father?: Pointer, persist: boolean = true): DClass {
if (!name) name = this.defaultname("concept ", father);
if (!name) name = this.defaultname("Concept ", father);
return new Constructors(new DClass('dwc'), father, persist, undefined).DPointerTargetable().DModelElement()
.DNamedElement(name).DClassifier().DClass(isInterface, isAbstract, isPrimitive, partial, partialDefaultName).end();
}

static new2(setter: Partial<ObjectWithoutPointers<DClass>>, father: DClass["father"], name?: DClass["name"]): DClass {
if (!name) name = this.defaultname((name || "concept "), father);
if (!name) name = this.defaultname((name || "Concept "), father);
return new Constructors(new DClass('dwc'), father, true, undefined).DPointerTargetable().DModelElement()
.DNamedElement(name).DClassifier().DClass().end((d) => { Object.assign(d, setter); });
}

static new3(a: Partial<ClassPointers>, callback: undefined | ((d: DClass, c: Constructors) => void), persist: boolean = true): DClass {
if (!a.name) a.name = this.defaultname("concept ", a.father);
if (!a.name) a.name = this.defaultname("Concept ", a.father);
return new Constructors(new DClass('dwc'), a.father, persist, undefined, a.id).DPointerTargetable().DModelElement()
.DNamedElement(a.name).DClassifier().DClass().end(callback);
}
Expand Down
Loading

0 comments on commit 5ea0091

Please sign in to comment.