Procrastinators Revolt! Procrastinators Revolt!
Unfocused thoughts of a procrastinator
  • Home
  • SketchUp Extensions
    • Extension Warehouse
    • Vertex Tools
    • SUbD
  • Resources
    • GitHub Projects
    • BitBucket Projects
    • SketchUp
      • TT_Lib² Documentation
      • WebDialogs – The Lost Manual
      • State of SketchUp’s Observers
      • SKUI — A GUI Framework for SketchUp
    • Old Blog
    • Workshop
  • Gallery
  • Social
    • Flickr
    • Instagram
    • GitHub
    • YouTube
    • Twitter
    • LinkedIn
Procrastinators Revolt! Procrastinators Revolt!
  • Home
  • SketchUp Extensions
    • Extension Warehouse
    • Vertex Tools
    • SUbD
  • Resources
    • GitHub Projects
    • BitBucket Projects
    • SketchUp
      • TT_Lib² Documentation
      • WebDialogs – The Lost Manual
      • State of SketchUp’s Observers
      • SKUI — A GUI Framework for SketchUp
    • Old Blog
    • Workshop
  • Gallery
  • Social
    • Flickr
    • Instagram
    • GitHub
    • YouTube
    • Twitter
    • LinkedIn
Blog
Procrastinators Revolt! / SketchUp / Ruby API / Never ever use .typename / Comment Page 1
Dec 31

Never ever use .typename

  • 31 December 2011
  • Thomas Thomassen
  • 3 Comments
  • Ruby API, SketchUp
  • API, Performance, SketchUp, typename

Something I all too often observe in plugins for SketchUp that kills performance is the usage of .typename within loops. I blame the SketchUp Ruby API documentation for this widespread usage as it’s used throughout the code examples.

Alex Mozg illustrated how big the performance hit is with his speed test snippet posed in a SketchUcation thread.

Using his script I found the biggest model I had at hand and came out with the following results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
=== AT - SpeedTest ===
is_a? - 16.297s
kind_of? - 16.141s
class - 18.703s
typename - 88.703s
 
=== Model Statistics ===
edges = 8396595
faces = 1026520
groups = 52643
components = 526428
construction_lines = 2
construction_points = 4
images = 0
section_planes = 0
texts = 0
others = 0
=== AT - SpeedTest ===
is_a? - 16.297s
kind_of? - 16.141s
class - 18.703s
typename - 88.703s

=== Model Statistics ===
edges = 8396595
faces = 1026520
groups = 52643
components = 526428
construction_lines = 2
construction_points = 4
images = 0
section_planes = 0
texts = 0
others = 0

As you can see, .typename is many times slower than .class or .is_a?. This is because .typename means string comparison which is a slow operation in any language. The other methods compares based on object classes which is simple lookups – many times faster.

The exception

There is a set of entities where one has to use .typename in order to determine what an entity is; Polyline3d, DimensionLinear and DimensionRadial. All of these report Sketchup::DrawingElement to Entity.class. These are entities which the Ruby API doesn’t really expose any control over. The only thing is that .typename will return "Polyline3d", "DimensionLinear" and "DimensionRadial" respectively.

If you really need to identify these objects in a loop, first check if it’s class is SketchUp::DrawingElement then use .typename in order to reduce string comparisons.

1
2
3
4
5
6
7
8
9
10
11
for entity in entities
  if entity.class == Sketchup::DrawingElement
    if entity.typename == 'Polyline3d'
      # ...
    elsif entity.typename == 'DimensionLinear'
      # ...
    elsif entity.typename == 'DimensionRadial'
      # ...
    end
  end
end
for entity in entities
  if entity.class == Sketchup::DrawingElement
    if entity.typename == 'Polyline3d'
      # ...
    elsif entity.typename == 'DimensionLinear'
      # ...
    elsif entity.typename == 'DimensionRadial'
      # ...
    end
  end
end

Share this:

  • Facebook
  • Twitter
  • Reddit
  • LinkedIn
  • Bluesky
  • More
  • Pinterest
  • Print

Like this:

Like Loading...

Related Posts

  • PickHelper — A Visual Guide14 January 2013
  • SketchUp Plugins can be Installed Anywhere9 September 2012
  • Dealing with Units in SketchUp17 August 2012
  • The Secrets of SketchUp’s Materials17 March 2012

3 Comments

  1. John
    31 December 2011 at 16:20 ·

    hi Thom,

    nice write-up,
    when I grep my Plugins folder for .typename I get 276 hits,
    can find and replace be used to en-mass, with a filter for ‘Polyline3d’ or would each need case use evaluation and testing?

    Which of the alternatives would be the ‘safest’ switch.
    john

    Loading...
  2. Thomas Thomassen
    31 December 2011 at 17:42 ·

    You cannot do a simple search and replace I’m afraid. The pattern for which .typename is used may vary.

    It could be in a if else structure:

    1
    2
    3
    4
    
    if e.typename == 'Edge'
    elsif e.typename == 'Face'
    elsif e.typename == 'Group'
    end
    if e.typename == 'Edge'
    elsif e.typename == 'Face'
    elsif e.typename == 'Group'
    end

    It could be in a switch structure:

    1
    2
    3
    4
    5
    
    case e.typename
      when 'Edge'
      when 'Face'
      when 'Group'
    end
    case e.typename
      when 'Edge'
      when 'Face'
      when 'Group'
    end

    Or it could even be used standalone – maybe to just display the description of the selected entity. (A use case I didn’t describe in the exception section.)

    The ‘Polyline3d’ etc. tests is only needed if the script needs to be checking for polyline or dimension entities. I described them as cases where one cannot avoid using .typename.

    So unfortunately, one has to look at each plugin and the source code in order to make a reliable replacement for .typename.

    I really wish the API documentation would change its examples to not use that method. It promotes a really bad practice unfortunately.

    Loading...
  3. SketchUp Plugin Checklist ← Procrastinators Revolt!
    11 February 2013 at 21:08 ·

    […] Avoid Entity.typename, use Entity.is_a? […]

    Loading...

Leave a ReplyCancel reply

Recent Posts

  • The Great Motorcycle Road Trip of 2024 10 December 2024
  • 2024 summer motorcycle trip around Dovre 9 December 2024
  • Motorcycle trip from Trondheim, Norway to Lund, Sweden and back via Denmark, May 2024 25 September 2024
  • Ergonomics of unit tests 31 December 2023
  • Extension Sources 1.0 Released 9 July 2022

RSS Feed

RSS Atom

Categories

  • Boardgames (1)
  • Laser Cutting (3)
  • Motorcycle (3)
  • Ramblings (1)
  • SketchUp (34)
    • Plugin (12)
    • Ruby API (15)
  • Software (3)
  • Software Development (1)
  • Tutorial (3)
  • Uncategorized (1)
  • Usability (2)
  • V-Ray (1)

Blogroll

  • Official SketchUp Blog
  • SketchUp [Plugins] Blog
  • SketchUp API Blog
Copyright © 2004–2024 Thomas Thomassen, All Rights Reserved.
  • Contact
  • About
 

Loading Comments...
 

    %d