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

Added buffer files #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SharafMohamed
Copy link
Contributor

Description

Added 5 new files for buffer classes.

  • Buffer.hpp provides the structure of the buffer class, with storage for a static buffer and a vector of dynamic buffers.
  • InputBuffer.hpp + InputBuffer.cpp provides functionality for a character buffer which is split into two halves. A half can only be overwritten once its content has been consumed. If more input is needed when neither half is fully consumed, the buffer size can be doubled.
  • OutputBuffer.hpp + OutputBuffer.cpp provides functionality for a token buffer. If the buffer is not big enough to store the desired amount of token, the buffer size can be doubled.

};
}

#endif // COMPRESSOR_FRONTEND_BUFFER_HPP
Copy link
Member

Choose a reason for hiding this comment

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

Add empty newline at end of file.

// Dynamic storage performs better as c-style arrays than as vectors
type* m_active_storage;
std::vector<type*> m_dynamic_storages;
type m_static_storage[cStaticByteBuffSize];
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
type m_static_storage[cStaticByteBuffSize];
std::array<type, cStaticByteBuffSize> m_static_storage;

}
m_dynamic_storages.clear();
m_active_storage = m_static_storage;
m_curr_storage_size = cStaticByteBuffSize;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
m_curr_storage_size = cStaticByteBuffSize;
m_curr_storage_size = m_static_storage.size();

// Project Headers
#include "Constants.hpp"

/**
Copy link
Member

Choose a reason for hiding this comment

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

  • Class header comment should go above the class definition.
  • Comment lines should be less than 80 characters.
  • This description is vague. You should ideally explain why you would need a static and a dynamic buffer, grouped together in one class.

class Buffer {
public:
// Prevent copying of buffer as this will be really slow
Buffer (Buffer&&) = delete;
Copy link
Member

Choose a reason for hiding this comment

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

  • If you're deleting the move constructor, you should delete the copy constructor as well (see rule of 5).
  • Why're you deleting the move constructor?
  • The move constructor should come after the constructor.

public:

/**
* Increment buffer pos, swaps to a dynamic buffer (or doubles its size) if needed
Copy link
Member

Choose a reason for hiding this comment

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

  • Should be in imperative form.
  • Swapping to a dynamic buffer seems like an implementation detail (so it shouldn't be in the API header comment).
  • It's not really clear to a reader what pos is. Perhaps this should be called advance_to_next_token?


/**
* Resets output buffer
* @return
Copy link
Member

Choose a reason for hiding this comment

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

No return

(Token*)malloc(2 * m_curr_storage_size * sizeof(Token)));
if (m_dynamic_storages.back() == nullptr) {
SPDLOG_ERROR("Failed to allocate output buffer of size {}.", m_curr_storage_size);
/// TODO: update exception when they're properly
Copy link
Member

Choose a reason for hiding this comment

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

When they're properly what?

*/
namespace compressor_frontend {

class OutputBuffer : public Buffer<Token> {
Copy link
Member

Choose a reason for hiding this comment

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

OutputTokenBuffer?

}
m_dynamic_storages.clear();
m_active_storage = m_static_storage;
m_curr_storage_size = cStaticByteBuffSize;
Copy link
Member

Choose a reason for hiding this comment

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

It's not in this review, but I would suggest changing cStaticByteBuffSize to be something like cStaticSingleBufferSize and make it be half of its current value. Then multiply it by 2 wherever necessary.

  • Dropping "Byte" is because it's not just used for a byte buffer.
  • Dividing it by 2 means we avoid a case where a developer later changes the size to something that's not evenly divisible.

@kirkrodrigues
Copy link
Member

Can we also add some tests, even if they're basic?

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.

None yet

2 participants