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

CompositeCloseable: prepend doesn't respect ordering #2097

Merged
merged 2 commits into from
Feb 23, 2022

Conversation

idelpivnitskiy
Copy link
Member

Motivation:

DefaultCompositeCloseable does not respect ordering if prepend
operation is invoked after append. It causes issues when the ordering
matters for closeable resources that depend upon each other.

Modifications:

  • Change Operand.closables to ArrayDeque;
  • Use addFirst for prepend operation;
  • Test ordiring for "prepend after append" and "prepend after merge";

Result:

Correct ordering of invoking AsyncCloseables if prepend is used
after append.

Motivation:

`DefaultCompositeCloseable` does not respect ordering if `prepend`
operation is invoked after `append`.

Modifications:

- Change `Operand.closables` to `ArrayDeque`;
- Use `addFirst` for `prepend` operation;
- Test ordiring for "prepend after append" and "prepend after merge";

Result:

Correct ordering of invoking `AsyncCloseable`s if `prepend` is used
after `append`.
@idelpivnitskiy idelpivnitskiy self-assigned this Feb 18, 2022
@idelpivnitskiy idelpivnitskiy added the bug Something isn't working label Feb 21, 2022
@@ -203,8 +202,8 @@ private Completable buildCompletable(Function<AsyncCloseable, Completable> close
}

private static final class Operand {
private final List<AsyncCloseable> closables = new ArrayList<>(4);
private final boolean isMerge;
final Deque<AsyncCloseable> closables = new ArrayDeque<>(2);
Copy link
Contributor

Choose a reason for hiding this comment

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

The default size of the collection changes. There is probably no reason not to still use 4 as that still typically fits within the alignment size of a heap allocation of a reference array. ie. The first resize to 4 will probably just reallocate to another array with less wasted space.

Copy link
Member

@Scottmitch Scottmitch left a comment

Choose a reason for hiding this comment

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

good catch!

@idelpivnitskiy idelpivnitskiy merged commit eb5e452 into apple:main Feb 23, 2022
@idelpivnitskiy idelpivnitskiy deleted the DefaultCompositeCloseable branch February 23, 2022 16:29
idelpivnitskiy added a commit that referenced this pull request Feb 23, 2022
Motivation:

`DefaultCompositeCloseable` does not respect ordering if `prepend`
operation is invoked after `append`.

Modifications:

- Change `Operand.closables` to `ArrayDeque`;
- Use `addFirst` for `prepend` operation;
- Test ordiring for "prepend after append" and "prepend after merge";

Result:

Correct ordering of invoking `AsyncCloseable`s if `prepend` is used
after `append`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants