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
Custom Memory Allocator Methods #584
Comments
Questions:
Unfortunately, it isn't as simple as introducing a new TurboJPEG function. The TurboJPEG API is a wrapper for the libjpeg API, and the libjpeg API allocates memory in a bunch of different places. libjpeg originally shipped with different memory manager implementations for various platforms, but with the exception of the Any major new TurboJPEG API feature is best implemented as part of the TurboVNC v3 API overhaul (#517), which is an even more non-trivial effort. The other hell of it is that a global state-setting function such as you propose above wouldn't be appropriate, because the thread safety of that function could only be ensured on platforms that support thread-local storage. (That includes most of them these days, but not all, and I really don't want to make a major part of the TurboJPEG API dependent on TLS. One of the goals in the API overhaul is to eliminate global functions and make everything in the API stateful.) Thus, it would be possible to override the memory allocation/de-allocation functions on a per-instance basis, but |
Thank you for the quick and detailed response. It is super informative.
To answer your questions:
1. The project I am working on is a small part of a very large system with lots of memory allocations/frees happening simultaneously across several threads. We have seen that lots of small allocation/frees will leave holes in the memory manager when other allocations are happening concurrently (which I have no control over). This ends up with lots of small gaps (fragmentation) in the heap and also leads to slowness when the memory manager has to try coalesce all the tiny free blocks that are near each other. It takes a long time to get to this stage (on the order of an hour or more) which is an outlier for our usage as our process likely won’t be running for that long *most* of the time. However, we have seen this when doing stress testing and so are looking for ways to reduce memory churn. We have other avenues to explore as well, but Libjpeg-turbo is one of our dependencies that we don’t control memory management for and so I thought it would make sense to reach out. That being said........
1. Reusing handles DOES work and solved our problem. This was the first thing that I asked one of our developers to try. He said that he did it and would get crashes and so handles were not reusable. After getting your email that indicated the handles should be reusable, I made the code change myself and everything worked perfectly. He doesn’t know why he got a different result, but now that I’ve shown it can work we will go with a small pool of handles and so can withdraw my request.
Once again, thank you for your quick response and guidance. Your library is awesome. 😃
Would you like me to withdraw my request or add a comment on it? I’m new to Github (this was my first post ever) and so don’t know what the etiquette is here.
Thank you,
Gavin
|
Since your use case isn't common and you were able to work around it in a different way, I will close the feature request for now. For anyone stumbling upon this issue, I will note that:
|
Hello, is this still the case? In my use case, I want to pass a custom allocator (that would do an allocation in wasm memory) |
Yes. If you're using the libjpeg API, then you'll need to modify the library source code in order to use allocate/free methods other than |
Thanks for the reply. In my use case, I want to call the libjpeg API from native and pass an allocator that would allocate in WASM memory. Instead, what you were referring to seems more like compiling libjpeg to WASM. |
Your path of least resistance there would be to modify jmemnobs.c. |
Libjpeg-turbo uses malloc/free internally for its memory allocation. While large allocations can be avoided by providing pre-sized buffers, each call to tjInitCompress or tjInitDecompress does a small allocation (and then free when tjDestroy is called). When used in the same process over a long period of time, this constant churn of malloc/free can produce memory fragmentation.
My request is to create an API that allows a developer to set custom alloc/free methods (with the same signatures as malloc/free). This would allow a developer to use a memory pool or do other custom handling.
Potential interface:
The text was updated successfully, but these errors were encountered: