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

refactor: Make it thread-safe #54

Merged
merged 1 commit into from
Jul 31, 2024
Merged

refactor: Make it thread-safe #54

merged 1 commit into from
Jul 31, 2024

Conversation

Iceapinan
Copy link
Contributor

The current implementation of rxing uses Rc for reference counting, which is not thread-safe. This limitation restricts the library’s use in multi-threaded applications where safe concurrent access to shared resources is required.

Passed all test cases successfully.

@hschimke
Copy link
Collaborator

Hello, I'm going to admit my lack of familiarity with thread-safety in rust. Does the entire library need to be fully thread-safe in order to be used in a multi-threaded context? I understand why you might want the things passed in and out of the library to be fully thread-safe, but I'm unclear on if every internal bit has to be Arc. I'm curious about the context where this doesn't work.

@hschimke
Copy link
Collaborator

Actually now that I have read a bit more and tested it I see the point. I'll review this later this week. Thanks

@Iceapinan
Copy link
Contributor Author

Iceapinan commented Jul 30, 2024

Correct. The library must be thread-safe to be used in a multi-threaded context, otherwise in Rust, it won't even compile. I recommend using Arc instead of Rc as it provides library users with more flexibility.

I also really need it in my application. I've already forked the repo and used it.

@hschimke
Copy link
Collaborator

I'm about half done with the review. Do you have benchmarks for the before-after change (I'd like to include them in the patch notes).

@Iceapinan
Copy link
Contributor Author

Before

aztec time: [51.649 µs 51.689 µs 51.737 µs]
Found 9 outliers among 100 measurements (9.00%)
4 (4.00%) high mild
5 (5.00%) high severe

codabar time: [1.7609 µs 1.7645 µs 1.7682 µs]
Found 2 outliers among 100 measurements (2.00%)
1 (1.00%) high mild
1 (1.00%) high severe

code39 time: [3.6943 µs 3.7049 µs 3.7145 µs]
Found 5 outliers among 100 measurements (5.00%)
1 (1.00%) low mild
3 (3.00%) high mild
1 (1.00%) high severe

code93 time: [1.0691 µs 1.0760 µs 1.0820 µs]
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high mild

datamatrix time: [13.575 µs 13.594 µs 13.613 µs]
Found 4 outliers among 100 measurements (4.00%)
3 (3.00%) low mild
1 (1.00%) high severe

ean8 time: [1.1383 µs 1.1400 µs 1.1418 µs]
Found 4 outliers among 100 measurements (4.00%)
3 (3.00%) low mild
1 (1.00%) high mild

ean13 time: [3.9190 µs 3.9289 µs 3.9390 µs]
Found 3 outliers among 100 measurements (3.00%)
1 (1.00%) low mild
2 (2.00%) high severe

itf time: [1.4751 µs 1.4870 µs 1.5113 µs]
Found 3 outliers among 100 measurements (3.00%)
1 (1.00%) low severe
1 (1.00%) low mild
1 (1.00%) high severe

maxicode time: [70.059 µs 70.152 µs 70.264 µs]
Found 7 outliers among 100 measurements (7.00%)
2 (2.00%) low mild
1 (1.00%) high mild
4 (4.00%) high severe

pdf417 time: [96.889 µs 97.199 µs 97.718 µs]
Found 11 outliers among 100 measurements (11.00%)
1 (1.00%) low severe
1 (1.00%) low mild
7 (7.00%) high mild
2 (2.00%) high severe

qrcode time: [386.03 µs 387.06 µs 388.17 µs]
Found 9 outliers among 100 measurements (9.00%)
1 (1.00%) low mild
4 (4.00%) high mild
4 (4.00%) high severe

rss14 time: [8.6547 µs 8.6795 µs 8.7127 µs]
Found 10 outliers among 100 measurements (10.00%)
2 (2.00%) low severe
4 (4.00%) low mild
3 (3.00%) high mild
1 (1.00%) high severe

rss_expanded time: [6.7197 µs 6.7342 µs 6.7577 µs]
Found 3 outliers among 100 measurements (3.00%)
1 (1.00%) low severe
1 (1.00%) high mild
1 (1.00%) high severe

telepen time: [2.0463 µs 2.0526 µs 2.0593 µs]
Found 2 outliers among 100 measurements (2.00%)
1 (1.00%) low mild
1 (1.00%) high mild

upca time: [26.738 µs 26.782 µs 26.827 µs]
Found 5 outliers among 100 measurements (5.00%)
2 (2.00%) low severe
3 (3.00%) high mild

upce time: [945.95 ns 947.34 ns 948.71 ns]
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high severe

multi_barcode time: [2.7054 ms 2.7094 ms 2.7135 ms]
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high mild

After

