Skip to content

Improvement on Immutable Lists - #10

Merged
carldebilly merged 3 commits into
masterfrom
dev/cdb/immutable-list
Mar 12, 2018
Merged

Improvement on Immutable Lists#10
carldebilly merged 3 commits into
masterfrom
dev/cdb/immutable-list

Conversation

@carldebilly

@carldebilly carldebilly commented Feb 26, 2018

Copy link
Copy Markdown
Member

Uno.Core's version of ImmutableList<T> implements IImmutableList<T> from Microsoft's Immutable Collections.

@carldebilly carldebilly added the enhancement New feature or request label Feb 26, 2018
@carldebilly carldebilly self-assigned this Feb 26, 2018
@carldebilly
carldebilly force-pushed the dev/cdb/immutable-list branch from 2e2e8e8 to 0bfaa2a Compare March 9, 2018 20:47
{
this.data = data;
_data = new T[data.Length];
Array.Copy(data, _data, data.Length);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does copying all elements each time is not the perf issue we try to avoid? At least I think we should add a private ctor which allow to bypass this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Changed.

public int IndexOf(T item, int index, int count, IEqualityComparer<T> equalityComparer)
{
var comparer = equalityComparer ?? EqualityComparer<T>.Default;
for (var i = index; i < index + count; i++)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Possible OutOfRange. How does behave the ImmutableList from ImmutableCollections ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just checked: yes, same behavior.

public int LastIndexOf(T item, int index, int count, IEqualityComparer<T> equalityComparer)
{
var comparer = equalityComparer ?? EqualityComparer<T>.Default;
for (var i = index + count - 1; i >= index; i--)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same thing here, possible OutOfRange

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just checked: yes, same behavior.

var newData = new T[_data.Length + itemsToAdd.Length];
Array.Copy(_data, newData, _data.Length);
Array.Copy(itemsToAdd, 0, newData, _data.Length, itemsToAdd.Length);
return new ImmutableList<T>(newData);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here an exemple of why we should have a constructor which does not create a new copy of the array

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.

public IImmutableList<T> RemoveAll(Predicate<T> match)
{
var newData = _data.Where(x => !match(x)).ToArray();
return new ImmutableList<T>(newData);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If there is not matches, we should return this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll do it.


public IImmutableList<T> RemoveAll(Predicate<T> match)
{
var newData = _data.Where(x => !match(x)).ToArray();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there a reason why you used Linq here but nowhere else in the class?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

lazyness.

}



Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Blank lines

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed.


public IImmutableList<T> SetItem(int index, T value)
{
if (Equals(_data[index], value))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think here you should use ReferenceEquals

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No we need to invoke equality members there.


public IImmutableList<T> Replace(T oldValue, T newValue, IEqualityComparer<T> equalityComparer)
{
if (Equals(oldValue, newValue))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same thing here: ReferenceEquals

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Idem.

Comment thread src/Uno.Core/Options/Option.cs Outdated
/// Creates an option for a given value.
/// </summary>
public static Some<T> Some<T>(T value) => new Some<T>(value);
public static Option<T> Some<T>(T value) => new Some<T>(value);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Alleluia

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's a binary breaking change... why is it needed ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It has been delayed in the past because of this reason. Was waiting for the right moment to push it. We can put it back on backburner if you want.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Has been moved to pr #11

get { return _empty; }
/// <param name="data">An array as source</param>
/// <param name="copyData">If the array should be copied</param>
public ImmutableList(T[] data, bool copyData = false)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we want to let this public ? If so, does the copyData should not be set to true by default ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

copyData is false by default because of existing code using it right now - as it's the previous behavior.

@jeromelaban what do you think?

@carldebilly
carldebilly force-pushed the dev/cdb/immutable-list branch from 1f98923 to 836df78 Compare March 12, 2018 18:45
@carldebilly
carldebilly merged commit 7467b9e into master Mar 12, 2018
@carldebilly
carldebilly deleted the dev/cdb/immutable-list branch March 12, 2018 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants