Changeset 1114

Show
Ignore:
Timestamp:
12/05/07 18:13:51 (9 months ago)
Author:
mikey
Message:

Fixed bug: annotation cache did not store information that an annotation is not present which lead to parsing a doc block again and again for an annotation that is not present. Not loading the annotation parser classes and not doing any parsing for annotations saves about 30% of the runtime.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/reflection/annotations/stubAnnotationCache.php

    r501 r1114  
    44 * 
    55 * @author      Stephan Schmidt <schst@stubbles.net> 
     6 * @author      Frank Kleine <mikey@stubbles.net> 
    67 * @package     stubbles 
    78 * @subpackage  reflection_annotations 
     
    6768     * @param  stubAnnotation  $annotation      optional  the annotation to store 
    6869     */ 
    69     public static function put($target, $targetName, $annotationName, stubAnnotation $annotation = null) { 
    70         if (isset(self::$annotations[$target][$targetName]) == false) { 
     70    public static function put($target, $targetName, $annotationName, stubAnnotation $annotation = null) 
     71    { 
     72        if (isset(self::$annotations[$target][$targetName]) === false) { 
    7173            self::$annotations[$target][$targetName] = array(); 
    7274        } 
     
    7678            self::$annotations[$target][$targetName][$annotationName] = $clone->getSerialized(); 
    7779        } else { 
    78             self::$annotations[$target][$targetName][$annotationName] = null
     80            self::$annotations[$target][$targetName][$annotationName] = ''
    7981        } 
     82    } 
     83 
     84    /** 
     85     * removes an annotation from the cache 
     86     * 
     87     * @param  int             $target          target of the annotation 
     88     * @param  string          $targetName      name of the target 
     89     * @param  string          $annotationName  name of the annotation 
     90     */ 
     91    public static function remove($target, $targetName, $annotationName) 
     92    { 
     93        if (isset(self::$annotations[$target][$targetName]) === false || isset(self::$annotations[$target][$targetName][$annotationName]) === false) { 
     94            return; 
     95        } 
     96         
     97        unset(self::$annotations[$target][$targetName][$annotationName]); 
    8098    } 
    8199 
     
    88106     * @return  bool 
    89107     */ 
    90     public static function has($target, $targetName, $annotationName) { 
     108    public static function has($target, $targetName, $annotationName) 
     109    { 
     110        if (isset(self::$annotations[$target][$targetName]) === false) { 
     111            return false; 
     112        } 
     113         
     114        if (isset(self::$annotations[$target][$targetName][$annotationName]) === false) { 
     115            return false; 
     116        } 
     117         
     118        return self::$annotations[$target][$targetName][$annotationName] !== ''; 
     119    } 
     120 
     121    /** 
     122     * check, whether an annotation is available in the cache 
     123     * 
     124     * @param   int     $target          target of the annotation 
     125     * @param   string  $targetName      name of the target 
     126     * @param   string  $annotationName  name of the annotation 
     127     * @return  bool 
     128     */ 
     129    public static function hasNot($target, $targetName, $annotationName) 
     130    { 
    91131        if (isset(self::$annotations[$target][$targetName]) == false) { 
    92132            return false; 
     
    97137        } 
    98138         
    99         return true
     139        return self::$annotations[$target][$targetName][$annotationName] === ''
    100140    } 
    101141 
     
    108148     * @return  stubAnnotation 
    109149     */ 
    110     public static function get($target, $targetName, $annotationName) { 
    111         if (self::has($target, $targetName, $annotationName) == true) { 
     150    public static function get($target, $targetName, $annotationName) 
     151    { 
     152        if (self::has($target, $targetName, $annotationName) === true) { 
    112153            return clone self::$annotations[$target][$targetName][$annotationName]->getUnserialized(); 
    113154        } 
  • trunk/src/main/php/net/stubbles/reflection/annotations/stubAnnotationFactory.php

    r1072 r1114  
    5555            return stubAnnotationCache::get($target, $fileName . '::' . $targetName, $annotationName); 
    5656        } 
     57         
     58        if (stubAnnotationCache::hasNot($target, $fileName . '::' . $targetName, $annotationName) === true) { 
     59            throw new ReflectionException('Can not find annotation ' . $annotationName); 
     60        } 
    5761 
    5862        $hash = md5($fileName . $comment . $targetName); 
     
    6771 
    6872        if (isset(self::$annotations[$hash][$annotationName]) === false) { 
     73            // put null into cache to save that the annotation does not exist 
     74            stubAnnotationCache::put($target, $fileName . '::' . $targetName, $annotationName); 
    6975            throw new ReflectionException('Can not find annotation ' . $annotationName); 
    7076        } 
  • trunk/src/test/php/net/stubbles/reflection/annotations/stubAnnotationFactoryTestCase.php

    r1072 r1114  
    163163    { 
    164164        // clear the cache from both annotations first 
    165         stubAnnotationCache::put(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation', null); 
    166         stubAnnotationCache::put(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation', null); 
     165        stubAnnotationCache::remove(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation'); 
     166        stubAnnotationCache::remove(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation'); 
    167167         
    168168        $myAnnotation1 = stubAnnotationFactory::create($this->commentComplex, 'MyAnnotation', stubAnnotation::TARGET_CLASS, 'MyClass', __FILE__ . '1'); 
    169169        $this->assertTrue(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation')); 
     170        $this->assertFalse(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation')); 
    170171        $this->assertFalse(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation')); 
     172        $this->assertFalse(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation')); 
    171173        $myAnnotation2 = stubAnnotationFactory::create($this->commentComplex, 'MyAnnotation', stubAnnotation::TARGET_CLASS, 'MyClass', __FILE__ . '2'); 
    172174        $this->assertTrue(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation')); 
     175        $this->assertFalse(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '1' . '::' . 'MyClass', 'MyAnnotation')); 
    173176        $this->assertTrue(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation')); 
     177        $this->assertFalse(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '2' . '::' . 'MyClass', 'MyAnnotation')); 
    174178    } 
    175179 
     
    277281        $this->assertEqual('cucumber', $anno3->veggie); 
    278282    } 
     283 
     284    /** 
     285     * ensure that information about a non-existing annotation is cached as well 
     286     */ 
     287    public function testCachingOfNonExistingAnnotations() 
     288    { 
     289        // make sure that the information is really not in the cache 
     290        stubAnnotationCache::remove(stubAnnotation::TARGET_CLASS, __FILE__ . '::' . 'MyClass', 'NonExistingAnnotation'); 
     291        $this->assertFalse(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__  . '::' . 'MyClass', 'NonExistingAnnotation')); 
     292        $this->assertFalse(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '::' . 'MyClass', 'NonExistingAnnotation')); 
     293        try { 
     294            $myAnnotation1 = stubAnnotationFactory::create($this->commentComplex, 'NonExistingAnnotation', stubAnnotation::TARGET_CLASS, 'MyClass', __FILE__); 
     295        } catch (Exception $e) { 
     296            $this->assertFalse(stubAnnotationCache::has(stubAnnotation::TARGET_CLASS, __FILE__  . '::' . 'MyClass', 'NonExistingAnnotation')); 
     297            $this->assertTrue(stubAnnotationCache::hasNot(stubAnnotation::TARGET_CLASS, __FILE__ . '::' . 'MyClass', 'NonExistingAnnotation')); 
     298            return; 
     299        } 
     300         
     301        $this->fail('Found NonExistingAnnotation while this annotation should not be present.'); 
     302    } 
    279303} 
    280304?>