-
-
Notifications
You must be signed in to change notification settings - Fork 731
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
Remove non-spec functionality from std.json
by removing object field sorting in toJSON
.
#6084
Conversation
Thanks for your pull request, @marler8997! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Some tips to help speed things up:
Bear in mind that large or tricky changes may require multiple rounds of review and revision. Please see CONTRIBUTING.md for more information. Bugzilla referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. |
std.json
by remove object field sorting in toJSON
.std.json
by removing object field sorting in toJSON
.
109c185
to
e2737a7
Compare
c9c8449
to
e4ebfe2
Compare
e4ebfe2
to
1433ec7
Compare
Forgive the dumb question, but, would this field order sorting that's being removed here help with the initial problem that #6059 set out to solve (which I understand to be testing DMD's JSON output)? Simply sorting the fields to some stable order, incl. lexicographically, seems to satisfy the necessities of that particular use case.
While pedantically true, I wonder if we can eat the practical implications of this change. AA iteration order varies across D versions and (I think) the bitness of the target CPU architecture. Imposing a breaking change onto our users and then simply telling them that they were doing it wrong when they complain will not be doing them a service. |
If we compared the JSON files by loading them in and comparing the data structures then it would work yes. However, it's more useful to show a diff at the file level since the file is what actually needs to be maintained, not the data structures that represent the files. |
Well, why not sort the file before writing it? I understand that, given the present situation, it's as easy as |
That would "hopefully" work:) Hopefully the ordering of the associative array key/values will be the same on different runs. But even if it did work, you end up with files where the once nicely ordered fields are no longer ordered logically, and now it's much harder to fix the expected file since the contents that were "diff'd" are in a completely different order. |
You can avoid that problem by also keeping the expected file sorted. |
Since order isn't guaranteed, the order can change on each platform. |
Oh sorry you were saying with the current state of things, yes you're right |
My thoughts:
|
Pinging @quickfur No need to rush in getting a reply, but at some point I'd like to get your input on this PR. Thanks. |
@wilzbach Browsers may have preserved insertion order (up to certain exceptions, as pointed out by the replies in the discussion you linked to), but that's not what I'd say:
So unless we can justify this breakage, e.g. if there is a huge performance win (which I doubt, given that this is If we do decide to go ahead with this, we'd better be sure to put up a big fat warning in the changelog to warn people of the potential breakage. |
@quickfur Thanks for your comments. I think in this case, the potential for breaking others code is likely, I am a bit confused though. The second point you made, an argument for not integrating this change is also an argument for integrating #6059 and contradicts what you said in criticism of that PR.
Did you change your mind? |
Well, that's why I said "arguably" in italics. In an ideal world, we'd only deal with strictly spec-only behaviour, so there would be no sorting / explicit order preservation anywhere. But we're dealing with a non-ideal situation where OTOH, with PR #6059, there was no existing non-spec behaviour, and that PR is proposing to introduce such behaviour. Our hands are not tied in that case, and we can choose not to make the situation worse by adding more non-spec behaviour to Taking a step back from these details, though: I think the amount of effort spent trying to improve |
I agree that when you take all arguments into account the decision to not integrate this PR is clear. But the arguments you made in each PR stand on their own and very clearly contradict each other. In one case you say |
I really did not want to get into splitting hairs over this, but since you asked: There is a subtle, but important distinction between a spec allowing an implementation to choose some particular order in its output, vs. an implementation actively, or deliberately, choosing a particular order. When a spec says something is implementation-defined or unspecified, the intent is usually that the implementation would choose whatever is most convenient for it -- since client code should not rely on unspecified behaviours, that means whatever order the implementation happens to produce should not cause any problems. So the implementor has more flexibility in how he implements the feature, since he's not bound by any requirements that the output must be in some particular order. Usually, this flexibility is used to make the implementation more performant and/or more memory-efficient, or perhaps whatever the implementor finds easiest to write. Actively choosing a particular order, or even more overtly, giving client code control over the order (PR #6059) where a spec says the order is undefined, is something else. Explicitly sorting is something that introduces an extra cost, but is unnecessary because the spec doesn't require the output to be sorted. So if the implementor were given a choice, not sorting seems to be the better option, but neither violates the spec (the spec says the order is unspecified; sorted is included in "unspecified"). OTOH, as a library giving user code the option of specifying a sorting order, in other words, sorting order is now a feature of your library -- that's endorsing reliance on a particular order and encouraging user code to rely on it, whereas the spec clearly says the order is undefined. It's this endorsement that, even if it may not break the letter of the spec, clearly goes against the spirit of the spec, because now user code is encouraged to rely on something the spec says is undefined (otherwise why would the library implementor go out of his way to implement the possibility of choosing a sort order in the first place). |
While attempting to enhance
std.json
to support ordering object fields, I noticed that it was sorting fields alphabetically when converting an object back to it's JSON string representation. This extra operation not only has a performance cost, but also imposes an ordering that is not defined by the spec. @quickfur has argued that such "non-spec" functionality should not exist instd.json
so this PR has emerged as a result.#6059 (comment)