Pure functions as a replacement of constant mappings?
I need to create a lookup table [uint -> uint] that is constant.
Constant mappings
or switch-case
are not available in solidity. Are pure
functions full of if-else
the only option?
Any better suggestion?
Update Ideally, I would like an option that minimizes gas utilization
solidity mapping pure
add a comment |
I need to create a lookup table [uint -> uint] that is constant.
Constant mappings
or switch-case
are not available in solidity. Are pure
functions full of if-else
the only option?
Any better suggestion?
Update Ideally, I would like an option that minimizes gas utilization
solidity mapping pure
It seems from discussion below that this question is about gas optimization.
– smarx
Jan 2 at 23:48
add a comment |
I need to create a lookup table [uint -> uint] that is constant.
Constant mappings
or switch-case
are not available in solidity. Are pure
functions full of if-else
the only option?
Any better suggestion?
Update Ideally, I would like an option that minimizes gas utilization
solidity mapping pure
I need to create a lookup table [uint -> uint] that is constant.
Constant mappings
or switch-case
are not available in solidity. Are pure
functions full of if-else
the only option?
Any better suggestion?
Update Ideally, I would like an option that minimizes gas utilization
solidity mapping pure
solidity mapping pure
edited 2 days ago
asked Jan 2 at 21:03
purpletentacle
1164
1164
It seems from discussion below that this question is about gas optimization.
– smarx
Jan 2 at 23:48
add a comment |
It seems from discussion below that this question is about gas optimization.
– smarx
Jan 2 at 23:48
It seems from discussion below that this question is about gas optimization.
– smarx
Jan 2 at 23:48
It seems from discussion below that this question is about gas optimization.
– smarx
Jan 2 at 23:48
add a comment |
2 Answers
2
active
oldest
votes
I don't see a dilemma.
You can have a mapping:
mapping(uint => uint) public myMap;
You can populate some locations in the constructor or elsewhere:
constructor() public {
myMap[1] = 101;
myMap[2] = 201;
}
That gives you a simple view
function (myMap(uint) public view returns(uint)
) that returns the number stored at an index.
You can also make view
functions full of if/else
if that's the best approach for your use-case.
Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.
Hope it helps.
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
Jan 2 at 23:14
add a comment |
The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.
If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "642"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f64925%2fpure-functions-as-a-replacement-of-constant-mappings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't see a dilemma.
You can have a mapping:
mapping(uint => uint) public myMap;
You can populate some locations in the constructor or elsewhere:
constructor() public {
myMap[1] = 101;
myMap[2] = 201;
}
That gives you a simple view
function (myMap(uint) public view returns(uint)
) that returns the number stored at an index.
You can also make view
functions full of if/else
if that's the best approach for your use-case.
Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.
Hope it helps.
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
Jan 2 at 23:14
add a comment |
I don't see a dilemma.
You can have a mapping:
mapping(uint => uint) public myMap;
You can populate some locations in the constructor or elsewhere:
constructor() public {
myMap[1] = 101;
myMap[2] = 201;
}
That gives you a simple view
function (myMap(uint) public view returns(uint)
) that returns the number stored at an index.
You can also make view
functions full of if/else
if that's the best approach for your use-case.
Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.
Hope it helps.
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
Jan 2 at 23:14
add a comment |
I don't see a dilemma.
You can have a mapping:
mapping(uint => uint) public myMap;
You can populate some locations in the constructor or elsewhere:
constructor() public {
myMap[1] = 101;
myMap[2] = 201;
}
That gives you a simple view
function (myMap(uint) public view returns(uint)
) that returns the number stored at an index.
You can also make view
functions full of if/else
if that's the best approach for your use-case.
Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.
Hope it helps.
I don't see a dilemma.
You can have a mapping:
mapping(uint => uint) public myMap;
You can populate some locations in the constructor or elsewhere:
constructor() public {
myMap[1] = 101;
myMap[2] = 201;
}
That gives you a simple view
function (myMap(uint) public view returns(uint)
) that returns the number stored at an index.
You can also make view
functions full of if/else
if that's the best approach for your use-case.
Elaborate on the question with some hints about what you want to accomplish and possibly more specific guidance will be possible.
Hope it helps.
answered Jan 2 at 21:12
Rob Hitchens B9lab
26k54379
26k54379
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
Jan 2 at 23:14
add a comment |
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
Jan 2 at 23:14
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
Jan 2 at 23:14
Well, clearly I want to minimize gas. What do you think would be more efficient?
– purpletentacle
Jan 2 at 23:14
add a comment |
The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.
If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.
add a comment |
The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.
If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.
add a comment |
The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.
If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.
The else-if function is by far less efficient that mapping in case of sparse data if there are not special criteria for searching.
If, on the contrary, you fill all the holes in the range of interest, a simple array is the best solution.
answered Jan 3 at 0:29
Rick Park
825111
825111
add a comment |
add a comment |
Thanks for contributing an answer to Ethereum Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f64925%2fpure-functions-as-a-replacement-of-constant-mappings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It seems from discussion below that this question is about gas optimization.
– smarx
Jan 2 at 23:48