Performance: remove some occurences of type.to_s.downcase

Those are either DRYed or plain unneeded.

Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Performance: memoize resource#resource_type

Because calling Puppet::Type.type forces a full and slow autoloading
session with a negative result when the given resource type is a
defined type, we therefore cache the result (there's no chance this
value would change during the compilation).
This saves about 20% of compilation time on my setup.

Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Performance: remove some 'require' inside methods

MRI 1.8.7/1.9.3 do a full search in the full load path at every call
to require, even though nothing will be loaded at the end.
This can be slow if require is called in a method that is called
a lot.

This saves a few percents on compilation time on my setup.

Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Performance: TypeCollection should remember inexistant types before autoloading

When the TypeCollection doesn't find a class/definition it tries to
autoload it in each namespace. This requires calling
TypeLoader#import for this qualified name, which is a time consuming
operation.

When loading definitions/classes that can't be found by the autoloader
(ie legacy manifests for instance),  the autoloader will still try
to load them in the autoloaded module path, even though those types
have possibly already been loaded (ie directly imported).
Searching for those definitions/classes then becomes really expensive.

This problem is frequent for people (like me) that migrated manifests
from the 0.24/0.25 era without refactoring all their modules to strictly
follow the autoloading mechanism.

Now, we negatively cache when we couldn't load a type with the autoloader,
so that the next attempt we won't spend our time trying again.

This saves about 30% of compilation time on my setup

Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Performance: template wrapper calling info is slow

On every variable lookup of templates we're trying to gather where
originated this operation in the template. This is time consuming
and needed only when the variable can't be found.
So we now lazily fetch this information only when we actually need
the line information.

Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Performance: negatively cache not loaded types per compilation

When checking a given resource_type, we defer to the builtin type
system. This type system tries to autoload the type as ruby classes
in the ruby loadpath.
If the resource in question happens to be a definition that might be
used a lot of time we would constantly try to load it as a builtin
type from disk.

This patch adds a per thread negative cache system to the type manager.

Note: we could also have checked for definition in P::Resource#resource_type
before going to ruby types, but that would change the semantic of
load ordering. Because at initialization, the master uses some built-in
types to setup dependent files, those would be checked as definitions
first, and would trigger a call to known_resource_type which would
then load the whole user manifests way too early. Thus adding
negative caching seems the sanest solution :)

Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Fix agent backward compatibility tests on osx

On my macbook temporary paths contains series of '+', which are
forbidden as Regexp.

Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>