-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add number of allocations to time/timed macros, fix realloc calculations #11186
Conversation
This seems like a reasonable way to do this for now, but it might be good in the future to refactor things so that there are fewer tiny functions to get info about allocations. Maybe a struct that can be returned. |
You are not counting pool allocations, which are 99% of the workload in most cases. It is not strictly a malloc call but I think people would expect it to be taken into account. I'm not sure that the number of free calls is very informative for the same reason. We could expose the number of freed objects instead but it requires a bit of addtional bookeeping in the pool sweeping. I mostly (perhaps cowardly) don't want people trying to second guess the gc heuristics and argue to change them. The number of allocation only depends on your code whereas the number of freed objects depends a lot on the gc's decisions. |
@StefanKarpinski agreed |
@StefanKarpinski Very good point, I was just following what was there before... refactoring would be nice... I wanted to refactor timed / time, but I'm not sure how (yet) with macros... more investigation! |
@carnaval I was just changing the places where allocd_bytes was being updated... I missed this pooled allocation? If you can point me where to fix it, I will, thanks! |
I think the number of allocation could use a little pretty printing since it is likely to get very large, as you will probably experience once you add the pool stuff. |
@carnaval I ended up splitting up the display of malloc/realloc/pool, they have very different performance characteristics (realloc may mean copying, etc.). Is there a nice function already to get "K","M","G" to display counts prettily? Thanks! |
I really think this is getting out of hand. It would be fine if we provide a way or getting this info for those who really need to know, but this is way too much info to display by default. |
@StefanKarpinski Adding the number of allocations was from @vtjnash 's suggestion on julia-users. |
I'm fine with the number of allocations, but separating out the number of reallocs is too much. And what is this "pool" value? Displaying it with slashes between means there's zero chance that someone can understand what's going on just from reading the output of |
So, would you have mallocs+reallocs, or mallocs+reallocs+pool, displayed as a single number in |
I don't know, but this is too much information. We should either display a multiline detailed output here that's clear and readable or introduce another mechanism for getting this information. I suspect that returning a struct from |
|
I made that comment at the point where you were also printing all the information. I do think that returning a structure might be more reasonable at this point. Returning that many values from a function is getting unwieldy. |
I agree with Stefan. I think a summary of total_number_of_alloc_events and total_size_allocated is sufficient. I don't think we have a facility for big number formatting in Base. I'm not convinced by the realloc argument. I'm not against providing it as a separate function but I think just having the summary is what people need. Remember that a lot of people are using |
Minor style nitpick but could you rename those counters to |
All good stuff everyone! I'm trying to get this done ASAP so I can get back to some "real" work 😀 |
Help! I must have messed things up with Git! This is only supposed to be 3 files! I updated it to remove a trailing space on one line... and now... 😢 |
Oooh. Got a merge. Nasty. The following is not recommended, but is also exactly what I'd do to fix.
|
@pao You are a real lifesaver! That looks like it did the trick, thanks! |
Any reason this cannot be merged in? I believe I've addressed all of the requests for changes. |
Anywhere that we have a struct in C and a type in Julia that need to be kept exactly in sync, it would be good to mark both with a comment pointing to the other - "If you change this, be sure to make the corresponding change in ____" |
OK, very good point! Will add that now... anything else? |
Whoops, didn't notice this at first, but speaking of keeping them in sync, is it just me or are the last 3 elements in different orders? |
@tkelman Thanks very much for catching that! I need more sleep or more caffeine before I commit stuff! |
Last few points, then I think you can squash and this is good to go :
|
OK, all good points. About making the struct immutable, doesn't that mean you'd always have to do an allocation, instead of just allocating 2 structs at the beginning of a loop (if you are reporting the differences in a loop)? I'm still learning all the ins and outs of optimizing Julia for mem allocations... |
If a type is immutable and known to the compiler without ambiguities (which is very likely to be the case here) it is passed around by copies and never uses dynamic memory allocation. The only case where we do allocate memory for such objects is when it must be passed to code which may not be aware of the type in advance and require boxing it. |
Note: when I compare this branch to Julia, it only shows my commits... |
The idea is you want just 7582922, yes? I suspect you had everything right until you tried to do a push, which complained and made you do a merge - the merge commit is where things get messy and you don't want to include those here. Try the following
you may need to use a different remote name than |
Thanks, is that correct now? I've put some examples on Gist: |
It's only one commit now which is good, you tell me if it has all the changes you wanted. One superficial thing is it looks like you've got some mixed tabs and spaces, we try to avoid tabs everywhere except in makefiles, 4-space indent is preferred for the C and Julia code in src and base. |
Sorry about the tabs... I keep resetting the "smart-tabs" variable in the editor, and writing out the state, but it's like a zombie, it keeps coming back! I did do 4-space indents everywhere (that's also what we came up with where I'm consulting) |
Do I need to squash this puppy again? 😀 |
yes |
The puppy is all squished! 🐶 |
Anyone else think And sorry to nitpick, but the couple times in the C code where you're using code like this
could use some newlines and indenting to fit with the code style elsewhere. |
@tkelman As I've said before, I greatly appreciate all constructive criticism. I'll change the formatting of the bug fixes with reallocs. I just noticed the bugs and fixed them quickly, and 35 years of the style I've become accustomed to came out of my fingers without thought! About the names, I'll change those |
Right, there have been a few conversations around this, and other than sometimes sort-of following some of the Python convention of underscore-oriented-programming (which some people understandably dislike), I don't think we have a great solution for this yet. At least the convention is generally towards extension via methods and dispatch rather than monkey-patching the way Ruby and Python code often does. Shipping hidden proprietary code written in Julia will probably have to be done by AOT compilation into shared libraries. Not sure whether there will be any better solutions but it's worth thinking about. |
I'm not concerned so much about shipping hidden proprietary code, but rather software engineering practices. I've seen too many times what happens when customers start playing around directly with your implementation, and then you can be locked into that implementation forever (even if it sucks!). |
OK, please look at this again... I changed the names to |
OK... any hope of getting it merged? 😀 Thanks for all the comments! |
I think this is good enough to merge for now, we can keep tweaking it on master. Could do with some tests eventually, for one thing (but I don't think we have any for these macros now, so this isn't really making things any worse). |
Add number of allocations to time/timed macros, fix realloc calculations
Thanks! I’m not sure how you’d test a macro like this... @timed maybe, where it returns the information instead of printing it... if anyone has a suggestion, I’ll certainly add testing... |
@ScottPJones The time seems to be printed twice for |
Yes, the first line is the pretty printed value, exactly the same as @time, after that, it prints out the raw information that it collects, not rounded or anything... |
We do still need docs for the new |
Ah! Yes, that slipped between the cracks... will do (must be that early onset Alzheimers!) |
This adds counting of the number of allocations to
@time
and@timed
(@timed
also now returns the number offree
s, andrealloc
s).It also fixes a bug that happens if you use realloc to shrink the size of something, it would subtract from allocd_bytes, instead of adding to freed_bytes.