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

Improve Osa implementation #62

Merged
merged 1 commit into from
Jan 4, 2024
Merged

Improve Osa implementation #62

merged 1 commit into from
Jan 4, 2024

Conversation

maxbachmann
Copy link
Member

This reduces the binary size of osa_distance by more than 25% while improving the performance.

Binary size before change:

File .text   Size  Crate Name
0.0%  0.7% 1.9KiB strsim strsim::osa_distance2
0.0%  0.1%   199B strsim alloc::raw_vec::RawVec<T,A>::reserve::do_reserve_and_handle
0.0%  0.1%   140B strsim alloc::raw_vec::finish_grow
0.0%  0.0%    28B strsim core::ptr::drop_in_place<alloc::vec::Vec<usize>>
0.0%  0.0%     0B        And 0 smaller methods. Use -n N to show more.
0.1%  0.9% 2.3KiB        filtered data size, the file size is 4.4MiB

Binary size after the change:

File .text   Size  Crate Name
0.0%  0.6% 1.7KiB strsim strsim::osa_distance
0.0%  0.0%    28B strsim core::ptr::drop_in_place<alloc::vec::Vec<usize>>
0.0%  0.0%     0B        And 0 smaller methods. Use -n N to show more.
0.0%  0.7% 1.7KiB        filtered data size, the file size is 4.4MiB

The following graph shows the runtime for random strings of different lengths:

image

let b_len = b.chars().count();
if a == b {
Copy link
Member Author

Choose a reason for hiding this comment

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

This special case would help performance if the two strings are similar, but hurt if they are not.

Generally it would be better to reduce the common prefix + postfix and return early in case the remaining length is 0. This has the same runtime, but always either helps the performance or doesn't really hurt. In our case the added binary size isn't really worth it though.

Copy link
Member

Choose a reason for hiding this comment

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

I would also guess that it's rare to actually hit these cases in real world usage.

@maxbachmann
Copy link
Member Author

maxbachmann commented Jan 1, 2024

I think we could probably get away with two vectors instead of three. However that's more work and I would need to test the effect of the additional bookkeeping on performance and binary size. For this reason I will try this separately.

This reduces the binary size of osa_distance by more than 25% while
improving the performance.
Copy link
Member

@dguo dguo left a comment

Choose a reason for hiding this comment

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

This is awesome! Thanks for the comparison data!

let b_len = b.chars().count();
if a == b {
Copy link
Member

Choose a reason for hiding this comment

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

I would also guess that it's rare to actually hit these cases in real world usage.

@dguo dguo merged commit 66822fe into main Jan 4, 2024
12 checks passed
@dguo dguo deleted the osa branch January 4, 2024 03:15
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.

2 participants