After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 306801 - gdk-pixbuf-io.c should use mmap rather than fread
gdk-pixbuf-io.c should use mmap rather than fread
Status: RESOLVED NOTABUG
Product: gdk-pixbuf
Classification: Platform
Component: general
git master
Other All
: Normal enhancement
: ---
Assigned To: gtk-bugs
gtk-bugs
Depends on: 148218
Blocks:
 
 
Reported: 2005-06-07 18:29 UTC by Philip Van Hoof
Modified: 2010-07-10 04:08 UTC
See Also:
GNOME target: ---
GNOME version: Unversioned Enhancement


Attachments
Patch that will replace the fread stuff with mmap (3.30 KB, patch)
2005-06-07 18:31 UTC, Philip Van Hoof
none Details | Review
Forget the prev one. This one is better (3.13 KB, patch)
2005-06-07 19:06 UTC, Philip Van Hoof
none Details | Review
No no! this one! (3.06 KB, patch)
2005-06-07 19:18 UTC, Philip Van Hoof
needs-work Details | Review

Description Philip Van Hoof 2005-06-07 18:29:19 UTC
As Robert Love told us on GUADEC :-).

Will attach diff that does this . . .
Comment 1 Philip Van Hoof 2005-06-07 18:31:12 UTC
Created attachment 47397 [details] [review]
Patch that will replace the fread stuff with mmap

It has some ifdefs. Thats because I'm not sure whether mmap is supported on all
platforms. Feel free to remove those ifdefs if mmap actually is supported on
all platforms on which Gtk+ currently runs.
Comment 2 Federico Mena Quintero 2005-06-07 18:43:35 UTC
See also bug #148218, which is about an mmap() wrapper for glib.
Comment 3 Federico Mena Quintero 2005-06-07 18:45:03 UTC
It would be nice to have some timings before/after this patch gets applied. 
Probably both with cold and warm caches.
Comment 4 Philip Van Hoof 2005-06-07 18:45:56 UTC
Wait, creating new diff with some improvements (munmap and check on HAVE_MMAP
rather than on sys/mman.h)
Comment 5 Philip Van Hoof 2005-06-07 18:47:32 UTC
Federico: About bug #148218: gtkiconcache.c is also usind dirty ifdef-tricks to
overcome the mmap-on-win32 issue. So also gtkiconcache.c should be fixed once
this mmap-glib wrapper is in (is it in?).
Comment 6 Philip Van Hoof 2005-06-07 19:06:03 UTC
Created attachment 47399 [details] [review]
Forget the prev one. This one is better
Comment 7 Philip Van Hoof 2005-06-07 19:18:18 UTC
Created attachment 47400 [details] [review]
No no! this one!

I had some conditional/unconditional stuff wrong (the fdin variable).
Comment 8 Federico Mena Quintero 2005-06-07 19:32:15 UTC
Loaders have two ways of loading images:

1. if they implement the load() method, then gdk-pixbuf-io.c feeds them a FILE
*.  Loaders which have a load() method should use mmap() themselves.

2. with begin_load() / load_increment().  This is what gdk-pixbuf-io.c uses if
the loader does not implement load().  Then, the loader gets fed chunks of
memory.  In this case, gdk-pixbuf-io.c should use mmap() itself.

Note that it is done this way for historical reasons.  Originally loaders had
both load() for synchronous operation and begin_load()/load_increment() for
incremental operation.  Then, we saw that load() could be made into a wrapper
for incremental loading, and loader modules could just implement *that* instead
of both variants.
Comment 9 Philip Van Hoof 2005-06-07 19:42:20 UTC
I noticed that gdk_pixbuf_new_from_file_at_scale could use some mmap-love. Other
than that (which would fix this concern), I question/wonder: Is using 4069 bytes
on the stack a good idea? the at_scale method is doing that :-\.
Comment 10 Matthias Clasen 2005-06-07 21:00:59 UTC
Re Comment #8: some image libraries only offer a FILE * based api (libtiff comes
to mind), and thus can only support load(), not the incremental api.
Comment 11 Federico Mena Quintero 2005-06-07 22:59:24 UTC
About comment #10 - in that case, those libraries lose :)  If anyone cares about
them, they can optimize them directly.  There's not much we can do for them at
the gdk-pixbuf level.
Comment 12 Soren Sandmann Pedersen 2005-06-08 14:31:14 UTC
Maybe I am just stupid, but why do people think that mapping the image files
will buy us anything substantial? They still have to be uncompressed.

If we could get the files stored uncompressed on disk (or compressed in memory),
then we would be getting somewhere.
Comment 13 Behdad Esfahbod 2005-06-08 16:09:54 UTC
comment 12:  You don't have to copy the data two times:  From kernel buffer to
stream buffer and from there to user buffer.  Moreover, you share the memory
with other processes.
Comment 14 Soren Sandmann Pedersen 2005-06-08 17:12:23 UTC
When you uncompress the files, you are copying into malloc()ed memory that is
not mapped into other applications, so the data ends up not being shared.

