You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "Will Pierce (JIRA)" <ji...@apache.org> on 2011/03/23 05:03:06 UTC

[jira] [Created] (THRIFT-1107) improvement for compiler-generated python for 'None' object comparisons

improvement for compiler-generated python for 'None' object comparisons
-----------------------------------------------------------------------

                 Key: THRIFT-1107
                 URL: https://issues.apache.org/jira/browse/THRIFT-1107
             Project: Thrift
          Issue Type: Improvement
          Components: Python - Compiler
            Reporter: Will Pierce
            Assignee: Will Pierce


The python code generator produces several python statements, especially the {{write()}} methods' per-field code, that compares something to None using '{{!= None}}', when it is more efficient to use the '{{is not None}}' expression.

>From what I understand, in python it's almost always true that ({{x \!= None}}) == ({{x is not None}}), but the actual implementation and intent is very different.  The '{{\!= None}}' comparison does a by-value comparison that does much more work than an object identity '{{is not None}}' comparison does.

The actual performance impact isn't much, but I benchmarked the performance of '{{x is not None}}' to '{{x \!= None}}' and got some interesting results.  In python 2.4, 2.7 and 3.1, it's about 2-3 times as fast to use '{{is not None}}' over '{{\!= None}}'.

I'll attach a patch to switch to 'is not None', and attach a simple benchmark test script exercising '{{is not None}}' vs. '{{\!= None}}' and post the performance measurements to this ticket.

These URLs are somewhat relevant about this specific issue in general:
* http://www.python.org/dev/peps/pep-0008/ (Search for 'singletons', or scroll to 'Programming Recommendations' item 2)
* http://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python - Stack Overflow question about the same, though it veers into the cost of typecasting to bool, which isn't relevant here
* http://jaredgrubb.blogspot.com/2009/04/python-is-none-vs-none.html - a similar experience and test results that match


--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Closed] (THRIFT-1107) improvement for compiler-generated python for 'None' object comparisons

Posted by "Bryan Duxbury (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/THRIFT-1107?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Bryan Duxbury closed THRIFT-1107.
---------------------------------

       Resolution: Fixed
    Fix Version/s: 0.7

I just committed this. Thanks Will!

> improvement for compiler-generated python for 'None' object comparisons
> -----------------------------------------------------------------------
>
>                 Key: THRIFT-1107
>                 URL: https://issues.apache.org/jira/browse/THRIFT-1107
>             Project: Thrift
>          Issue Type: Improvement
>          Components: Python - Compiler
>            Reporter: Will Pierce
>            Assignee: Will Pierce
>             Fix For: 0.7
>
>         Attachments: THRIFT-1107.compiler_generate_py_is_not_none.patch, test_isnotnone.py
>
>
> The python code generator produces several python statements, especially the {{write()}} methods' per-field code, that compares something to None using '{{!= None}}', when it is more efficient to use the '{{is not None}}' expression.
> From what I understand, in python it's almost always true that ({{x \!= None}}) == ({{x is not None}}), but the actual implementation and intent is very different.  The '{{\!= None}}' comparison does a by-value comparison that does much more work than an object identity '{{is not None}}' comparison does.
> The actual performance impact isn't much, but I benchmarked the performance of '{{x is not None}}' to '{{x \!= None}}' and got some interesting results.  In python 2.4, 2.7 and 3.1, it's about 2-3 times as fast to use '{{is not None}}' over '{{\!= None}}'.
> I'll attach a patch to switch to 'is not None', and attach a simple benchmark test script exercising '{{is not None}}' vs. '{{\!= None}}' and post the performance measurements to this ticket.
> These URLs are somewhat relevant about this specific issue in general:
> * http://www.python.org/dev/peps/pep-0008/ (Search for 'singletons', or scroll to 'Programming Recommendations' item 2)
> * http://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python - Stack Overflow question about the same, though it veers into the cost of typecasting to bool, which isn't relevant here
> * http://jaredgrubb.blogspot.com/2009/04/python-is-none-vs-none.html - a similar experience and test results that match

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (THRIFT-1107) improvement for compiler-generated python for 'None' object comparisons

Posted by "Hudson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/THRIFT-1107?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13010284#comment-13010284 ] 

Hudson commented on THRIFT-1107:
--------------------------------

Integrated in Thrift #98 (See [https://hudson.apache.org/hudson/job/Thrift/98/])
    THRIFT-1107. py: improvement for compiler-generated python for 'None' object comparisons

This patch switches from 'x != None' to 'x is not None' for a small performance boost.

Patch: Will Pierce


