Skip to content

Compare unnamed environments by value - #133

Merged
hadley merged 5 commits into
mainfrom
env-by-value
Mar 10, 2022
Merged

hadley merged 5 commits into
mainfrom
env-by-value

Conversation

@hadley

@hadley hadley commented Mar 10, 2022

Copy link
Copy Markdown
Member

Fixes #127. Fixes #117.

Comment thread R/compare-opts.R Resolved
Comment thread NEWS.md Resolved
@hadley

hadley commented Mar 10, 2022

Copy link
Copy Markdown
Member Author

This does resolve #133, but it's veeeeeeeeeery slow:

library(survival)

set.seed(1234)
fit1 <- party::ctree(Surv(time, status) ~ age + ph.ecog, data = lung)

set.seed(1234)
fit2 <- party::ctree(Surv(time, status) ~ age + ph.ecog, data = lung)

bench::system_time(waldo::compare(fit1, fit2))
#> process    real 
#>    8.5s   8.51s

Created on 2022-03-10 by the reprex package (v2.0.1)

This makes me concerned that there's risk of other major performance regressions elsewhere.

@hadley

hadley commented Mar 10, 2022 •

Copy link
Copy Markdown
Member Author

Oh that was because I wasn't using a mutable cache of environments, so in list(x = env, y = env, z = env) all environments would be compared by value, not just the first. That brings the timing above down to 185ms; that's still slower than the current CRAN version which is ~60ms, but not extraordinarily so.

Comment thread R/utils.R Resolved
@hadley

hadley commented Mar 10, 2022

Copy link
Copy Markdown
Member Author

I think I've realised the general principle: you never want to see the comparison between a given pair of environments more than once. So this:

e1 <- new.env()
e1$x <- 1
e2 <- new.env()
e2$x <- 2

waldo::compare(list(e1, e1, e1), list(e2, e2, e2))

Should only report the different once.

@hadley
hadley merged commit 51c2158 into main Mar 10, 2022
@hadley

hadley commented Mar 10, 2022

Copy link
Copy Markdown
Member Author

I'm feeling good about this so I've merged it. The next step is to run the testthat revdeps to see if this (or any of the other changes) have introduced any new problems. It's likely to take me a few days to work through any failures, so please let me know if you have any general concerns about this PR, and I'll make sure to hold off on release until they're resolved.

@hadley
hadley deleted the env-by-value branch March 10, 2022 21:59
@MichaelChirico

Copy link
Copy Markdown
Contributor

thanks! I've watched the repo -- if you file issues about revdeps & I've got some spare cycles I can try and help resolve what comes out.

I'll also try running this on our internal CI to see what comes out.

@AshesITR

AshesITR commented Mar 11, 2022 •

Copy link
Copy Markdown

A mutable cache may break cases such as this:

e1 <- new.env()
e2 <- new.env()
e3 <- new.env()
e4 <- new.env()
e3$x <- 1
e4$x <- 2

e1$a <- e3
e2$a <- e3
e1$b <- e4
e2$b <- e4
e1$c <- e3
e2$c <- e4

compare(e1, e2)
# should find e1$c$x != e2$c$x

The current implementation is fine because is_seen(x, envs) actually checks env pairs and not individual envs, but it might be worth adding a test case to prevent things from breaking in the future.

@hadley

hadley commented Mar 11, 2022

Copy link
Copy Markdown
Member Author

@AshesITR I'm pretty the logic using pairs prevents that problem, and https://github.com/r-lib/waldo/pull/133/files#diff-4cf1913ccfc0a40d0281afb87562e1e3cd210cb9e34f0311d0cc96425f4230f2R626 tests that we really are caching the pairs, not individual environments.

@hadley

hadley commented Mar 11, 2022 •

Copy link
Copy Markdown
Member Author

@MichaelChirico the revdeps look good. There were only 10 problems, mostly caused by #134. I think the only case related to comparing environments by value is #135, which I'm pretty sure will be easy to resolve. httr2 also fails but only because of some bug in CRAN waldo that allowed testthat::expect_equal(as.POSIXct("2016-07-18 16:06:06"), as.POSIXct("2016-07-18 16:06:00")) to succeed. (Oh hmmm probably due to numeric tolerance)

@AshesITR

Copy link
Copy Markdown

@hadley sorry I missed the test case that already covered it.
I'm also convinced the current implementation is correct, just wanted to make sure it's tested.

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.

Documentation request: how to compare environments? shiny::reactiveValues() causes infinite loop

3 participants