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

Improve Util.Pp pretty-printers #379

Merged
merged 8 commits into from
Aug 25, 2023
Merged

Improve Util.Pp pretty-printers #379

merged 8 commits into from
Aug 25, 2023

Conversation

shym
Copy link
Collaborator

@shym shym commented Jul 28, 2023

This PR brings a few features:

  • usage of Format boxes to provide the same formatting as deriving show and abide by Format’s rule that all content should be output into some box,
  • the possibility to require the output to be truncated,
  • a couple of pretty-printers that were missing,
  • a couple of typo fixes.

The possibility to truncate would address #308 if all pretty-printers in STM and Lin were always using Util.Pp (this is #343).

@jmid
Copy link
Collaborator

jmid commented Aug 21, 2023

LGTM, thanks!
I think an environment variable is the right solution, to allow for both a "shortened print" and a "full one" without having to recompile.

Should we do something similar for bytes?
Would it make sense to add unit/expect tests to ensure that it works as intended and protect ourselves against future breakage? 🤔
Finally (for another PR) I agree that we should seriously consider switching Lin+STM to use these for consistent
argument and result printing.

@shym
Copy link
Collaborator Author

shym commented Aug 22, 2023

I’ve added an internal test for the pretty-printers, with variants for the truncation.

Should we do something similar for bytes?

I don’t see what you have in mind, Util.Pp contains a pp_bytes pretty-printer.

@jmid
Copy link
Collaborator

jmid commented Aug 23, 2023

I don’t see what you have in mind, Util.Pp contains a pp_bytes pretty-printer.

My bad, I was too fast reading the cut off code as only applying to strings and not bytes 🤦
Since this is located in to_show I assume the cut off applies to all to-string-coercions (string, bytes, list, ...) passing through that function, right?

@jmid
Copy link
Collaborator

jmid commented Aug 23, 2023

The added tests look great BTW, and the expect outputs nicely answer the above question 😅

I guess having it print the final closing quote or square bracket like the OCaml toplevel will require some form of type-directed approach?

utop # String.make 375 'a';;
- : string =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"... (* string length 375; truncated *)
utop # List.init 375 (fun _ -> [0]);;
- : int list list =
[[0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; 
 [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; 
 [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; 
 [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; 
 [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; 
 [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; 
 [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; 
 [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; 
 [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; 
 [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; 
 [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [0]; [...]; ...]

This is just a thought - not meant as critique. I'm quite happy with this as a first version!

@shym
Copy link
Collaborator Author

shym commented Aug 23, 2023

The added tests look great BTW, and the expect outputs nicely answer the above question 😅

😅

I guess having it print the final closing quote or square bracket like the OCaml toplevel will require some form of type-directed approach?

I think so. At the level where the truncating mechanism is located in that PR, it sees only the stream of bytes to output, without structure. I wondered whether the boxing or tagging mechanisms in formatters could be retargeted for that kind of purposes, but I didn’t find how.

@shym
Copy link
Collaborator Author

shym commented Aug 23, 2023

Reviewing the CI logs I realise (finally!) that (obviously, as it is one of the main goal of Format) the result produced by to_show can be a multiline string while print_triple_vertical assumes it is not the case (see this example for instance).
(It must have been the case already with the pretty-printers generated by ppx_deriving.show since we have something really similar, hidden by the fact that long lines are not readable anyway)

A simple way to fix pretty-printing with print_triple_vertical is to set the formatter margin to max_int.
But now I wonder: do we want to_show functions to always generate one-liners? Or should we add the margin as an (optional?) argument to override the default 78? 🤔

@jmid
Copy link
Collaborator

jmid commented Aug 23, 2023

First of all, let me state that print_triple_vertical is an ugly piece of code that I'm not proud of! 😅
(I don't know whether it can be written more cleanly with Formatters).
I agree that it assumes 1-line cmd strings when stitching them together.

Code aside, too long outputs (strings, bytes, lists, ...) mangle the upside down Y-shape printed, which motivated the request for abbreviating print functions in #308

In CI logs, I'd rather skim over, e.g., an abbreviated string in an comprehendable Y-shape rather than see a long string in full detail. That suggests we should set the environment variable in the CI workflows.
I suspect other end-users would prefer such a default setup too 🤔

It would however be nice to be able to print the full string some way (locally and in the CI), say to create a standalone reproducer. That should however be possible by resetting the MCTUTILS_TRUNCATE environment variable.

To keep things nice and without wrapped lines in the former scenario, setting the formatter margin to max_int sounds reasonable.

There may be cases I'm overlooking, say record types with lists in them that could warrant multi-line formatted strings. So far, such types haven't occurred in the interfaces we have tested 🤷

shym added 6 commits August 23, 2023 17:45
If the environment variable `MCTUTILS_TRUNCATE` is set to a integer
greater than 0, `Util.Pp.to_show` will truncate strings that would have
been larger than that with the message "... (truncated)"
When `MCTUTILS_TRUNCATE` is less than the length of that message, it
will be set to the length of that message - 1
Truncated strings will be of length `MCTUTILS_TRUNCATE`+1

Ensure that a global box is created when using `to_show`, as `Format`
documentation says it is required
Bring `Util.Pp` in line with the code generated by `deriving show` by
opening and closing boxes and placing break hints at the same positions
Add a generic pretty-printer for tuples, taking the tuple elements and
pretty-printers as a list
Add standard pretty-printers for tuples of size 2 to 10
@jmid
Copy link
Collaborator

jmid commented Aug 25, 2023

CI summary for the latest run 64c1902

  • Cygwin trunk part 2 failed to trigger STM Sys test parallel
    before also timing out during a 253.7s Lin CList int64 test with Thread
    after a 2403.4s STM Sys test parallel, 1071.1s STM Weak test parallel, 3267.4s STM Weak HashSet test parallel, 5028.3s Lin ref int64 test with Thread and 1424.6s Lin CList int test with Thread [ocaml5-issue] General Cygwin slowness #384
  • Linux ppc64le failed with an STM Bytes test sequential segfault and an STM Float Array test sequential model difference [ocaml5-issue] Crashes and hangs on ppc64 trunk/5.2 #380

Neither of these are related to the PR, so I'll go ahead and merge

@jmid
Copy link
Collaborator

jmid commented Aug 25, 2023

Actually, it would be nice with a CHANGES entry to document changes in the user-facing Util.Pp module.
Could I convince you to add one @shym?

@shym
Copy link
Collaborator Author

shym commented Aug 25, 2023

Indeed!
Should I add also a default setting to MCTUTILS_TRUNCATE to 50 I’ve tested locally? As Util.Pp are used only for STM commands, it won’t bring the hoped truncation for the worse offenders (Dynlink, say) just yet, though.

@jmid
Copy link
Collaborator

jmid commented Aug 25, 2023

Should I add also a default setting to MCTUTILS_TRUNCATE to 50 I’ve tested locally?

Yep, please do! 🙏

@jmid
Copy link
Collaborator

jmid commented Aug 25, 2023

CI summary for 13d87f8

None of these are related to the PR, so I'll merge (and I mean it this time!)

@jmid jmid merged commit 448c059 into ocaml-multicore:main Aug 25, 2023
@shym shym deleted the pp branch August 28, 2023 08:26
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