Skip to content

Improve performance and optimize memory #67

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 8 commits into from
May 31, 2024
Merged

Improve performance and optimize memory #67

merged 8 commits into from
May 31, 2024

Conversation

rainyl
Copy link
Owner

@rainyl rainyl commented May 30, 2024

Reduce the consumption, improve performance

  • Optimize the use of Arenas
  • Add dispose() to all classes with native pointer
  • Add detach to the constructors with native pointer

@rainyl rainyl linked an issue May 30, 2024 that may be closed by this pull request
@codecov-commenter
Copy link

codecov-commenter commented May 30, 2024

Codecov Report

Attention: Patch coverage is 89.98779% with 82 lines in your changes are missing coverage. Please review.

Project coverage is 89.50%. Comparing base (a04f909) to head (a6e0907).

Files Patch % Lines
lib/src/core/point.dart 80.30% 13 Missing ⚠️
lib/src/imgproc/imgproc.dart 81.81% 10 Missing ⚠️
lib/src/core/cv_vec.dart 94.80% 8 Missing ⚠️
lib/src/core/mat.dart 79.48% 8 Missing ⚠️
lib/src/core/rect.dart 73.33% 8 Missing ⚠️
lib/src/highgui/highgui.dart 27.27% 8 Missing ⚠️
lib/src/core/array.dart 80.00% 7 Missing ⚠️
lib/src/core/asyncarray.dart 28.57% 5 Missing ⚠️
lib/src/dnn/dnn.dart 83.87% 5 Missing ⚠️
lib/src/features2d/features2d.dart 96.69% 4 Missing ⚠️
... and 3 more

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #67      +/-   ##
==========================================
+ Coverage   88.97%   89.50%   +0.53%     
==========================================
  Files          36       36              
  Lines        4897     5165     +268     
==========================================
+ Hits         4357     4623     +266     
- Misses        540      542       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@rainyl
Copy link
Owner Author

rainyl commented May 31, 2024

Ready to merge.

Note:

  • Wrapper classes with large native resources, users now can free them manually with dispose() to free native resources, but note that the dart instances will still be available, users have to ensure not to access them if disposed, dart instances are small and believe dart VM/GC to manage them.

Example:

import 'package:opencv_dart/opencv_dart.dart' as cv;

void main() {
  const counts = 10000;
  final mat = cv.Mat.zeros(1000, 1000, cv.MatType.CV_8UC3);
  final sw = Stopwatch()..start();
  for (var count = 0; count < counts; count++) {
    final mat1 = cv.cvtColor(mat, cv.COLOR_BGR2YCrCb);
    // manually dispose will reduce the memory consumption
    mat1.dispose();
  }
  sw.stop();
  print(
      "All: ${sw.elapsedMicroseconds}μs, counts: $counts, per: ${sw.elapsedMicroseconds / counts} μs");
}

Without mat1.dispose(), peak memory: ~18G

All: 5527358μs, counts: 10000, per: 552.7358 μs

With mat1.dispose(), peak memory: ~240M

All: 7263633μs, counts: 10000, per: 726.3633 μs

@rainyl rainyl merged commit 822c0f6 into main May 31, 2024
5 checks passed
@abdelaziz-mahdy
Copy link
Contributor

Ready to merge.

Note:

  • Wrapper classes with large native resources, users now can free them manually with dispose() to free native resources, but note that the dart instances will still be available, users have to ensure not to access them if disposed, dart instances are small and believe dart VM/GC to manage them.

Example:

import 'package:opencv_dart/opencv_dart.dart' as cv;

void main() {
  const counts = 10000;
  final mat = cv.Mat.zeros(1000, 1000, cv.MatType.CV_8UC3);
  final sw = Stopwatch()..start();
  for (var count = 0; count < counts; count++) {
    final mat1 = cv.cvtColor(mat, cv.COLOR_BGR2YCrCb);
    // manually dispose will reduce the memory consumption
    mat1.dispose();
  }
  sw.stop();
  print(
      "All: ${sw.elapsedMicroseconds}μs, counts: $counts, per: ${sw.elapsedMicroseconds / counts} μs");
}

Without mat1.dispose(), peak memory: ~18G

All: 5527358μs, counts: 10000, per: 552.7358 μs

With mat1.dispose(), peak memory: ~240M

All: 7263633μs, counts: 10000, per: 726.3633 μs

Does this mean the package user will have to manage the object? I another way do I have to dispose every native asset I use?

@rainyl
Copy link
Owner Author

rainyl commented May 31, 2024

package user will have to manage the object?

Ready to merge.
Note:

  • Wrapper classes with large native resources, users now can free them manually with dispose() to free native resources, but note that the dart instances will still be available, users have to ensure not to access them if disposed, dart instances are small and believe dart VM/GC to manage them.

Example:

import 'package:opencv_dart/opencv_dart.dart' as cv;

void main() {
  const counts = 10000;
  final mat = cv.Mat.zeros(1000, 1000, cv.MatType.CV_8UC3);
  final sw = Stopwatch()..start();
  for (var count = 0; count < counts; count++) {
    final mat1 = cv.cvtColor(mat, cv.COLOR_BGR2YCrCb);
    // manually dispose will reduce the memory consumption
    mat1.dispose();
  }
  sw.stop();
  print(
      "All: ${sw.elapsedMicroseconds}μs, counts: $counts, per: ${sw.elapsedMicroseconds / counts} μs");
}

Without mat1.dispose(), peak memory: ~18G

All: 5527358μs, counts: 10000, per: 552.7358 μs

With mat1.dispose(), peak memory: ~240M

All: 7263633μs, counts: 10000, per: 726.3633 μs

Does this mean the package user will have to manage the object? I another way do I have to dispose every native asset I use?

Not necessary, the above test is for dart, I didn't test in Flutter but Flutter will trigger GC more frequently than pure dart, so it tends to rely on GC to free, BUT, if users can know that the APP will process large images (e.g., 3840*2160*3), it's recommend to dispose manually.

Anyway, this PR just provides a potential to manage native resources more efficiently according to the weakness of the GC of dart VM, but not necessary.

@abdelaziz-mahdy
Copy link
Contributor

Oh got it , so GC works too but if needed manual mode can be used for optimization

@rainyl
Copy link
Owner Author

rainyl commented May 31, 2024

Oh got it , so GC works too but if needed manual mode can be used for optimization

bingo!

rainyl added a commit that referenced this pull request Jun 1, 2024
@rainyl rainyl deleted the issue-64 branch June 3, 2024 09:12
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.

Memory consumption
3 participants