Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test code in production provokes an unnecessary find operation #1701

Open
fedejinich opened this issue Jan 25, 2022 · 0 comments
Open

Test code in production provokes an unnecessary find operation #1701

fedejinich opened this issue Jan 25, 2022 · 0 comments

Comments

@fedejinich
Copy link
Contributor

fedejinich commented Jan 25, 2022

Each time MutableRepository.addStorageBytes() its called, it triggers an unnecessary Trie.find() operation by doing MutableTrie.getValueLength(). This may cause performance issues, or at least it's not the most performant option, this is because (as the comment says) we have test code in production.

    @Override
    public synchronized void addStorageBytes(RskAddress addr, DataWord key, byte[] value) {
        // This should not happen in production because contracts are created before storage cells are added to them.
        // But it happens in Repository tests, that create only storage row cells.
        if (!isExist(addr)) {
            createAccount(addr);
            setupContract(addr);
        }

        byte[] triekey = trieKeyMapper.getAccountStorageKey(addr, key);

        // Special case: if the value is an empty vector, we pass "null" which commands the trie to remove the item.
        // Note that if the call comes from addStorageRow(), this method will already have replaced 0 by null, so the
        // conversion here only applies if this is called directly. If suppose this only occurs in tests, but it can
        // also occur in precompiled contracts that store data directly using this method.
        if (value == null || value.length == 0) {
            mutableTrie.put(triekey, null);
        } else {
            mutableTrie.put(triekey, value);
        }
    }

same happens in MutableRepository.saveCode()

    @Override
    public synchronized void saveCode(RskAddress addr, byte[] code) {
        byte[] key = trieKeyMapper.getCodeKey(addr);
        mutableTrie.put(key, code);

        // THIS IS TEST CODE IN PRODUCTION
        if (code != null && code.length != 0 && !isExist(addr)) {
            createAccount(addr);
        }
    }

We should try to refactor it and move test code into tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant