Skip to content

Commit

Permalink
reduce allocation in making records; bump 0.3.28
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Jun 7, 2021
1 parent 45ce2ca commit 1d04475
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 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_runner"
version = "0.3.27"
version = "0.3.28"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
1 change: 1 addition & 0 deletions lib/calcit-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ export let getStringName = (x: CrDataValue): string => {
throw new Error("Cannot get string as name");
};

/** returns -1 when not found */
export function findInFields(xs: Array<string>, y: string): number {
let lower = 0;
let upper = xs.length - 1;
Expand Down
2 changes: 1 addition & 1 deletion lib/calcit.procs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export * from "./calcit-data";
export * from "./record-procs";
export * from "./custom-formatter";

export const calcit_version = "0.3.27";
export const calcit_version = "0.3.28";

let inNodeJs = typeof process !== "undefined" && process?.release?.name === "node";

Expand Down
36 changes: 25 additions & 11 deletions lib/record-procs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
cloneSet,
getStringName,
CrDataRecord,
findInFields,
} from "./calcit-data";

export let new_record = (name: CrDataValue, ...fields: Array<CrDataValue>): CrDataValue => {
Expand All @@ -39,7 +40,7 @@ export let fieldsEqual = (xs: Array<string>, ys: Array<string>): boolean => {
if (xs.length !== ys.length) {
return false;
}
for (let idx in xs) {
for (let idx = 0; idx < xs.length; idx++) {
if (xs[idx] !== ys[idx]) {
return false;
}
Expand All @@ -52,18 +53,31 @@ export let _AND__PCT__MAP_ = (proto: CrDataValue, ...xs: Array<CrDataValue>): Cr
if (xs.length % 2 !== 0) {
throw new Error("Expected even number of key/value");
}
let pairs: Array<[string, CrDataValue]> = [];
for (let i = 0; i < xs.length >> 1; i++) {
let idx = i << 1;
pairs.push([getStringName(xs[idx]), xs[idx + 1]]);
if (xs.length !== proto.fields.length * 2) {
throw new Error("fields size does not match");
}
// mutable sort for perf
pairs.sort(fieldPairOrder);
let fields = pairs.map((ys) => ys[0]);
if (!fieldsEqual(fields, proto.fields)) {
throw new Error("Fields does not match prototype");

let values = new Array(proto.fields.length);

for (let i = 0; i < proto.fields.length; i++) {
let idx = -1;
let k = proto.fields[i];
for (let j = 0; j < proto.fields.length; j++) {
if (k === getStringName(xs[j * 2])) {
idx = j;
break;
}
}

if (idx < 0) {
throw new Error("invalid field name for this record");
}
if (values[i] != null) {
throw new Error("record field already has value, probably duplicated key");
}
values[i] = xs[idx * 2 + 1];
}
let values = pairs.map((ys) => ys[1]);

return new CrDataRecord(proto.name, proto.fields, values);
} else {
throw new Error("Expected prototype to be a record");
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.3.27",
"version": "0.3.28",
"main": "./lib/calcit.procs.js",
"devDependencies": {
"@types/node": "^15.0.1",
Expand Down

0 comments on commit 1d04475

Please sign in to comment.