> improvement for compiler-generated python for 'None' object comparisons
> -----------------------------------------------------------------------
>
>                 Key: THRIFT-1107
>                 URL: https://issues.apache.org/jira/browse/THRIFT-1107
>             Project: Thrift
>          Issue Type: Improvement
>          Components: Python - Compiler
>            Reporter: Will Pierce
>            Assignee: Will Pierce
>             Fix For: 0.7
>
>         Attachments: THRIFT-1107.compiler_generate_py_is_not_none.patch, test_isnotnone.py
>
>
> The python code generator produces several python statements, especially the {{write()}} methods' per-field code, that compares something to None using '{{!= None}}', when it is more efficient to use the '{{is not None}}' expression.
> From what I understand, in python it's almost always true that ({{x \!= None}}) == ({{x is not None}}), but the actual implementation and intent is very different.  The '{{\!= None}}' comparison does a by-value comparison that does much more work than an object identity '{{is not None}}' comparison does.
> The actual performance impact isn't much, but I benchmarked the performance of '{{x is not None}}' to '{{x \!= None}}' and got some interesting results.  In python 2.4, 2.7 and 3.1, it's about 2-3 times as fast to use '{{is not None}}' over '{{\!= None}}'.
> I'll attach a patch to switch to 'is not None', and attach a simple benchmark test script exercising '{{is not None}}' vs. '{{\!= None}}' and post the performance measurements to this ticket.
> These URLs are somewhat relevant about this specific issue in general:
> * http://www.python.org/dev/peps/pep-0008/ (Search for 'singletons', or scroll to 'Programming Recommendations' item 2)
> * http://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python - Stack Overflow question about the same, though it veers into the cost of typecasting to bool, which isn't relevant here
> * http://jaredgrubb.blogspot.com/2009/04/python-is-none-vs-none.html - a similar experience and test results that match

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (THRIFT-1107) improvement for compiler-generated python for 'None' object comparisons

Posted by "Will Pierce (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/THRIFT-1107?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Will Pierce updated THRIFT-1107:
--------------------------------

    Attachment: THRIFT-1107.compiler_generate_py_is_not_none.patch

Attached small patch to compiler: THRIFT-1107.compiler_generate_py_is_not_none.patch

It changes 4 lines in compiler/cpp/src/generate/t_py_generator.cc from "\!= None" to "is not None".

> improvement for compiler-generated python for 'None' object comparisons
> -----------------------------------------------------------------------
>
>                 Key: THRIFT-1107
>                 URL: https://issues.apache.org/jira/browse/THRIFT-1107
>             Project: Thrift
>          Issue Type: Improvement
>          Components: Python - Compiler
>            Reporter: Will Pierce
>            Assignee: Will Pierce
>         Attachments: THRIFT-1107.compiler_generate_py_is_not_none.patch, test_isnotnone.py
>
>
> The python code generator produces several python statements, especially the {{write()}} methods' per-field code, that compares something to None using '{{!= None}}', when it is more efficient to use the '{{is not None}}' expression.
> From what I understand, in python it's almost always true that ({{x \!= None}}) == ({{x is not None}}), but the actual implementation and intent is very different.  The '{{\!= None}}' comparison does a by-value comparison that does much more work than an object identity '{{is not None}}' comparison does.
> The actual performance impact isn't much, but I benchmarked the performance of '{{x is not None}}' to '{{x \!= None}}' and got some interesting results.  In python 2.4, 2.7 and 3.1, it's about 2-3 times as fast to use '{{is not None}}' over '{{\!= None}}'.
> I'll attach a patch to switch to 'is not None', and attach a simple benchmark test script exercising '{{is not None}}' vs. '{{\!= None}}' and post the performance measurements to this ticket.
> These URLs are somewhat relevant about this specific issue in general:
> * http://www.python.org/dev/peps/pep-0008/ (Search for 'singletons', or scroll to 'Programming Recommendations' item 2)
> * http://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python - Stack Overflow question about the same, though it veers into the cost of typecasting to bool, which isn't relevant here
> * http://jaredgrubb.blogspot.com/2009/04/python-is-none-vs-none.html - a similar experience and test results that match

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (THRIFT-1107) improvement for compiler-generated python for 'None' object comparisons

Posted by "Will Pierce (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/THRIFT-1107?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13009988#comment-13009988 ] 

Will Pierce commented on THRIFT-1107:
-------------------------------------

Output from benchmark script (test_isnotnone.py attached):

% for i in python2.4 python2.7 python3.1; do $i ./test_isnotnone.py 125; done 
h2. Testing python v2.4.6, 125.0 million iterations
|| Code || Time Per Iteration (microseconds) || Total Test Elapsed Time (seconds)
|       x != None | 0.08892 usec/iteration |  11.115 total sec elapsed
|   x is not None | 0.05089 usec/iteration |   6.361 total sec elapsed

