Skip to content

Initial implementation of the global buffer arena #270

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

Merged
merged 25 commits into from
Nov 28, 2019

Conversation

Robbepop
Copy link
Collaborator

@Robbepop Robbepop commented Nov 22, 2019

Implements the buffer pool mentioned in this issue: #245

The implementation of the BufferArena shall solve the problem of throttling heap memory allocations due to heavy usage in the layer between SRML contracts and ink!. Most often buffers are short-lived since they are just used for encoding and decoding intermediate values. The BufferArena acts as a globally accessible heap allocation cache.

Provides a single API:

  • fn get_buffer(&self) -> BufferRef

Where BufferRef implements all traits required for ink! and SRML contracts interaction.
When such a BufferRef goes out of scope its internal buffer is added back to the global BufferArena instance.

The implementation tries to be as efficient as possible while maintaining checks to identify malicious smart contracts that accumulate many cached buffers.

This small module will be the backbone of the future dynamic allocator and maybe also of a reimplementation of the environmental accessor.

@Robbepop Robbepop requested a review from pepyakin November 25, 2019 13:49
@Robbepop Robbepop marked this pull request as ready for review November 25, 2019 15:32
@codecov-io
Copy link

codecov-io commented Nov 25, 2019

Codecov Report

Merging #270 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #270   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           1      1           
  Lines          30     30           
=====================================
  Hits           30     30

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d3b091d...b6e937b. Read the comment docs.

/// Since the whole point behind this byte buffer arena is to cache
/// allocated heap memory the number of concurrent byte buffers that are
/// in use at the same time should be kept small.
const IN_USE_LIMIT: usize = 1000;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the limit is defined by the number of instances, but what is the byte limit per instance? I wonder if it makes more sense to define the arena limit as a number of bytes, since this would provide a better idea of the upper byte boundary.

Does the number have any reasoning behind it or is it more a rough value of thumb for now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The limit is arbitrary.
However imposing it makes sense since there is one way how you could abuse this global buffer arena and it is when you end up with many many different buffers, so when in_use is a high number.
I expect normal use cases to spawn at most 5-10 buffers so 1000 is already pretty intense.

A regulation on the total bytes in use could be introduced but it should definitely be possible to have for example one giant buffer if that's what has been needed.

@Robbepop Robbepop merged commit 19fe191 into master Nov 28, 2019
@Robbepop Robbepop deleted the robin-implement-global-buffers branch November 28, 2019 18:16
cmichi pushed a commit that referenced this pull request Dec 10, 2019
* [core] initial implementation of the global buffer arena

* [core] add license header to buffer arena

* [core] add module level docs to buffer arena

* [core] move license header where it belongs (to the top)

* [core] add docs for diagnostic fields and getters

* [core] add tests to buffer arena

* [core] apply rust fmt

* [core] remove allocated field from BufferArena

The information is redundant since it can be computed as free + in_use.

* [core] improve buffer arena tests

* [core] export buffer arena public symbols from core::env2

* [core] fix doc comment link to AsRef and AsMut

* [core] remove nightly cell-update feature

* [core] enable no_std for BufferArena and mirror thread_local interfacing

* [core] fix some obvious no_std mis-compilations

* [core] apply rustfmt

* [core] apply rustfmt #2

* [core] fix clippy warning in buffer_arena

* [core] fix typo

Co-Authored-By: Michael Müller <[email protected]>

* [core] slightly improve get_buffer impl

Co-Authored-By: Michael Müller <[email protected]>

* [core] slight improvements

* [core] rename LocalKey to GlobalBufferArena

* [core] fix no_std build
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

Successfully merging this pull request may close these issues.

4 participants