Skip to content

Implement byteSize methods on lru delegates for use in GC threshold #1893

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 3 commits into from
Oct 1, 2018

Conversation

gsoltis
Copy link

@gsoltis gsoltis commented Oct 1, 2018

These methods will be used to determine if we should run LRU collection.

@@ -16,6 +16,7 @@

#import <Foundation/Foundation.h>

#import "Firestore/Source/Local/FSTLocalSerializer.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

Conventionally in Objective-C we've been using forward declarations (with @class) to keep compilation times down.

Note that this works pretty well in Objective-C but basically doesn't work for C++ so we use the IWYU rule there.

Copy link
Author

Choose a reason for hiding this comment

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

Ok, switched to a forward declaration, here and elsewhere.

auto iter = util::DirectoryIterator::Create(_directory);
for (; iter->Valid(); iter->Next()) {
off_t fileSize = util::FileSize(iter->file()).ValueOrDie();
count += fileSize;
Copy link
Contributor

Choose a reason for hiding this comment

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

Strictly speaking this isn't safe: size_t is a type that measures the sizes of objects in memory, so on 32-bit systems it will be 32 bits. off_t is the size of files on disk, and can be defined to be 64 bits even on 32 bit systems (e.g. by passing -D_FILE_OFFSET_BITS=64 to the build).

However, I'm not sure I care about supporting local caches larger than 4GB on 32 bit systems and I can't see us exposing the end user control of the cache size in terms of off_t so it seems silly to internally represent the cache size that way only to knee cap it through the control knob.

Maybe just assert we don't overflow here?

Copy link
Author

Choose a reason for hiding this comment

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

I switched the accumulator to off_t and assert that it is less than SIZE_MAX before returning.

@@ -16,6 +16,8 @@

#import "Firestore/Source/Local/FSTLevelDB.h"

#include <dirent.h>
Copy link
Contributor

Choose a reason for hiding this comment

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

These headers aren't portable.

Better to allow filesystem.h to provide off_t however it's going to get it.

Copy link
Author

Choose a reason for hiding this comment

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

Removed. I think they were from a previous iteration of this anyways, and are no longer needed.

@@ -103,4 +105,6 @@ extern const firebase::firestore::model::ListenSequenceNumber kFSTListenSequence
- (int)removeOrphanedDocumentsThroughSequenceNumber:
(firebase::firestore::model::ListenSequenceNumber)sequenceNumber;

- (size_t)onDiskSize;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this name be the same as byteSize above?

Copy link
Author

Choose a reason for hiding this comment

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

Done.

@@ -466,6 +469,14 @@ - (NSUInteger)indexOfExistingBatchID:(BatchId)batchID action:(NSString *)action
return (NSUInteger)index;
}

- (size_t)byteSizeWithSerializer:(FSTLocalSerializer *)serializer {
__block size_t count = 0;
[self.queue enumerateObjectsUsingBlock:^(FSTMutationBatch *batch, NSUInteger idx, BOOL *stop) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use a for-in loop:

size_t count = 0;
for (FSTMutationBatch* batch in self.queue) {
  count += [[[serializer encodedMutationBatch:batch] data] length];
}

Copy link
Author

Choose a reason for hiding this comment

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

Done.

static size_t FSTDocumentKeyByteSize(FSTDocumentKey *key) {
size_t count = 0;
for (auto it = key.path.begin(); it != key.path.end(); it++) {
count += (*it).size();
Copy link
Contributor

Choose a reason for hiding this comment

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

How exact is this meant to be? At the very least it's excluding the path separators.

Copy link
Author

Choose a reason for hiding this comment

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

I've updated the comment to reflect that this is an estimate. It's for memory persistence, so it doesn't have to be exact, and furthermore I don't think we actually store the separators in memory? I believe it's a vector of segments, or something like that.

@wilhuff wilhuff assigned gsoltis and unassigned wilhuff Oct 1, 2018
Copy link
Contributor

@wilhuff wilhuff left a comment

Choose a reason for hiding this comment

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

LGTM

@gsoltis gsoltis merged commit 7c4a12f into master Oct 1, 2018
@gsoltis gsoltis deleted the gsoltis/lru_bytesize branch October 1, 2018 23:35
@firebase firebase locked and limited conversation to collaborators Oct 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants