Skip to content

Make copy and reference clear at ValueFactory#324

Merged
xerial merged 2 commits into
developfrom
value-factory-copy
Jan 7, 2016
Merged

Make copy and reference clear at ValueFactory#324
xerial merged 2 commits into
developfrom
value-factory-copy

Conversation

@frsyuki
Copy link
Copy Markdown
Member

@frsyuki frsyuki commented Jan 5, 2016

ValueFactory .newBinary, .newString, .newArray and .newMap take a
mutable argument. Some of them used them as a reference and some of them
copied them. This pull-request makes it clear that they copy the
argument by default and try to omit the copy if an extra argument is
set to true.

This change also includes:

  • newMap(Value... kvs) method which was previously newMap(Value[] kvs)
  • MessageUnpacker.unpackValue removes a duplicated copy of new byte[](string and binary) or Value[](array and map).

ValueFactory .newBinary, .newString, .newArray and .newMap take a
mutable argument. Some of them used them as a reference and some of them
copied them. This pull-request makes it clear that they copy the
argument by default and try to omit the copy if an extra argument is
set to true.

This change also includes:

* newMap(Value... kvs) method which was previously newMap(Value[] kvs)
* MessageUnpacker.unpackValue removes a duplicated copy of new byte[]
  (string and binary) or Value[] (array and map).
@frsyuki
Copy link
Copy Markdown
Member Author

frsyuki commented Jan 5, 2016

@xerial Another possible design is adding new methods (such as newBinaryRef, newArrayRef) instead of adding an extra argument. With this way, users will expect that -Ref methods will always use reference. But in fact, they use reference only if off == 0 && len == b.length because ImmutableBinaryValue and ImmutableStringValue (in terms of implementation, AbstractImmutableRawValue) don't accept offset and length. So AbstractImmutableRawValue also needs more code. An ideas?

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.

The meaning of ref is a little bit ambiguous. How about using boolean createCopy?

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.

The "correct" name is useReferenceIfPossible but it seems too long. How about boolean omitCopy instead?
I think inverting true/false is confusing because using true as the default seems unexpected.

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.

omitCopy is fine.

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 changed it to omitCopy.

@xerial
Copy link
Copy Markdown
Member

xerial commented Jan 7, 2016

ValueFactory is just a collection of helper methods and users simply need to use newXXX methods without omitCopy flag. In this sense, MessageUnpacker is the only user who cares about the performance when creating Value objects. I think simply having the omitCopy flag is sufficient.

xerial added a commit that referenced this pull request Jan 7, 2016
Make copy and reference clear at ValueFactory
@xerial xerial merged commit 41ef827 into develop Jan 7, 2016
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