diff --git a/mercurial/commands.py b/mercurial/commands.py
index acbd19b9fbe144ebe7096a3de79897b628993176_bWVyY3VyaWFsL2NvbW1hbmRzLnB5..19b1c62cee1c83371780fa4621b8ac30f079c227_bWVyY3VyaWFsL2NvbW1hbmRzLnB5 100644
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -2548,8 +2548,10 @@
             ui.write(('deltas against other : ') + fmt % pcfmt(numother,
                                                              numdeltas))
 
-@command('debugrevspec', [], ('REVSPEC'))
-def debugrevspec(ui, repo, expr):
+@command('debugrevspec',
+    [('', 'optimize', None, _('print parsed tree after optimizing'))],
+    ('REVSPEC'))
+def debugrevspec(ui, repo, expr, **opts):
     """parse and apply a revision specification
 
     Use --verbose to print the parsed tree before and after aliases
@@ -2561,6 +2563,9 @@
         newtree = revset.findaliases(ui, tree)
         if newtree != tree:
             ui.note(revset.prettyformat(newtree), "\n")
+        if opts["optimize"]:
+            weight, optimizedtree = revset.optimize(newtree, True)
+            ui.note("* optimized:\n", revset.prettyformat(optimizedtree), "\n")
     func = revset.match(ui, expr)
     for c in func(repo, revset.baseset(range(len(repo)))):
         ui.write("%s\n" % c)
diff --git a/tests/test-completion.t b/tests/test-completion.t
index acbd19b9fbe144ebe7096a3de79897b628993176_dGVzdHMvdGVzdC1jb21wbGV0aW9uLnQ=..19b1c62cee1c83371780fa4621b8ac30f079c227_dGVzdHMvdGVzdC1jb21wbGV0aW9uLnQ= 100644
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -250,7 +250,7 @@
   debugrebuilddirstate: rev
   debugrename: rev
   debugrevlog: changelog, manifest, dump
-  debugrevspec: 
+  debugrevspec: optimize
   debugsetparents: 
   debugsub: rev
   debugsuccessorssets: