I'm confused about whether to use memory or storage params when I am passing large state variables to functions in external libraries. My library functions are "view" only, and declared public, but are meant to be called only by contracts which import the library.

so if I have something like:

 imports "./FooFunctions.sol"
 contract Foo {
     mapping(string=>string[]) private _dictionary;
     string[] private _keyList;

     function getDocument() public view returns(string) {
        return FooFunctions.createDocument(_dictionary, _keyList);

and I have a library that does something with the those state variables;

 library FooFunctions {
      function createDocument(mapping(string=>string[] storage dictionary,
                              string[] storage keyList) 
                              public pure returns(string memory) {
             string memory value; 
             [make a document];
              return value;      

What is the difference, if any, if I pass those variables to the library function as storage or memory (or calldata?) either in terms of contract size (which is a concern) , deployment cost, or gas consumption when I call the function (less of a concern since I don't anticipate calling this function as part of a transaction)?

I am guessing that size should somehow be smaller if I use storage since there's no need to copy the storage data to memory first, but I'm not sure if that's true, or if there is some other issue I'm ignoring.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.