Skip to content

Conversation

@dconeybe
Copy link
Contributor

@dconeybe dconeybe commented Mar 21, 2023

This PR avoids copying the bytes of the bloom filter sent from the server in the "existence filter" message. Previously, these bytes were copied from the proto into a vector in the "BloomFilterParameters" struct. The bloom filter byte array could be somewhat large, in the ballpark of 8kb, so avoiding the copy is desirable, especially since the bloom filter is only ever used in the somewhat-rare case of an existence filter mismatch.

The challenge of avoiding the copy is that "stealing" the bytes must be done carefully to avoid both memory leaks and double-freeing of the dynamically-allocated byte array. Since the nanopb::Message class models a unique ownership like std::unique_ptr, we decided to simply pass around the nanopb::Message for the bloom filter.

See #10953 (comment) for more details.

#no-changelog

milaGGL and others added 30 commits February 15, 2023 21:52
* upload initial code

* add golden test with md5 calculator

* resolve comments

* add static const to kGoldenDocumentPrefix

* resolve comments

* resolve comments
…/BloomFilter-apply-bloom-filter-on-existence-filter-mismatch
…nto mila/BloomFilter-apply-bloom-filter-on-existence-filter-mismatch"

This reverts commit 2b8533c, reversing
changes made to 4158352.
…/BloomFilter-apply-bloom-filter-on-existence-filter-mismatch
@dconeybe dconeybe self-assigned this Mar 21, 2023
Base automatically changed from mila/BloomFilter-apply-bloom-filter-on-existence-filter-mismatch to mila/BloomFilter March 23, 2023 20:38
@milaGGL milaGGL marked this pull request as ready for review March 27, 2023 23:30
bloom_filter_copy->bits.padding = 7;
bloom_filter_copy->bits.bitmap =
nanopb::MakeBytesArray(std::vector<uint8_t>{0x42, 0xFE});
EXPECT_TRUE(change.filter().bloom_filter().value() == bloom_filter_copy);
Copy link
Contributor

Choose a reason for hiding this comment

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

EXPECT_EQ(change.filter().bloom_filter().value(), bloom_filter_copy); crashes the build.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem was that the operator== for google_firestore_v1_BloomFilter was defined in the wrong place. It was defined in the firebase::firestore::remote namespace in existence_filter.h, but should have been defined in the firebase::firestore namespace (the same namespace in which google_firestore_v1_BloomFilter is defined). Being in the wrong namespace prevented it from being found by ADL (address-dependent lookup) at compile time.

It seemed like a good idea to move the operator== functions out of the existence_filter.h file. So I did that by adding a new operators.h file in the nanopb directory. I also added unit tests for it (which need a bit more work in a follow-up PR).

I also added operator== for nanopb::Message, which was missing. This allows comparing nanopb::Message objects directly with each other, and eventually calling the operator== overloads for the underlying protos.

With these changes, the EXPECT_EQ now works, and the code is much better as well, with proper design and unit test coverage.

See 7c7cb61

… protos: google_firestore_v1_BloomFilter and google_firestore_v1_BitSequence. Move the implementation of the operator== overloads for these two protos from existence_filter.h into the new file operators.h, and into the correct namespace.

TODO: Add tests in operators_test.cc for google_firestore_v1_BloomFilter
…y adding `NanopbOperatorsTest_BitSequence` fixture class and a `TestOperatorEquals()` method, which makes each TEST_F() function a single line.
Base automatically changed from mila/BloomFilter to master June 20, 2023 16:10
@dconeybe
Copy link
Contributor Author

I'm abandoning this work, for now.

@dconeybe dconeybe closed this Aug 15, 2023
@dconeybe dconeybe deleted the dconeybe/bloom_filter_defer_copying_bytes branch August 15, 2023 15:15
@firebase firebase locked and limited conversation to collaborators Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants