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

Single, more efficient context type #10

Open
christopher-dG opened this issue Jan 26, 2020 · 0 comments
Open

Single, more efficient context type #10

christopher-dG opened this issue Jan 26, 2020 · 0 comments
Labels
Function: mock Problems with the mock function. performance MOAAAAAR SPEEEEEEED

Comments

@christopher-dG
Copy link
Member

This has no effect on user API, but I think we can make things faster.

Currently, we abuse @eval to create a new Context type and implement a bunch of overdub methods every time you run mock (unless you specify otherwise). It's really slow because of all that compilation.

Instead of creating new types and implementing overdubs for that type, we could try something like this:

@context Ctx  # The one context for ALL mocks

struct Metadata{Fs}  # Fs is the union of function types that will be mocked
    mocks::Dict

    Metadata(mocks...) =  # Just like the current type: Metadata((f, sig) => mock, ...)
        new{Union{map(p -> typeof(first(first(p))), mocks)...}}(Dict(mocks))
end

# This one overdub method catches all the mocked methods
# for the particular metadata instance.
Cassette.overdub(ctx::Ctx{Metadata{Fs}}, f::F, args...) where {Fs, F <: Fs} =
    ctx.metadata.mocks[f](args...)

Filters would be the same as before, but the specific method mocks (e.g. mock((+, Int, Int))) will be more complicated (previously the methods we implemented were only for the specified signature, so dispatch took care of it for us), as well as keyword arguments. I think for kwargs we would still have to eval new methods because Cassette can't handle the general kwftype trick.

@christopher-dG christopher-dG added Function: mock Problems with the mock function. performance MOAAAAAR SPEEEEEEED labels Jan 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Function: mock Problems with the mock function. performance MOAAAAAR SPEEEEEEED
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant