Firefox Slow Page Load – Solved

Firefox 3 slow? 20 second page load times? Figured out why. And how to fix it.

A co-worker showed me an interesting problem with Firefox today. He loaded a page from our application (running on localhost) and the page content loaded instantly, but the page load itself didn’t end until a time out 20 seconds later. Literally.

Everything we saw a measured from the browser or from the sending application showed that the content was sent in milliseconds, and the page load was just sitting there doing nothing. We were even using the latest Firefox beta.

Other browsers had no such problem.

Turns out, we figured out what was going on using the Tamper Data add-on.

Turns out there was a Connection: keep-alive in the header. When we changed it from keep-alive to close, the browser behaved as expected. That is, it loaded the page instantly.

A little web investigation showed that when you use the keep-alive attribute, you must also use Content-Length: header, which the sending application wasn’t doing.

A quick application tweak to send the content length, and everything ran super spiffy.

Now, if you don’t have access to the application that’s sending you web pages, you can twiddle with the about:config and change the network.http.keep-alive setting to false.

SQLite Functions

I was trying to figure out how to display an integer as a date and time in SQLite3. And it’s documented, and documented very, very well. Problem is, so is the code base, so when I looked for time conversion functions for SQLite, even browsing the Wiki, I kept getting the developer pages, not the SQL core functions. But, I found them. And now I’m documenting where so I can get to them at a later date.

Seems silly, but I’ve been using SQLite and was having the darnedest time try to convert an integer into a time. Problem was, I knew the functions for Sqlite3 had to be out there, but all I kept getting from search engines and exploring the developer Wiki were the C/C++ API functions, not the ones needed for SQL. I guess the way the pages are indexed, the source pages score higher than the user manual pages.

Anyhow, located the SQL functions and wanted to document their location for myself so I could locate them again later.

Seems they are called Core Functions.

They hide under the language expression page, which is under the SQL language page.

I was thrilled to find that the SQLite C Interface allowed the creation of your own functions. Brilliant!

Turns out, that the date and time functions were hiding in a different part on the Wiki.

The magic I wanted, given a time stored in an integer, was:
SELECT datetime(timevalue, 'unixepoch', 'localtime');

An Outsider’s View of Programming Documentation

Curious to how a non-technical person internalizes introductory technical material? I was.

Size Of Void StarAs a programmer, who works with programmers, writing programs that are primarily targeted for programmers, I find that I’m blessed with the ability to drop into a pseudo-language to communicate complex ideas concisely with my peers.

Occasionally, when I come up for air to deal with the rest of the world, I have to revert back to English. For me, the context switch is effortless, and occasionally I find myself using programmatic terms, like ‘context-switch’, in standard sentences.

The problem is, for a technical person, we don’t grasp what it’s like not to deal with technical information on a constant basis and then be abruptly exposed to it. Today I got to find out, and it was insightful. No wonder so may people fear their computers.

This morning, I was reviewing some basic documentation on Objective-C. Here’s the section (skim it):

Sending Messages to nil
In Objective-C, it is valid to send a message to nil—it simply has no effect at runtime. There are several patterns in Cocoa that take advantage of this fact. The value returned from a message to nil may also be valid:

  • If the method returns an object, any pointer type, any integer scalar of size less than or equal to sizeof(void*), a float, a double, a long double, or a long long, then a message sent to nil returns 0.
  • If the method returns a struct, as defined by the Mac OS X ABI Function Call Guide to be returned in registers, then a message sent to nil returns 0.0 for every field in the data structure. Other struct data types will not be filled with zeros.
  • If the method returns anything other than the aforementioned value types the return value of a
    message sent to nil is undefined.

My wife came over to see what I was reading.

Curious to know what a non-programmer’s take on this would be, I asked her to read it to me, interjecting her comments along the way. It went like this:

“Sending Messages to nil” (she paused for a moment, internally analyzing what she’d just gotten into)

“In Objective-C, it is valid to send a message to nil—” (pause) “it simply has no effect at runtime.” This sunk in for a second. “Well it it has no effect, when the heck are you sending it messages for?” She resumed composure.

“There are several patterns in Cocoa that take advantage of this fact.” Confusion set it. “Several patterns? You need more than one to have no effect?

“The value returned from a message to nil may also be valid: …” Her eyes combed the list.

“If the method returns an object, any pointer type, any integer scalar…” She hung on that word, and when I sat quietly, she continued. “…of size less than or equal to size of void star…” This was too much, “What?! Black holes can be different sizes?

Frustrated, “a float, a double, a long ” (…pregnant pause…) “double, or a long long,…” She stopped again. “A ‘long long’? You’re making this up.” She checked my face to see to see if I was pulling a fast one.

She continued, “… then a message sent to nil returns 0.” By this point it the sentence had no meaning.

“If the method returns a struct,” apparently she liked that, adding, “Oh boy! A struct”, but it was unclear if she was teasing me now.

Resuming, “as defined by the Mac OS X ABI Function Call Guide” (which again meant nothing) “to be returned in registers.” I saw a furrowed brow drop, internally wondering what cash registers had to do with any of this.

“…then a message sent to nil returns 0.0 for every…” and she stopped herself.

What?!? Zero-point-zero is different than zero? What the f***!?!” At that point she burst into an embarrassed laughter at just how mind boggling her internal monologue had gotten and refused to continue.

I had never looked at documentation from an outsider’s perspective before, it really did seem like techno-babble in that …context.

BAD IDEA: “Wanted C++/Java Programmers”

It really bugs me when a company looks for candidates based on what programming language they “know.” There’s a better way.

I’m frequently the recipient of recruiter emails that are looking for C++ and Java programmers. And, while I know both of these languages very, very well, I tend to avoid offers that words things in terms of just programming languages.

Consider a help wanted sign that said: “Wanted English Writers”

In this context it’s more obvious what’s wrong: just because you write in a particular language doesn’t mean you’re a particularly good author. And, even if you are a master at words, you might be unable to convey complex ideas to the common man very well. And, even if you can communicate with technical precision, you might not be mentally engaging. And, even if you are able to keep a reader, you might not have an interesting topic to address the masses.

There’s a reason television shows have writers, there’s a reason comedians have joke writers, and there’s a reason why books that you really enjoy are done by a small circle of authors that resonate to your liking. Mastery of a written language doesn’t necessarily make you a writer.

And that’s the fallacy that many technical companies make: they assume that because you can write in C++ or Java, that you must be smart, and clearly smart means good. Right?

Problem is, learning a computer programming language isn’t all that difficult. Learning to program well, takes experience.

Consequently, when I perform an interview with someone for a position, I’m more interested in the problem solving skills and interpersonal communication than how well they know a particular language.

And when I say problem solving skills, I don’t mean the Microsoft “why are manhole covers round” brain teasers. No, I present real code and real problems that’s representative of how my team works together.

What I’ve found is that there are actually three types of candidates that make excellent programmers, regardless of language:

  • Mathematicians – these are people who clearly have a solid grasp of data structures and algorithms.
  • Philosophers – these are people who really, truly, and deeply grasp the intricate details of logic, notation, and language.
  • Musicians – these are people who intuitively see patterns and are extremely creative.

Whenever I’ve hired from these three groups, and the person has solid communication skills, and the person had demonstrated a personal passion for software development, we’ve always had resounding success.

And the funny thing? Those talented people know other talented people.

Reasonable Trial Durations: 30/30/30

I’ve come up with a way for trial software to be fair and recover lost sales opportunities. The secret: 30/30/30.

30/30/30While doing Java development, I was looking for a new IDE for Windows. Naturally, my hunt ended with IntelliJ, with Eclipse and NetBeans close on its heels.

What started the whole chain of thought for me, though, was IntelliJ’s trial period. Frankly, I really respect a company that has enough faith in their products that they let you use them, unencumbered, for a month in order to make an informed purchasing decision.

Oddly enough, though, it wasn’t enough. Allow me to explain.

The purpose behind a trial period is to allow end users to “log enough flight time” with the product that they know whether or not it meets their needs.

And here’s the problem. I have an existing code base of inter-related projects that I need to import into the IDE. And, since this is for work, my schedule is fairly swamped. I can only come up for air to do an evaluation once every week or two just for an hour or so.

What inevitably happens is this: I install the software, validate it installs, then a week or so later, I try to import; it fails, so I table the project until I have more time. A week or two goes by, and I try again, getting closer. Then, when I come up for air and try to get a bit further, the evaluation period is over. I’ve realistically had about less than three hours using the software, and none of it in the IDE writing code.

This happened to me last year as well.

And, what’s the natural conclusion at this point? I don’t know if it will meet my needs or not. Thus, a purchase doesn’t happen.

From marketing’s perspective, they think that the following scenario is the norm: a user downloads the project, tries creating a project, slings a bit of code, gets married to the IDE, and is willing to pay to keep the experience. In fact, I’ve done just this, and I really love IntelliJ.

But, no matter how much love I have for the product, if I can’t move our corporate applications into it from an existing source base, I can’t justify the site-wide purchase. End of story.

