mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-07 03:06:42 +01:00
Optimize number of getSignalOffset and getSignalSizes
This commit is contained in:
@@ -162,6 +162,8 @@ function buildCode(ctx) {
|
|||||||
ctx.refs = [];
|
ctx.refs = [];
|
||||||
ctx.fileName = ctx.templates[ctx.components[i].template].fileName;
|
ctx.fileName = ctx.templates[ctx.components[i].template].fileName;
|
||||||
ctx.filePath = ctx.templates[ctx.components[i].template].filePath;
|
ctx.filePath = ctx.templates[ctx.components[i].template].filePath;
|
||||||
|
ctx.getSignalSizesCache = {};
|
||||||
|
ctx.getSignalOffsetCache = {};
|
||||||
|
|
||||||
for (let p in ctx.components[i].params) {
|
for (let p in ctx.components[i].params) {
|
||||||
if (ctx.scopes[0][p]) return ctx.throwError(`Repeated parameter at ${ctx.components[i].template}: ${p}`);
|
if (ctx.scopes[0][p]) return ctx.throwError(`Repeated parameter at ${ctx.components[i].template}: ${p}`);
|
||||||
|
|||||||
48
src/c_gen.js
48
src/c_gen.js
@@ -55,7 +55,7 @@ function instantiateRef(ctx, refId, initValue) {
|
|||||||
for (let i=0; i<initValue.length; i++) {
|
for (let i=0; i<initValue.length; i++) {
|
||||||
if (utils.isDefined(initValue[i])) {
|
if (utils.isDefined(initValue[i])) {
|
||||||
const idConstant = ctx.addConstant(initValue[i]);
|
const idConstant = ctx.addConstant(initValue[i]);
|
||||||
ctx.code += `Fr_copy(&(${v.label}[${i}]), ctx->circuit->constants +${idConstant});\n`;
|
ctx.codeHeader += `Fr_copy(&(${v.label}[${i}]), ctx->circuit->constants +${idConstant});\n`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -537,6 +537,18 @@ function genGetSubComponentSizes(ctx, cIdxRef, label) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function genGetSigalOffset(ctx, cIdxRef, label) {
|
function genGetSigalOffset(ctx, cIdxRef, label) {
|
||||||
|
|
||||||
|
let constCIdx = null;
|
||||||
|
if (cIdxRef>=0) {
|
||||||
|
const cIdx = ctx.refs[cIdxRef];
|
||||||
|
if (utils.isDefined(cIdx.value)) {
|
||||||
|
constCIdx = cIdx.value.toString()+"_"+label;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
constCIdx = "_cIdx_"+label;
|
||||||
|
}
|
||||||
|
if (constCIdx && ctx.getSignalOffsetCache[constCIdx]) return ctx.getSignalOffsetCache[constCIdx];
|
||||||
|
|
||||||
const refOffset = newRef(ctx, "INT", "_sigIdx");
|
const refOffset = newRef(ctx, "INT", "_sigIdx");
|
||||||
const offset = ctx.refs[refOffset];
|
const offset = ctx.refs[refOffset];
|
||||||
instantiateRef(ctx, refOffset);
|
instantiateRef(ctx, refOffset);
|
||||||
@@ -549,14 +561,31 @@ function genGetSigalOffset(ctx, cIdxRef, label) {
|
|||||||
} else {
|
} else {
|
||||||
s = "__cIdx";
|
s = "__cIdx";
|
||||||
}
|
}
|
||||||
ctx.code += `${offset.label} = ctx->getSignalOffset(${s}, 0x${h}LL /* ${label} */);\n`;
|
if (constCIdx) {
|
||||||
|
ctx.codeHeader += `${offset.label} = ctx->getSignalOffset(${s}, 0x${h}LL /* ${label} */);\n`;
|
||||||
|
ctx.getSignalOffsetCache[constCIdx] = refOffset;
|
||||||
|
} else {
|
||||||
|
ctx.code += `${offset.label} = ctx->getSignalOffset(${s}, 0x${h}LL /* ${label} */);\n`;
|
||||||
|
}
|
||||||
return refOffset;
|
return refOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
function genGetSignalSizes(ctx, cIdxRef, label) {
|
function genGetSignalSizes(ctx, cIdxRef, label) {
|
||||||
const sizesRef = newRef(ctx, "SIZES", "_sigSizes");
|
let constCIdx = null;
|
||||||
const sizes = ctx.refs[sizesRef];
|
if (cIdxRef>=0) {
|
||||||
instantiateRef(ctx, sizesRef);
|
const cIdx = ctx.refs[cIdxRef];
|
||||||
|
if (utils.isDefined(cIdx.value)) {
|
||||||
|
constCIdx = cIdx.value.toString()+"_"+label;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
constCIdx = "_cIdx_"+label;
|
||||||
|
}
|
||||||
|
if (constCIdx && ctx.getSignalSizesCache[constCIdx]) return ctx.getSignalSizesCache[constCIdx];
|
||||||
|
|
||||||
|
|
||||||
|
const refSizes = newRef(ctx, "SIZES", "_sigSizes");
|
||||||
|
const sizes = ctx.refs[refSizes];
|
||||||
|
instantiateRef(ctx, refSizes);
|
||||||
|
|
||||||
let s;
|
let s;
|
||||||
if (cIdxRef>=0) {
|
if (cIdxRef>=0) {
|
||||||
@@ -566,8 +595,13 @@ function genGetSignalSizes(ctx, cIdxRef, label) {
|
|||||||
s = "__cIdx";
|
s = "__cIdx";
|
||||||
}
|
}
|
||||||
const h = utils.fnvHash(label);
|
const h = utils.fnvHash(label);
|
||||||
ctx.code += `${sizes.label} = ctx->getSignalSizes(${s}, 0x${h}LL /* ${label} */);\n`;
|
if (constCIdx) {
|
||||||
return sizesRef;
|
ctx.codeHeader += `${sizes.label} = ctx->getSignalSizes(${s}, 0x${h}LL /* ${label} */);\n`;
|
||||||
|
ctx.getSignalSizesCache[constCIdx] = refSizes;
|
||||||
|
} else {
|
||||||
|
ctx.code += `${sizes.label} = ctx->getSignalSizes(${s}, 0x${h}LL /* ${label} */);\n`;
|
||||||
|
}
|
||||||
|
return refSizes;
|
||||||
}
|
}
|
||||||
|
|
||||||
function genSetSignal(ctx, cIdxRef, sIdxRef, valueRef) {
|
function genSetSignal(ctx, cIdxRef, sIdxRef, valueRef) {
|
||||||
|
|||||||
Reference in New Issue
Block a user