This blog has moved, Update your bookmarks.

Stefan Rusek | Managed Sqlite

Managed Sqlite

October 24, 2005 @ 9:22 am

Last week I thought it would be interesting to try to
compile Sqlite in Managed C++. That way I could have a completely managed
sqlite provider, instead of having to have a managed wrapper for it. In the
process, I discovered that it was going to be more work, than I wanted to put
into it right now.

However, I did notice a place were efficiency could be
improved in sqlite. It treats the sqlite database file, as a sequence of fixed
size pages. It tracks dirty pages, caches them, reads and writes them to the
file. Because of the block nature of the database, Windows IO, and Windows VMS,
the sqlite db engine really ought to use memory mapped files. The OS’s
own disk IO system is mapping the files and copying them into the buffers used
for reading and writing, so the files is getting mapped anyway.

Sqlite already has a very small memory footprint, but by
using memory mapped files, the memory would be further reduced. Since Windows
already has the db file mapped, many of the page buffers would no longer have
to be allocated. In multiple processes where sqlite is running, if the same
pages are mapped, then those buffers are shared by the OS, so even less memory
would be used.

Another issue that Rico Mariani has routinely brought
up in his own blog, is that often it is better not cache data because if the
data gets paged to disk (which is likely in a paging OS like Windows or Linux),
then bringing that page back from the page file is as time consuming as
rereading it from the db file. So if you have a dirty page in the cache and it
had been paged out, then to write it to the db file, the OS is reading it from
the page file into memory and then writing it back to the disk. With memory
mapped files, when the OS pages out that dirty page, it is automatically
written where it belongs.

Another advantage of using memory mapped files is that you
can do page level locking instead of db level locking. Sqlite currently only
supports db level locking which could severely inhibit its scalability. With page
locking, reads and write could occur at the same time in differently pages, or
even multiple writes at the same time could be achieved if the writes were in
different pages.

–Stefan

Stefan Rusek | Custom Palette GIFs in c#

Custom Palette GIFs in c#

October 20, 2005 @ 5:55 pm

Today, I spent some time investigating a common problem that
comes up over and over. Most of the time image manipulation in .net involves working
with 24 or 32 bit images, but when you go to save one of these images as a GIF
file you run into problems because the GIF codec used by GDI+ is not smart
enough to create a palette for the image you are saving, it just uses its
default. This is especially annoying when you purposely use less than 256
colors, and they end up being dithered because they aren’t in the default
palette.

If you search on Google, you can find some sample code from
Microsoft that works.

http://support.microsoft.com/kb/319061/EN-US/

DO NOT USE THE TECHNIQUE THAT MS SUGGESTS IN THE ARTICLE. You
would expect that the company that wrote the classes would be able to tell you
how to properly use them, but here they don’t.

After some experimenting with the code provided and using
Reflector to decompile the classes involved, I have figured out how to create a
custom palette properly. In the article they suggest that you should create a
temporary bitmap and “steal” its palette. For starters the very
idea is a waste of resources, and second, this just seems stupid.

When you access the Image.Palette property you get a
ColorPalette object, and you expect that if you modified this object, you would
update the palette of the image, but this is not the case. Every time you get
the property, the Image object creates a new ColorPalette object. So the
following code does nothing!

for (int x = 0; x < 256; x++)

            img.Palette.Entries[x]
= Color.FromArgb(255, x,x,x);

In fact the code above will create 256 ColorPalette objects,
each with one color modified, and then toss them all to the garbage collector. GRRR!
Once we know that it creates a new ColorPalette object, we don’t have to “steal”
anything, and we can write the correct version.

ColorPalette pal = img.Palette;

for (int x = 0; x < 256; x++)

            pal.Entries[x]
= Color.FromArgb(255, x,x,x);

img.Palette = pal;

Unfortunately, it is not the most intuitive design, but it
is simple to use. Many hours of wasted programming would have been saved had Microsoft
usability tested this one property, but alas they didn’t. I hope that
someone else can find this useful and avoid wasting hours of productivity.

–Stefan