Repository navigation
Conversation
|
@hadley, @lionel-, and/or @pmur002, if you could have a look at this, and maybe do a review of the code it would be much appreciated... This is my first time writing C-code so my style may be weird - just let me know... I'll begin to work on a test suite, but as is ggplot2 works with the new spec (pending tidyverse/ggplot2#3034), which is a very good start |
|
@thomasp85 Could you cancel all whitespace changes please? This will facilitate the review. |
|
oh, sure... RStudio must have done something automatically... |
|
should be clean from ws changes now 😄 |
hadley
left a comment
There was a problem hiding this comment.
I gave it a once over at a fairly high level.
lionel-
left a comment
There was a problem hiding this comment.
Mostly styling comments:
-
It seems the grid package uses camelCase rather than snake_case.
-
Some if/else branches are not braced.
-
I think effectful expressions should be on their own line rather than hidden behind if conditions:
# Easy to miss: if (long && condition && stuff) stop("foo") for (x in y) { if (long && condition && stuff) a <- b } # Clearer: if (long && condition && stuff) { stop("foo") } for (x in y) { if (long && condition && stuff) { a <- b } }
-
Control flow can be hard to read at times, for instance in
Summary.unit(). Some patterns can help:-
Switching over enums is faster and gives more structure to the control flow (whereas if-else branch conditions might be different so the reader needs to double check).
-
Extract loops in functions.
-
Avoid nested if-else branches by extracting nested branches in functions.
-
Refactor if-else branches so that each branch exits the function (either by error or by returned value). Then you can remove the
elseand give some breathing room to the branches. This also reassures the reader that all branches play a similar role in the function, which is very helpful to understand control flow.
-
|
Thanks. I can build this ok, but a couple of tests show up some problems. To test, I am generating unit and grob objects in "old" (current release) R ... ... and then trying to load them and print/draw them in new-unit build of R, which gives me a fail for the "unit.list" object ... ... and bad things for all of the grobs ... Do you have time to take a look at those ? |
|
Thanks for the test problems... I thought I had tested these situations but I may have messed up somewhere... Will fix The |
|
Maybe don't export unitType() for now. I would like some evidence that people are using it for a good reason before officially letting that cat out of the bag. |
…tly don't understand why
This PR includes a new unit structure intended to simplify the unit specification as well as making it substantially faster. The reason for this is that unit operation has been identified as a recurring bottleneck in working with grid, and any improvements to this will have broad effects on grid performance.
The old unit spec was essentially covering three different structures that could be used interchangeably. The basic unit (what you get from the constructor), was an adorned numeric vector including attributes for the unit type, and potential unit data. A second type was the unit.arithmetic which was sort of an AST, containing the arithmetic function and the arguments. Lastly, a unit-list type was also provided that could contain scalar versions of the other two unit specs.
The new spec is greatly simplified and contain only a single structure, mostly resembling the unit.list. The structure is that of a list containing scalar units encoded as a list of three elements. The first element is a numeric given the value of the unit. The second element is any potential data related to the unit. The third element is the unit, given as an integer identifier (what was stored in the
valid.unitattribute in the old setup). Arithmetic is supported by adding three new units:sum,min, andmax. The data of these units is itself a unit vector.+and-operations are supported by converting to a sum representation:u1 + u2 -> sum(u1, u2)andu1 - u2 -> sum(u1, -u2). If possible, arithmetic operations simplify the result by inspecting the unit and data for equivalence and simply operating on the value if possible:1cm + 5cm -> 6cminstead of1cm + 5cm -> sum(1cm, 5cm)Most unit operations have further been moved to C code, and more may follow (e.g. data validation)
The speed-ups are significant in almost all regards:
some common operations:

absolute.units are mostly used when resolving viewport dimensions

vectorised arithmetic is much improved

The only performance regression is when adding and subtracting units, but this is simply because the old spec didn't perform any operations and simply returned a pseudo AST. Further operations on the result would provoke an evaluation of some kind and remove the performance lead
The new structure has been implemented without any user-facing changes, but users relying on the underlying structure of the old implementation will of course get errors (relying on that was dangerous in the first place due to the triple nature of units in the old setup).
as.numericandas.vectormethods has been provided to coerce the new structure into a numeric vector, but not all functions may call this explicitly but simply rely on automatic dropping of attributes (e.g.c(4, some_unit, 2)will no longer return a numeric vector)