cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Zoltan Farkas (Reopened) (JIRA)" <j...@apache.org>
Subject [jira] [Reopened] (CASSANDRA-3605) Exception swallowing in Hex.java
Date Sat, 10 Dec 2011 12:59:40 GMT

     [ https://issues.apache.org/jira/browse/CASSANDRA-3605?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Zoltan Farkas reopened CASSANDRA-3605:
--------------------------------------


Hi current implementation might work, but I propose the following:

    private static final Logger LOG= LoggerFactory.getLogger(Hex.class);
    private static final Constructor<String> stringConstructor;
    static {
        Constructor<String> cons = null;
        try {
            // test use of protected constructor
            cons = getProtectedConstructor(String.class, int.class, int.class, char[].class);
            char [] c = new char [] {};
            cons.newInstance(0, c.length, c);
        } catch (Exception ex) { // TODO improve this in java7
            LOG.warn("Not Using String instantiation optimization", ex);
        } 
        stringConstructor = cons;
    }

.....

    /**
     * Create a String from a char array with zero-copy (if available), using reflection to
access a package-protected constructor of String.
     * */
    public static String wrapCharArray(char[] c)
    {
        if (c == null)
            return null;

        String s = null;

        if (stringConstructor != null)
        {
            try
            {
                s = stringConstructor.newInstance(0, c.length, c);
            }
            catch (Exception e)
            {
                throw new RuntimeException(e);
            }
        }
        return s == null ? new String(c) : s;
    }
    
    /**
     * Used to get access to protected/private constructor of the specified class
     * @param klass - name of the class
     * @param paramTypes - types of the constructor parameters
     * @return Constructor if successful, null if the constructor cannot be
     * accessed
     */
    public static Constructor getProtectedConstructor(Class klass, Class... paramTypes) throws
NoSuchMethodException
    {
        Constructor c;
        c = klass.getDeclaredConstructor(paramTypes);
        c.setAccessible(true);
        return c;       
    }

This implementation has 2 advantages:

1. Exception is not lost, and we know the fact that the optimization does not work.
2. In case the optimization does not work performance will be an order of magnitude better
due to the fact that Exceptions will not be created at every string allocation attempt.


let me know if you have any questions




                
> Exception swallowing in Hex.java
> --------------------------------
>
>                 Key: CASSANDRA-3605
>                 URL: https://issues.apache.org/jira/browse/CASSANDRA-3605
>             Project: Cassandra
>          Issue Type: Improvement
>    Affects Versions: 1.0.5
>         Environment: all
>            Reporter: Zoltan Farkas
>            Priority: Minor
>
> org.apache.cassandra.utils.Hex line 94:
>             try
>             {
>                 s = stringConstructor.newInstance(0, c.length, c);
>             }
>             catch (Exception e)
>             {
>                 // Swallowing as we'll just use a copying constructor
>             }
> this code does not comply with coding standard, caught exception needs to be rethrown
as RuntimeException

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message