I am willing to believe that you save copying the data from memory to memory
once, though. 
Comment 15 Federico Mena Quintero 2005-06-08 17:20:20 UTC
We can't really know until we time it.

However, I'd say that the main slowdown is slow image libraries.  Compare
libjpeg's loading speed to Photoshop's, and cry.
Comment 16 Soren Sandmann Pedersen 2005-06-08 17:50:12 UTC
The point is, what Robert Love said at GUADEC does not apply here. We are at
best saving a little bit of memcpy() on something that doesn't show up on
profiles ...
Comment 17 Behdad Esfahbod 2005-06-08 18:11:38 UTC
Right, but we talked about this extensively with Robert Love yesterday on IRC,
and he said mmap()ing doesn't harm anyway.  But as Paul said, there are other
places in gdk-pixbuf-io.c that can use mmap.  So we can wait for a while to see
more patches.
Comment 18 Matthias Clasen 2005-06-08 20:39:43 UTC
sure, mmap doesn't hurt, but s/fread/mmap/g is not the answer. The two places
where mmap will make a really big difference (at least potentially) are mmap
caches for fontconfig and gconf
Comment 19 Behdad Esfahbod 2005-06-08 20:42:13 UTC
Do you mean we should /not/ mmap smaller files?

fontconfig will be fixed by the end of Summer, thanks to Google bounties :).
I'll have a look at gconf.
Comment 20 Manish Singh 2005-06-08 20:55:40 UTC
Making the code more complex and harder to read for nano performance gains isn't
really worth it, in my opinion.
Comment 21 Behdad Esfahbod 2005-06-08 21:25:10 UTC
Then I suggest we finish the api in bug 148218 and export a clean simple API (no
structs involved preferably) for opening files readonly, and use it everywhere
unconditionally.  Is that ok?
Comment 22 Philip Van Hoof 2005-06-09 08:57:45 UTC
Comment #19: GConf uses libXML for it's default backend. So what can be done is
to mmap the XML-file, parse that mmap'd buffer with xmlParseMemory, etcetera. 

Looks trivial:

backends/xml-dir.c:1380

if (!g_file_get_contents (filename,
                            &text,
                            &length,
                            err))
    return NULL;
doc = xmlParseMemory (text, length);
Comment 23 Philip Van Hoof 2005-06-09 09:35:29 UTC
Hey Behad (Comment #21 and #22), I created a small testcase for xml-dir.c at Bug
#307001
Comment 24 Matthias Clasen 2005-06-09 14:14:24 UTC
No, thats a misunderstanding. The aim for a mmap gconf cache has to be
to 

a) avoid all the parsing. The cache should contain the data in a ready-to-use
  format

b) avoid opening a bazillion small files. There should be a single cache file
Comment 25 Philip Van Hoof 2005-06-09 17:08:38 UTC
Comment #24: So writing the MarkupTree to a file? I don't think it can be done
easily since the memory of that tree isn't continuous (a parent has a doubly
linked list with allocated childs). What might be possible would be to write all
the MarkupEntries to a file, when reloading mmap that file read/write and
reconstruct the MarkupTree from that. I think the MarkupTree will need an 'id'
member. And I think another file could keep an index of those ids (how they form
the tree). Or each MarkupTree will need a char path[1024].

What you think Behdad? Sounds like a fun experiment :-)! I'm interested to try this.
Comment 26 Philip Van Hoof 2005-06-09 17:22:06 UTC
Comment #25 and #24: However ... it will basically mean that we'll be building a
database engine into gconf. So perhaps it's a better solution to just use an
existing fast database implementation (sqlite, etcetera?) ... fontconfig will
probably have the same problem.
Comment 27 Matthias Clasen 2005-06-09 17:46:00 UTC
You can look at the icon theme cache to get an idea what such a mmap cache could
look like.
Comment 28 Manish Singh 2005-06-09 18:19:07 UTC
fontconfig has pregenerated caches, so I don't see how a database is really a
win there.

For gconf it makes sense, but not something SQL based...
Comment 29 Owen Taylor 2005-07-26 22:34:06 UTC
Ignoring the bug completely driving off the GTK+ road and returning to the
original subject: what would need to be done to convince us to apply something
like the original proposal (of using mmap inside gdk-pixbuf) is to do
some measurements indicating that it makes a difference.
Comment 30 Philip Van Hoof 2009-01-06 22:50:31 UTC
Reading the comments and looking at the code myself I don't think it'll make a big difference. Mostly the fact that the images need to be decompressed anyway kinda makes mmapping them pointless. An mmap of the rgb pixmap would be more interesting.

Feel free to mark as wontfix or obsolete.
Comment 31 Behdad Esfahbod 2009-01-06 23:04:50 UTC
Agreed.