aztec time: [51.292 µs 51.378 µs 51.468 µs]
change: [-0.9825% -0.7879% -0.5641%] (p = 0.00 < 0.05)
Change within noise threshold.
Found 6 outliers among 100 measurements (6.00%)
5 (5.00%) high mild
1 (1.00%) high severe

codabar time: [1.7456 µs 1.7565 µs 1.7731 µs]
change: [-1.1738% -0.7988% -0.4175%] (p = 0.00 < 0.05)
Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
1 (1.00%) high mild
1 (1.00%) high severe

code39 time: [3.6865 µs 3.6921 µs 3.6981 µs]
change: [-0.3382% +0.2073% +0.8987%] (p = 0.53 > 0.05)
No change in performance detected.
Found 5 outliers among 100 measurements (5.00%)
4 (4.00%) high mild
1 (1.00%) high severe

code93 time: [1.0543 µs 1.0574 µs 1.0612 µs]
change: [-0.9329% +0.5383% +2.5858%] (p = 0.65 > 0.05)
No change in performance detected.
Found 15 outliers among 100 measurements (15.00%)
6 (6.00%) low mild
5 (5.00%) high mild
4 (4.00%) high severe

datamatrix time: [13.387 µs 13.420 µs 13.454 µs]
change: [-1.4722% -1.1283% -0.7649%] (p = 0.00 < 0.05)
Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
1 (1.00%) high mild
1 (1.00%) high severe

ean8 time: [1.1182 µs 1.1206 µs 1.1234 µs]
change: [-1.8838% -1.6391% -1.3947%] (p = 0.00 < 0.05)
Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) high mild

ean13 time: [3.8146 µs 3.8209 µs 3.8275 µs]
change: [-2.8822% -2.5939% -2.3045%] (p = 0.00 < 0.05)
Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) high mild

itf time: [1.4424 µs 1.4456 µs 1.4492 µs]
change: [-2.8474% -1.7848% -1.0798%] (p = 0.00 < 0.05)
Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
1 (1.00%) high mild
1 (1.00%) high severe

maxicode time: [68.368 µs 68.514 µs 68.675 µs]
change: [-9.3811% -4.9183% -1.8387%] (p = 0.00 < 0.05)
Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
4 (4.00%) high mild
2 (2.00%) high severe

pdf417 time: [94.042 µs 94.490 µs 95.307 µs]
change: [-3.5205% -2.9157% -2.3693%] (p = 0.00 < 0.05)
Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
1 (1.00%) low mild
6 (6.00%) high mild
5 (5.00%) high severe

qrcode time: [350.42 µs 352.26 µs 354.29 µs]
change: [-11.115% -10.022% -9.1687%] (p = 0.00 < 0.05)
Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
3 (3.00%) high mild
1 (1.00%) high severe

rss14 time: [8.4125 µs 8.4346 µs 8.4562 µs]
change: [-3.1137% -2.7348% -2.4195%] (p = 0.00 < 0.05)
Performance has improved.
Found 17 outliers among 100 measurements (17.00%)
4 (4.00%) low severe
3 (3.00%) low mild
4 (4.00%) high mild
6 (6.00%) high severe

rss_expanded time: [6.5365 µs 6.5546 µs 6.5740 µs]
change: [-3.2475% -2.7949% -2.4064%] (p = 0.00 < 0.05)
Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) high mild

telepen time: [1.9871 µs 1.9932 µs 1.9999 µs]
change: [-1.8368% -1.3395% -0.8800%] (p = 0.00 < 0.05)
Change within noise threshold.

upca time: [26.086 µs 26.204 µs 26.390 µs]
change: [-2.1386% -1.7858% -1.3706%] (p = 0.00 < 0.05)
Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) high severe

upce time: [925.68 ns 928.49 ns 931.35 ns]
change: [-2.1126% -1.8347% -1.5451%] (p = 0.00 < 0.05)
Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
3 (3.00%) low mild
3 (3.00%) high mild

multi_barcode time: [2.6075 ms 2.6118 ms 2.6169 ms]
change: [-3.8154% -3.6005% -3.3837%] (p = 0.00 < 0.05)
Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
4 (4.00%) high mild
2 (2.00%) high severe

@hschimke
Copy link
Collaborator

Thank you, I'll finish review and have it merged tomorrow.

Due to the changes to the public surface this is going to be v0.6.0

Copy link
Collaborator

@hschimke hschimke left a comment

Choose a reason for hiding this comment

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

This looks ok to me. I am going to merge it later today and then likely spend some time removing as many uses of Arc as possible (there are several internal areas of the program that likely do not need to use reference counting at all).

@hschimke hschimke merged commit d775047 into rxing-core:main Jul 31, 2024
1 check passed
@hschimke
Copy link
Collaborator

I'm going to spend time today trying to clean up uses or Arc, I did switch the definition of the result point callback to require the function to be Send+Sync so that it can be used in static contexts.

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