You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2015/04/17 11:07:59 UTC

[jira] [Commented] (THRIFT-2877) Optimize generated hashCode

    [ https://issues.apache.org/jira/browse/THRIFT-2877?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14499511#comment-14499511 ] 

ASF GitHub Bot commented on THRIFT-2877:
----------------------------------------

GitHub user roshan opened a pull request:

    https://github.com/apache/thrift/pull/447

    THRIFT-2877 Generate hashCode using primitives, static utility methods

    This is pretty much the List.hashCode() except without any list. It takes about a third the time on some rudimentary benchmarks. I tried it out with
    
    ```
    typedef i32 SomeId
    typedef binary BinId
    typedef string StringId
    
    struct NonTrue {}
    
    union MaybeAThing {
        1: NonTrue nt
        2: bool bl
    }
    
    enum Nomnom {
        EAT=31
        LIVE=515
    }
    
    struct AllPrims {
        1: bool boole
        2: byte single_byte
        3: i16 shrt
        4: i32 integ
        5: i64 longue
        6: double f64
        7: string str
        8: binary bin
        9: Nomnom en
        10: NonTrue stru
        11: MaybeAThing un
        12: SomeId intid
        13: BinId binid
        14: StringId strid
    }
    ```
    
    generating [this hashCode()](https://gist.github.com/roshan/7b7fff349e3ed06322c3).
    
    Populating these structs with some values has it match the AbstractList.hashCode() result but maybe we could try a different multiplicative factor.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/roshan/thrift THRIFT-2877_int_based_hashcode

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/thrift/pull/447.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #447
    
----
commit f7618aee96061e14b323c653ab36d40158c391c4
Author: Roshan George <ro...@arjie.com>
Date:   2015-04-17T07:46:02Z

    THRIFT-2877 Generate hashCode using primitives and static utility methods

----


> Optimize generated hashCode
> ---------------------------
>
>                 Key: THRIFT-2877
>                 URL: https://issues.apache.org/jira/browse/THRIFT-2877
>             Project: Thrift
>          Issue Type: Improvement
>          Components: Java - Compiler
>    Affects Versions: 0.9.3
>            Reporter: Mike Rettig
>            Priority: Critical
>
> The generated java hashCode method allocates an ArrayList then appends the fields to the list.  Primitive fields will be boxed when added to the list. 
> The generated code shouldn't allocate a list or box primitives.  The hashCode can be calculated by using a primitive int and some static utility methods which can return the hashCode for each type.
>  out << indent() << "@Override" << endl << indent() << "public int hashCode() {" << endl;
> 1839   indent_up();
> 1840   indent(out) << "List<Object> list = new ArrayList<Object>();" << endl;
> 1841 
> 1842   for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
> 1843     out << endl;
> 1844 
> 1845     t_type* t = get_true_type((*m_iter)->get_type());
> 1846     bool is_optional = (*m_iter)->get_req() == t_field::T_OPTIONAL;
> 1847     bool can_be_null = type_can_be_null(t);
> 1848     string name = (*m_iter)->get_name();
> 1849 
> 1850     string present = "true";
> 1851 
> 1852     if (is_optional || can_be_null) {
> 1853       present += " && (" + generate_isset_check(*m_iter) + ")";
> 1854     }
> 1855 
> 1856     indent(out) << "boolean present_" << name << " = " << present << ";" << endl;
> 1857     indent(out) << "list.add(present_" << name << ");" << endl;
> 1858     indent(out) << "if (present_" << name << ")" << endl;
> 1859     if (t->is_enum()) {
> 1860       indent(out) << "  list.add(" << name << ".getValue());" << endl;
> 1861     } else {
> 1862       indent(out) << "  list.add(" << name << ");" << endl;
> 1863     }
> 1864   }
> 1865 
> 1866   out << endl;
> 1867   indent(out) << "return list.hashCode();" << endl;
> 1868   indent_down();
> 1869   indent(out) << "}" << endl << endl;
> 1870 }



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)