h2. Testing python v2.7.0, 125.0 million iterations
|| Code || Time Per Iteration (microseconds) || Total Test Elapsed Time (seconds)
|       x != None | 0.06088 usec/iteration |   7.611 total sec elapsed
|   x is not None | 0.03261 usec/iteration |   4.077 total sec elapsed

h2. Testing python v3.1.2, 125.0 million iterations
|| Code || Time Per Iteration (microseconds) || Total Test Elapsed Time (seconds)
|       x != None | 0.08650 usec/iteration |  10.813 total sec elapsed
|   x is not None | 0.02976 usec/iteration |   3.720 total sec elapsed



> improvement for compiler-generated python for 'None' object comparisons
> -----------------------------------------------------------------------
>
>                 Key: THRIFT-1107
>                 URL: https://issues.apache.org/jira/browse/THRIFT-1107
>             Project: Thrift
>          Issue Type: Improvement
>          Components: Python - Compiler
>            Reporter: Will Pierce
>            Assignee: Will Pierce
>         Attachments: test_isnotnone.py
>
>
> The python code generator produces several python statements, especially the {{write()}} methods' per-field code, that compares something to None using '{{!= None}}', when it is more efficient to use the '{{is not None}}' expression.
> From what I understand, in python it's almost always true that ({{x \!= None}}) == ({{x is not None}}), but the actual implementation and intent is very different.  The '{{\!= None}}' comparison does a by-value comparison that does much more work than an object identity '{{is not None}}' comparison does.
> The actual performance impact isn't much, but I benchmarked the performance of '{{x is not None}}' to '{{x \!= None}}' and got some interesting results.  In python 2.4, 2.7 and 3.1, it's about 2-3 times as fast to use '{{is not None}}' over '{{\!= None}}'.
> I'll attach a patch to switch to 'is not None', and attach a simple benchmark test script exercising '{{is not None}}' vs. '{{\!= None}}' and post the performance measurements to this ticket.
> These URLs are somewhat relevant about this specific issue in general:
> * http://www.python.org/dev/peps/pep-0008/ (Search for 'singletons', or scroll to 'Programming Recommendations' item 2)
> * http://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python - Stack Overflow question about the same, though it veers into the cost of typecasting to bool, which isn't relevant here
> * http://jaredgrubb.blogspot.com/2009/04/python-is-none-vs-none.html - a similar experience and test results that match

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Updated] (THRIFT-1107) improvement for compiler-generated python for 'None' object comparisons

Posted by "Will Pierce (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/THRIFT-1107?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Will Pierce updated THRIFT-1107:
--------------------------------

    Attachment: test_isnotnone.py

Attaching benchmark test script: test_isnotnone.py

This takes a single cmdline argument, the # of millions of iterations to execute.  It prints out confluence/jira style markup with the benchmark results comparing "is not None" to "\!= None"

I will post that output in a moment, for python 2.4, 2.7, and 3.1.


> improvement for compiler-generated python for 'None' object comparisons
> -----------------------------------------------------------------------
>
>                 Key: THRIFT-1107
>                 URL: https://issues.apache.org/jira/browse/THRIFT-1107
>             Project: Thrift
>          Issue Type: Improvement
>          Components: Python - Compiler
>            Reporter: Will Pierce
>            Assignee: Will Pierce
>         Attachments: test_isnotnone.py
>
>
> The python code generator produces several python statements, especially the {{write()}} methods' per-field code, that compares something to None using '{{!= None}}', when it is more efficient to use the '{{is not None}}' expression.
> From what I understand, in python it's almost always true that ({{x \!= None}}) == ({{x is not None}}), but the actual implementation and intent is very different.  The '{{\!= None}}' comparison does a by-value comparison that does much more work than an object identity '{{is not None}}' comparison does.
> The actual performance impact isn't much, but I benchmarked the performance of '{{x is not None}}' to '{{x \!= None}}' and got some interesting results.  In python 2.4, 2.7 and 3.1, it's about 2-3 times as fast to use '{{is not None}}' over '{{\!= None}}'.
> I'll attach a patch to switch to 'is not None', and attach a simple benchmark test script exercising '{{is not None}}' vs. '{{\!= None}}' and post the performance measurements to this ticket.
> These URLs are somewhat relevant about this specific issue in general:
> * http://www.python.org/dev/peps/pep-0008/ (Search for 'singletons', or scroll to 'Programming Recommendations' item 2)
> * http://stackoverflow.com/questions/100732/why-is-if-not-someobj-better-than-if-someobj-none-in-python - Stack Overflow question about the same, though it veers into the cost of typecasting to bool, which isn't relevant here
> * http://jaredgrubb.blogspot.com/2009/04/python-is-none-vs-none.html - a similar experience and test results that match

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira