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

Tell users that function barriers can help with dynamic properties performance #18

Open
Tortar opened this issue Aug 26, 2024 · 4 comments

Comments

@Tortar
Copy link
Collaborator

Tortar commented Aug 26, 2024

While this being a general performance tip, I think that emphasizing this in the ReadMe could be beneficial. Consider:

julia> using DynamicStructs

julia> @dynamic struct A end

julia> a = A(x = [1]);

julia> b = A(x = [2]);

julia> f(a, b) = a.x .+ a.y;

julia> using BenchmarkTools

julia> f(a, b) = a.x .+ a.x
f (generic function with 1 method)

julia> @benchmark f($a, $b)
BenchmarkTools.Trial: 10000 samples with 984 evaluations.
 Range (min  max):   56.815 ns  198.130 μs  ┊ GC (min  max):  0.00%  99.92%
 Time  (median):      99.324 ns               ┊ GC (median):     0.00%
 Time  (mean ± σ):   111.972 ns ±   1.981 μs  ┊ GC (mean ± σ):  18.85% ±  2.37%

                                        ▄█▂▁                     
  ▂▃██▅▄▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▁▁▁▁▁▁▁▁▂▅▇████▅▄▄▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▃
  56.8 ns          Histogram: frequency by time          123 ns <

 Memory estimate: 96 bytes, allocs estimate: 2.

julia> f(a, b) = g(a.x, b.x);

julia> g(x1, x2) = x1 .+ x2;

julia> @benchmark f($a, $b)
BenchmarkTools.Trial: 10000 samples with 993 evaluations.
 Range (min  max):  34.486 ns  431.558 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     63.524 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   63.087 ns ±   8.535 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

                                           ▇ █▂                 
  ▂▂▂▃▂▂▂▂▂▂▂▂▂▂▂▁▂▂▁▁▁▁▁▁▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▂▅█▄██▃▄▃▃▃▃▂▂▂▂▂▂▂▂▂ ▃
  34.5 ns         Histogram: frequency by time         74.4 ns <

 Memory estimate: 64 bytes, allocs estimate: 1.
@AntonOresten
Copy link
Owner

AntonOresten commented Aug 26, 2024

Ah, smart! I had a hunch something like this could help.

However, is it possible that in this case the first one is slower because Julia calls Broadcast.broadcastable on each argument, whereas the second one can compile more effectively because it dispatches on known types, and knows that two Vector{Int}s get broadcasted in a certain way? I could not reproduce this with Int types instead of Vector{Int}, which makes sense since + is just another function, which serves as a barrier, I guess?

Regardless, it is worth noting this type of optimization.

EDIT: Actually nevermind! this was the point you were proving!🤣

@Tortar
Copy link
Collaborator Author

Tortar commented Aug 27, 2024

EDIT: Actually nevermind! this was the point you were proving!🤣

yes, I think so :-)

Could I ask you if you can give me some privileges on the repo, because I think that I could probably maintain it for a fair amount of time? I won't do any changes without first discussing them with you anyway, it just would be cool :D

@AntonOresten
Copy link
Owner

Sure thing! You've had a lot of good ideas, and I'm happy to cut out the middleman.😋I've added you as a collaborator. You might need to check your email.

@Tortar
Copy link
Collaborator Author

Tortar commented Aug 28, 2024

Thanks a lot!

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

No branches or pull requests

2 participants