Oh sure, I could talk with the kind folks at JetBrains and ask for an extension, and I’m sure they’d give me one.

But that isn’t the point.

Being a software provider myself, I see this as a generic problem. What if I want to produce trial software that’s fair. I can’t have my customers not being able to make a well informed decision for running out of time.

Here’s my solution… 30 days, 30 invocations, 30 hours – Whichever Comes Last

Here’s how it works:

  • You’re guaranteed at least a month of physical time.
  • You’re guaranteed at least 30 invocations.
  • You’re guaranteed at least 30 hours.
  • When all three of the above goals are hit, stop the trial.

Implementing this isn’t be hard at all. It’s also quite fair and balanced.

If you are doing real work, making use of the application for 30 days, then you’re going to quickly chew through the 30 invocations and 30 hours.

If you have just haven’t even tried the software enough, you get 30 attempts.

Finally, the 30 hour rule recognizes if you haven’t had time to actually experience the software.

I’d like to see vendors start taking this approach. It’s a good one, too. It would certainly result in more sales.

Hibernate: Duplicate Mapping and Imports

<GEEK BLOG ENTRY>
I ran into a very frustrating problem this evening, causing me to stay much later than I had intended, and to miss out on some fun socializing event that I was looking forward to. Unfortunately, there was little to no useful information on the Internet as Google was coming up with few and useless results.

I hope this post saves some poor soul from the same fate.

The Problem


I’m using the Hibernate library for persistence with a JBoss EJB using JPA. My code is sprinkled with annotations, my hibernate.cfg.xml file is clean, and I have no *.hbm.xml files. My code compiles. And it runs.

However.

When I try to access something that uses the Hibernate library, I get an odd message about “Duplicate Collection Role Mapping“.

The class in question contained a Set interface and a HashSet implementation for a member.

So, I commented out this container and tried again, hoping to simplify the problem.

This time I was greeted with a “[Mappings] duplicate import” and a “DuplicateMappingException: Duplicate class/entity mapping” set of error messages.

The only related web pages was a handful of archive with people asking similar questions in various online forums.

Almost always these fell into one of three responses:

  1. You’ve got a problem with the mapping element in your hibernate config file.
  2. You’ve got annotations and and class.hbm.xml file doing something wrong.
  3. This is an old bug in JBoss.

None of the symptoms existed in my case.

Here’s How I Solved It


Turns out that Hibernate makes the recommendation that you build a HibernateUtil helper class. Inside it, you’re supposed to make a singleton of the SessionFactory (and in the case of JBoss, you should use JNDI).

A co-worker had refactored the AnnotationConfiguration() to store a single copy, however, the routine that returned it happened to call .configure() on it before returning it each time. An honest mistake, which got integrated silently into my code when I pulled the latest version from version control.

Because .configure() was being called twice, to Hibernate it did look as if I had duplicate mapping directives in my hibernate config file.

Correcting the HibernateUtil method, which handled setting up and returning the AnnotationConfiguration solved the problem.

</GEEK BLOG ENTRY>

Hibernate: No Persistance Provider

While working through a code sample in Java Persistence with Hibernate, I ran into a problem where Hibernate was reporting No Persistence Provider. Solved the problem, and it wasn’t moving the persistence.xml file as you’d normally find on the web as the solution.

I’ve been spending a lot of time with Hibernate, an ORM persistence manager.

After ripping Pro Hibernate 3 a new one with a scathing review, I finally have concluded that the best book on Hibernate is Java Persistence with Hibernate, which is revised edition of Hibernate In Action, though you’ll still need to reference the ERRATA for Java Persistence with Hibernate on occasion.

In the middle of page 76 it was time to go build my sample application, and all I got was a strange message that said: javax.persistence.PersistenceException: No Persistence provider for EntityManager named helloworld


Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named helloworld
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)

Most of the discussions on the Web lead to the conclusion that the file persistence.xml is missing, or if it isn’t, it hasn’t been placed in the META-INF directory. In my case it was present, it was in the right place, and it was correct. What gives? Seems other people were having this problem as well.

The Solution
Thanks to a szermierz‘s entry on the introductory page An Introduction to Java Persistence for Client-Side Developers, which is actually a great article itself, it got me to question what libraries I had in my classpath, and whether or not that list was complete.

Turns out there was one file he listed that I didn’t have: hibernate-entitymanager.jar.

Looks important, eh?


I easily found a copy in the Hibernate Extensions at HibernateExt\tools\lib\testlibs.
There was also a copy in the Hibernate Tools at HibernateTools-3.2.0b9a\plugins\org.hibernate.exlipse_3.2.0.beta9a\lib\annotations.

