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

Store vtables sorted in Rust builder #6765

Merged
merged 3 commits into from
Aug 3, 2021
Merged

Store vtables sorted in Rust builder #6765

merged 3 commits into from
Aug 3, 2021

Conversation

CasperN
Copy link
Collaborator

@CasperN CasperN commented Aug 2, 2021

The previous implementation was slow if there were too many tables.
It linearly searches a vector for vtable duplicates. The new implementation
binary searches a vector for duplicates.

Asymototically when inserting the n^th vtable: The old implementation
took O(n) lookup steps and O(1) insertion. The new implementation is
O(log n) lookup and O(n) insertion. This might be improved further by
using a balanced binary tree stored in a vector.

Note that std::collections do not work for us. This is because we store keys
into the flatbuffer's owned buffer. This buffer may move due to reallocation
so we cannot rely on references/pointers, instead we must pass the buffer
with each comparison, which does not fit the standard API.

Benchmarking, create_many_tables is 7.5x faster (on my laptop):

// Simple vector cache
test create_many_tables ... bench: 728,875 ns/iter (+/- 12,279) = 44 MB/s

// Sorted vector cache
test create_many_tables ... bench: 97,843 ns/iter (+/- 4,430) = 334 MB/s

@github-actions github-actions bot added c++ codegen Involving generating code from schema rust labels Aug 2, 2021
Casper Neo added 2 commits August 2, 2021 16:46
The previous implementation was slow if there were too many tables.

Asymototically when inserting the n^th vtable: The old implementation
took O(n) lookup steps and O(1) insertion. The new implementation is
O(log n) lookup and O(n) insertion. This might be improved further by
using a balanced btree.

Benchmarking, create_many_tables is 7.5x faster (on my laptop):

// Simple vector cache
test create_many_tables ... bench: 728,875 ns/iter (+/- 12,279) = 44 MB/s

// Sorted vector cache
test create_many_tables ... bench: 97,843 ns/iter (+/- 4,430) = 334 MB/s
@github-actions github-actions bot removed codegen Involving generating code from schema c++ labels Aug 2, 2021
@CasperN
Copy link
Collaborator Author

CasperN commented Aug 3, 2021

#6511

@CasperN CasperN merged commit 35e2cac into google:master Aug 3, 2021
@CasperN CasperN deleted the faster branch August 16, 2021 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant