xapi-project / xen-api

The Xapi Project's XenAPI Server
http://xenproject.org/developers/teams/xapi.html
Other
342 stars 282 forks source link

IH-633: Transition away from exception-raising Hashtbl.find and Unix.getenv #5751

Closed last-genius closed 9 hours ago

last-genius commented 1 week ago

This PR starts the transition away from exception-raising OCaml's stdlib functions Unix.getenv and Hashtbl.find in favour of Sys.getenv_opt and Hashtbl.find_opt respectively.

This is beneficial because:

A naive benchmark shows the benefit:

┌───────────────────────────────────────────────┬──────────┐
│ Name                                          │ Time/Run │
├───────────────────────────────────────────────┼──────────┤
│ id                                            │   0.93ns │
│ Option.value (Hashtbl.find_opt) ~default      │  13.58ns │
│ Hashtbl.mem then find                         │  27.05ns │
└───────────────────────────────────────────────┴──────────┘

===========

This PR is structured in the following way:

Even though the PR is relatively large, most changes are trivial and fall into several classes:

===========

These changes passed the BST+BVT test suites.

last-genius commented 1 week ago

Due to the size of this PR, I will be pushing fixup commits to ease the review process. Will squash these at the end.

last-genius commented 1 week ago

~Pedantically, there is a case where you can η-reduce (fun s -> Stunnel.disconnect s).~ - nevermind, we shouldn't be making unrelated changes here (I guess do them if you want so long as they are obviously correct and won't impair review?).

Nice catch! I've been trying to keep this PR to changes that do not change the logic of code -- this one does not, however, a simple grep indicates there are at least 50 such cases in the codebase!

$ unbuffer rg 'fun\s+(\w+)\s+->\s+\w+\s+\1\)' --pcre2 --no-heading | tee >(wc -l)
[....]
50

Might just as well put this in a separate ticket/PR - though there is little practical benefit, it gets boiled down to the same code

Also, if you want to be consistent with using Option.map and friends to outline casing behaviour, there are a few cases where fold could be used, e.g.:

I myself find that the parseability of match is better than that of Option.fold -- perhaps it's just a question of style and preference. match is not so nice when one of the arms is heavy and the other is empty, so that's why I've used .map and .iter to immediately indicate the intention without having to search for the other arm.

contificate commented 1 week ago

Might just as well put this in a separate ticket/PR - though there is little practical benefit, it gets boiled down to the same code

Indeed, it's only a stylistic concern; this case is a minor one but there are areas of the codebase where eta-reduction could be applied several times, which cleans things up a lot, visually (typical examples would be with List.fold_left). It's one of the more pedantic style changes, but it's generally accepted as good practice (so long as you don't get carried away with point-free style and get caught out by the value restriction).

last-genius commented 6 days ago

Had to rebase on top of master to get the Clock module, so hashes have changed. Otherwise review fixes are still in separate fixup commits.

coveralls commented 6 days ago

Coverage Status

coverage: 44.887%. remained the same when pulling dd2718061588df3e65f0a6fa69b8452e33f7b2d8 on last-genius:private/asultanov/opt-refactoring into ee8e80032f17c97e0f2aa4952a531b81a5c0ce94 on xapi-project:master.

lindig commented 4 days ago

Has your comment being addressed, @psafont? Please revisit this such that we can merge this.

psafont commented 4 days ago

Please squash the commits

last-genius commented 4 days ago

Squashed the fixes. Commits are still split into several as was suggested by Christian initially, so that we could revert only one if needed later (especially in the case of the more complex refactored files)