Skip to content

[objective_c] avoid an extra copy in toNSData - #3761

Merged
brianquinlan merged 4 commits into
dart-lang:mainfrom
brianquinlan:tonsdata_single_copy
Oct 30, 2026
Merged

brianquinlan merged 4 commits into
dart-lang:mainfrom
brianquinlan:tonsdata_single_copy

Conversation

@brianquinlan

Copy link
Copy Markdown
Contributor

Description

Avoid an extra copy in toNSData

PR Checklist

  • I’ve reviewed the contributor guide and applied the relevant portions to this PR.
  • I've run dart tool/ci.dart --all locally and resolved all issues identified. This ensures the PR is formatted, has no lint errors, and ran all code generators. This applies to the packages part of the toplevel pubspec.yaml workspace.
  • All existing and new tests are passing. I added new tests to check the change I am making.
  • The PR is actually solving the issue. PRs that don't solve the issue will be closed. Please be respectful of the maintainers' time. If it's not clear what the issue is, feel free to ask questions on the GitHub issue before submitting a PR.
  • I have updated CHANGELOG.md for the relevant packages. (Not needed for small changes such as doc typos).
  • I have updated the pubspec package version if necessary.

`toNSData` copied the list into a temporary buffer and then copied that
buffer into a new `NSData`. Now the `NSData` takes ownership of the buffer
(`initWithBytesNoCopy:length:freeWhenDone:`), so the data is copied once.

`toNSData` originally worked this way (dart-lang#1104), but was changed to copy
twice in dart-lang#1100 because of crashes when the `NSData` was destroyed (dart-lang#1107).
Those crashes were caused by FFIgen treating `dataWithBytesNoCopy:length:`
as returning a retained object, because its name contains "Copy", so the
`NSData` was over-released. FFIgen has followed the Objective-C method
family rules since dart-lang#1463, and `initWithBytesNoCopy:length:freeWhenDone:` is
in the `init` family, so it really does return a retained object.

Adds tests that the list is copied, that the bytes stay valid while the
`NSData` is alive, and that they are freed when it is deallocated.
@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.84%. Comparing base (ad203b0) to head (4ce2c0b).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #3761   +/-   ##
=======================================
  Coverage   85.84%   85.84%           
=======================================
  Files         322      322           
  Lines       20876    20877    +1     
=======================================
+ Hits        17921    17922    +1     
  Misses       2955     2955           
Flag Coverage Δ
ffigen 91.36% <ø> (ø)
objective_c 62.87% <100.00%> (+<0.01%) ⬆️
swiftgen 80.41% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
code_assets 95.31% <ø> (ø)
data_assets 91.56% <ø> (ø)
ffi 100.00% <ø> (ø)
ffigen 91.45% <ø> (ø)
hooks 86.40% <ø> (ø)
hooks_runner 90.52% <ø> (ø)
jni 66.34% <ø> (ø)
jni_flutter ∅ <ø> (∅)
jni_util 25.00% <ø> (ø)
jnigen 82.85% <ø> (ø)
json_syntax_generator 42.01% <ø> (ø)
native_test_helpers 85.71% <ø> (ø)
native_toolchain_c 95.99% <ø> (ø)
objective_c 79.56% <100.00%> (+0.03%) ⬆️
pub_formats ∅ <ø> (∅)
record_use 81.48% <ø> (ø)
snippet_tool 66.23% <ø> (ø)
swift2objc 92.95% <ø> (ø)
swiftgen 80.41% <ø> (ø)
test_case_selector 97.28% <ø> (ø)
Files with missing lines Coverage Δ
pkgs/objective_c/lib/src/ns_data.dart 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@brianquinlan
brianquinlan merged commit 63f3e86 into dart-lang:main Oct 30, 2026
55 of 56 checks passed
copybara-service Bot pushed a commit to dart-lang/sdk that referenced this pull request Nov 4, 2026
Revisions updated by `dart tools/rev_sdk_deps.dart native`.

native (https://github.com/dart-lang/native/compare/e0ca7c6..b442038):
  b442038de  Mon Nov 2 10:46:15 2026 -0800  Brian Quinlan  [objective_c] support `scheduleInRunLoop:forMode:` (dart-lang/native#3763)
  264318e75  Fri Oct 30 12:49:03 2026 -0700  Brian Quinlan  [objective_c] fix `toNSInputStream` double copy (dart-lang/native#3762)
  63f3e8641  Fri Oct 30 12:09:04 2026 -0700  Brian Quinlan  [objective_c] avoid an extra copy in `toNSData`  (dart-lang/native#3761)
  ad203b01d  Fri Oct 30 11:48:15 2026 -0700  Brian Quinlan  [objective_c] increase read chunk size (dart-lang/native#3760)
  fa112d95b  Fri Oct 30 09:14:23 2026 -0700  Brian Quinlan  [objective_c] Fix a `NSInputStream` hang (dart-lang/native#3751)
  035f4ef38  Fri Oct 30 09:23:30 2026 +0100  Michael Goderbauer  [jnigen] Fix race conditions in tests (dart-lang/native#3767)
  d58a8f514  Fri Oct 30 00:57:49 2026 -0400  Jhin Lee  [code_assets] Preserve full Apple deployment versions (dart-lang/native#3742)

Migrate `MacOSCodeConfig` in `pkg/dartdev/lib/src/native_assets.dart` from the
deprecated `targetVersion` parameter to `deploymentVersion: AppleVersion(...)`.

Change-Id: I2df469fc05bda2c1db051f43e803fe5145f7c246
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/573102
Auto-Submit: Nate Bosch <[email protected]>
Reviewed-by: Nicholas Shahan <[email protected]>
Reviewed-by: Ben Konyi <[email protected]>
Commit-Queue: Ben Konyi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants