Skip to content

Commit

Permalink
Merge pull request #252 from calcit-lang/dup-convert
Browse files Browse the repository at this point in the history
fix redundant conversion from slice to map/list
  • Loading branch information
NoEgAm authored Aug 20, 2024
2 parents 26fcc68 + dcc5231 commit 0ea41fd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit"
version = "0.9.3"
version = "0.9.4"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@calcit/procs",
"version": "0.9.3",
"version": "0.9.4",
"main": "./lib/calcit.procs.mjs",
"devDependencies": {
"@types/node": "^22.1.0",
Expand Down
9 changes: 8 additions & 1 deletion ts-src/js-list.mts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export class CalcitSliceList {
start: number;
end: number;
cachedHash: Hash;
/** reference to converted list */
cachedTreeListRef: CalcitList;
constructor(value: Array<CalcitValue>) {
if (value == null) {
value = []; // dirty, better handled from outside
Expand All @@ -134,7 +136,12 @@ export class CalcitSliceList {
this.end = value.length;
}
turnListMode(): CalcitList {
return new CalcitList(initTernaryTreeListFromRange(this.value, this.start, this.end));
if (this.cachedTreeListRef != null) {
return this.cachedTreeListRef;
}
let ret = new CalcitList(initTernaryTreeListFromRange(this.value, this.start, this.end));
this.cachedTreeListRef = ret;
return ret;
}
len() {
return this.end - this.start;
Expand Down
10 changes: 9 additions & 1 deletion ts-src/js-map.mts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ export class CalcitSliceMap {
cachedHash: Hash;
/** in arrayMode, only flatten values, instead of tree structure */
chunk: CalcitValue[];
/** reference to generated HashMap in tree structure */
cachedTreeMapRef?: CalcitMap;
constructor(value: CalcitValue[]) {
if (value == null) {
this.chunk = [];
Expand All @@ -203,14 +205,20 @@ export class CalcitSliceMap {
throw new Error("unknown data for map");
}
}
/** convert to tree map when needed, also cached in case converted over and over again */
turnMap(): CalcitMap {
if (this.cachedTreeMapRef != null) {
return this.cachedTreeMapRef;
}
var dict: Array<[CalcitValue, CalcitValue]> = [];
let halfLength = this.chunk.length >> 1;
for (let idx = 0; idx < halfLength; idx++) {
dict.push([this.chunk[idx << 1], this.chunk[(idx << 1) + 1]]);
}
let value = initTernaryTreeMapFromArray(dict);
return new CalcitMap(value);
let ret = new CalcitMap(value);
this.cachedTreeMapRef = ret;
return ret;
}
len() {
return this.chunk.length >> 1;
Expand Down

0 comments on commit 0ea41fd

Please sign in to comment.