Once I placed a copy in there, things worked great. So it’s not just the property file, but the .jar file that needs it.

Flex – Could Not Resolve mx:Application

Had a problem compiling a Flex 2 SDK example via the command line. I was getting a “Could not resolve mx:Application to a component implementation” message. Turns out the fix was right under my nose, I wasn’t reading the compiler’s error message.

While looking at some Flex Examples for Flex 2 SDK, I ran into the follow error message:

Error: Could not resolve <mx:Application> to a component implementation.

My code looked like this:




Problem One: Use 2006, not 2003.

However, don’t be too quick to read the message and make this simple change. Read it again! It is telling you what to use.

Error: unsupported MXML namespace found (‘http://www.macromedia.com/2003/mxml’). Please use the MXML namespace ‘http://www.adobe.com/2006/mxml’ for MXML 2 source code.

Problem Two: It is no longer Macromedia, but Adobe.

The correct code that compiles looks like this:




Code Markup for WordPress

I’ve been looking for a decent code markup plugin for WordPress so that I can include source code fragments in WordPress.

Problem is, using <CODE> tag in conjuction with <PRE> injected extra blank lines ( <BR/> ) into the code.

Using Code Markup, I was able to do it.

But there was a trick…

First, the plugin requires that the <code> tag be in lowercase. Internally, I was using uppercase so it’d stand out visually to me. In theory, HTML tags ought to be case insensitive, but the filter required them. I’m going to look at this as a “good thing” since it allows me both worlds. I just wish I found it by a means other than clever guesswork.

Second, if you want spaces preserved, you need to put your code block inside of a <pre> tag. This is actually well documented on the Code Markup site.

Third and finally, do not go sprinkling HTML entities like &amp; in your code; let the filter do it for you.

Unused local variables, a gotcha that’ll getcha

Removing unused local variables from your programs is actually quite safe, makes your code more readable, and helps get faster compiler times, and can even help produce a tighter, faster, more optimized executable. However, there’s one thing you really need to watch out for…

Recently I attended the No Fluff Just Stuff conference again and learned about a free, fantastic static code analyzer for Java, called PMD. It can use be used standalone or even integrated into many popular IDEs like NetBeans or Eclipse. For the curious, I’d tell you what PMD stands for, but no one really knows; worse yet, I can’t stop myself from typing PDM.

PMD has a nifty rule that allows it to locate Unused Local Variables.

Very quickly I was able to walk through our entire code base, identify things that were assigned to, and subsequently not used, and remove them.

Gotcha #0
The first major hit of the day is that you’ll want to do a Clean on your project before you start. Believe it or not, some project building steps can build intermediate .java, files from your master source code. Problem is, if they appear anywhere inside your project’s directory structure when PMD is making the sweep, they get analyzed to. And you don’t want that.

Gotcha #1
It turns out that there’s a reverse ripple effect in performing this kind of code cleanup and operation. After you’ve removed code, you’ll want to make sure you perform subsequent passes until all concerns are removed.

Take for instance this trivial case:
  B = A;
  C = B;
  // C is unused!

What happens is that once you remove C, it turns out B may no longer be used. Remove B, and it’s even possible A may no longer be used either.

Additionally, this can eventually lead to additional Unused Imports, which dealing with those can also decrease build time.

Gotcha #2 — The Real Evil
Normally, this kind of code clean up is absolutely harmless, although there’s one error of omission that a developer can make which will create problems and cause a silent failure.

Here’s a case where a static code analyzer recommends removing a very important line of code:

boolean doSomething(int x) {
  // Do something very important with x
  return result;
}

...
  boolean result = doSomething( x ); // Do Something Important
  // result not used
...

If the return value from a method isn’t used, then the static analyzer will assume the method doesn’t need to be called, and it will recommend commenting out the line — causing your program to silently break.

This is not a error on the part of the tool!

The error was actually that of the developer for not checking the return results of the method.

To correct the problem with the source code, the developer has three options:

  1. Change the method signature to be of type void.
  2. Throw an exception from the method.
  3. Check the return value of the called method.

Otherwise, the real error is that the method may attempt to do something, fail, and communicate back to the caller than something went wrong, but the caller blindly trusts that things are okay.

If you’re not going to check a return value, you shouldn’t be incurring the overhead of sending one. If the library writer provided one for a reason, then you should be using it.

Conclusion
When a code analyzer makes a recommendation, ask yourself what implied rules about your code the analyzer is assuming. Rather than blaming the tool, the better solution is fixing the source.

It may be best to comment out, rather than deleting, code that initially seems superfluous.

Finally, after a massive code sweep, run those unit tests.