This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 256220 - Possibly unnessesary time spent in loop calculating free disk space
Summary: Possibly unnessesary time spent in loop calculating free disk space
Status: NEW
Alias: None
Product: installer
Classification: Unclassified
Component: NBI (show other bugs)
Version: 8.1
Hardware: PC Windows 10 x64
: P3 normal (vote)
Assignee: Libor Fischmeistr
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-10-28 06:57 UTC by Chiana
Modified: 2015-10-28 07:18 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chiana 2015-10-28 06:57:08 UTC
Product Version = NetBeans IDE Dev (Build 201510270002)
Operating System = Windows10 running on amd64
Java; VM; Vendor = 1.7.0_45
Runtime = Java HotSpot(TM) 64-Bit Server VM 24.45-b08

In the native support library for windows there is an unnessesay loop that is actually not needed, original reads;
JNIEXPORT jlong JNICALL Java_org_netbeans_installer_utils_system_WindowsNativeUtils_getFreeSpace0(JNIEnv* jEnv, jobject jObject, jstring jPath) {
    WCHAR*  path = getWideChars(jEnv, jPath);
    jlong  size = 0;
    typedef struct int64s { unsigned long Low, High; } int64t;
    int64t bytes;
    if (GetDiskFreeSpaceExW(path, (PULARGE_INTEGER) &bytes, NULL, NULL)) {
        unsigned long h = bytes.High;
        // workaround of using missing _allmul function
        // replace multiplication by sum
        // (2^32 * high + low) = 2^32 + 2^32 + ... (bytes.High times total) + bytes.Low     
        // can be relatively expensive on big sizes (peta bytes and more)
        while(h > 0) {
            h--;
            size+=4294967296L;
        }
        size+= bytes.Low;
    } else {
        throwException(jEnv, "Native error");
    }
    
    FREE(path);
    
    return size;
}

Could be rewritten as;

JNIEXPORT jlong JNICALL Java_org_netbeans_installer_utils_system_WindowsNativeUtils_getFreeSpace0(JNIEnv* jEnv, jobject jObject, jstring jPath) {
    WCHAR*  path = getWideChars(jEnv, jPath);
    jlong  size = 0;
    typedef struct int64s { unsigned long Low, High; } int64t;
    int64t bytes;
    if (GetDiskFreeSpaceExW(path, (PULARGE_INTEGER) &bytes, NULL, NULL)) {
         size = (bytes.high << 32) + bytes.low;
    } else {
        throwException(jEnv, "Native error");
    }
    
    FREE(path);
    
    return size;
}

or even simpler;

JNIEXPORT jlong JNICALL Java_org_netbeans_installer_utils_system_WindowsNativeUtils_getFreeSpace0(JNIEnv* jEnv, jobject jObject, jstring jPath) {
    WCHAR*  path = getWideChars(jEnv, jPath);
    jlong  size = 0;
    if (GetDiskFreeSpaceExW(path, (PULARGE_INTEGER) &size, NULL, NULL)==0) {
        throwException(jEnv, "Native error");
    }
    
    FREE(path);
    
    return size;
}

This because a jlong is a __int64 as is an ULARGE_INTEGER, the only problem that can occur is that if you have something like a goodzillion petabytes of free diskspace the number returned will be negative as an jlong is signed, but this will be a lot faster.
Comment 1 Chiana 2015-10-28 07:18:34 UTC
Sorry, made a typo in the first example, the shift operation should read;

  size = ((jlong) bytes.High) << 32 + bytes.Low;
or there will be a conversion error.