# Optimize mozilla-firefox for high performance

## Mike Hunt

Here firefox starts in 2-3 seconds after reboot, otherwise ~1 second on a athlon 2.0Ghz system.

The current version is mozilla-firefox-3.5.2, and this is how we did it:

We use LDFLAGS -Bdirect for direct ld binding - it's like prelink but without prelinking.

We use LDFLAGS -Wl,-z,now instead of the default ld -Wl,-z,lazy ld binding.

We compile xulrunner with -O2 and USE custom-optimization

We compile mozilla-firefox with -Os and USE custom-optimization

```
CFLAGS="-march=native -Os -pipe -fomit-frame-pointer"

CXXFLAGS="${CFLAGS}"

LDFLAGS="${LDFLAGS} -Bdirect -Wl,-z,now"
```

```
CFLAGS="-march=native -O2 -pipe -fomit-frame-pointer"

CXXFLAGS="${CFLAGS}"

LDFLAGS="${LDFLAGS} -Bdirect -Wl,-z,now"
```

```
net-libs/xulrunner custom-optimization

www-client/mozilla-firefox custom-optimization
```

And run the whole thing through squid as a caching proxy server - the default squid.conf works fine without changes.

```
emerge squid

/etc/init.d/squid start

rc-update add squid default
```

Setup firefox -> Edit -> Preferences -> Advanced -> Network -> Settings -> Manual proxy configuration -> 

```
HTTP Proxy: <FQDN> 

Port: 3128 

No Proxy for: localhost, 127.0.0.1
```

And add iptables rule to iptables.conf

```
iptables -A INPUT -p tcp --dport 3128 -m state --state NEW -j ACCEPT
```

This way firefox starts up fast, pages load fast, it stays fast over time, there is no maintenance required, all this is persistent through upgrades, and it saves bandwidth too.  :Smile: 

----------

## erik258

Neat, thanks for posting.

----------

## Mike Hunt

You are welcome.  :Smile: 

I thought it might be of interest for people who want a very responsive firefox.

----------

## Black

In my case, it had the opposite effect - Firefox takes much longer to start up.

----------

## Mike Hunt

Really, wow. I'm not sure why that would be.

How much longer?

How much RAM is on your box?

----------

## Black

I haven't timed it, but it looked more like 1 second before, and 2-3 seconds after. I've got 2GB RAM, ACCEPT_KEYWORD="amd64".

Anyway, in my case, it doesn't matter, startup time was never an issue.

----------

## Mike Hunt

I reverted back to the before way, recompiling xulrunner and firefox with -O2 -pipe and no LDFLAGS and no proxy server.

Firefox starts slower and loads pages slower.

So I recompiled them again with optimizations turned on, and with squid.

Firefox starts faster and loads pages faster. It is snappier and an overall performance boost on my box. 

One would expect in the worst case, no change at all.  But I am at a loss to explain worse performance.

----------

## M

I will definitely try this these days, although I don't care much about speed of starting firefox (I can wait those extra seconds once a day) but that faster page loading sounds nice, I hope it's not just because of squid. I noticed before that there is a difference in speed between official binaries and compiled version, and that swiftfox seemed faster too.

Also, a few quick tips for optimization that I use is to occasionally vacuum sqlite files in mozilla profile. I also mounted firefox profile in ram, I can dedicate 64mb for firefox, and only 25 is used actually since I removed urlclassifier, there was a thread about this, and with this location bar is much snapier.

----------

## zyko

 *Quote:*   

> We use LDFLAGS -Bdirect for direct ld binding - it's like prelink but without prelinking.
> 
> We use LDFLAGS -Wl,-z,now instead of the default ld -Wl,-z,lazy ld binding.
> 
> We compile xulrunner with -O2 and USE custom-optimization
> ...

 

I haven't tried it yet, but would you please elaborate a little: Why is this supposed to improve performance versus the defaults?

----------

## Mike Hunt

A few years ago, this would have been ricing. But not anymore. The devs both at Gentoo and upstream have been working very hard on implementing these (and other) linker directives into our open source software. The result is that it can make applications respond noticeably snappier/faster - at least on my box. But apparently this is not always true - see above posts. 

So if you wish, you can try - it cannot do any harm and is easily reversible.

 *Quote:*   

> ld -z now
> 
>     Equivalent to gcc -Wl,-z,now (which is probably preferrable, since I write C++ and always use the g++ frontend), this tells the dynamic linker to resolve symbols on program start instead of on first use. I’ve noticed that sometimes, the first run through a particular code path results in a latency of 800 milliseconds. That’s huge; that’s big enough for a human to notice with the naked eye. For server processes, that generally also means that the first request serviced on startup blows chunks, latency-wise. While it may not make a huge difference in the grand scheme of things, it’s nice to be able to pay the cost of symbol lookup up front instead of at first use. 

 

 *Quote:*   

> By passing -Bdirect at link time, the build process can cause many symbols to be directly linked, allowing the dynamic linker to severely decrease the search space during lookup. 

 

```
# quse -D custom-optimization

 local:custom-optimization:net-libs/xulrunner: Fine-tune custom compiler optimizations

 local:custom-optimization:www-client/mozilla-firefox: Fine-tune custom compiler optimizations
```

Of course, this will not make a celeron-750MHz load firefox in 2 seconds. Clock speed is one thing, fine-tuning is another thing.  At any rate, I like it enough that I thought I would share it.  :Smile: 

----------

