Rectangle 27 7

This is a paste from one of my methods (Restlet 2.0). Here I have a form that includes one file upload plus other fields, therefore it is rather complete:

@Post
public Representation createTransaction(Representation entity) {
    Representation rep = null;
    if (entity != null) {
        if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) {
            // 1/ Create a factory for disk-based file items
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setSizeThreshold(1000240);

            // 2/ Create a new file upload handler
            RestletFileUpload upload = new RestletFileUpload(factory);
            List<FileItem> items;
            try {
                // 3/ Request is parsed by the handler which generates a list of FileItems
                items = upload.parseRequest(getRequest());

                Map<String, String> props = new HashMap<String, String>();
                File file = null;
                String filename = null;

                for (final Iterator<FileItem> it = items.iterator(); it.hasNext(); ) {
                    FileItem fi = it.next();
                    String name = fi.getName();
                    if (name == null) {
                        props.put(fi.getFieldName(), new String(fi.get(), "UTF-8"));
                    } else {
                        String tempDir = System.getProperty("java.io.tmpdir");
                        file = new File(tempDir + File.separator + "file.txt");
                        filename = name;
                        fi.getInputStream();
                        fi.write(file);
                    }
                }

                // [...] my processing code

                String redirectUrl = ...; // address of newly created resource
                getResponse().redirectSeeOther(redirectUrl);
            } catch (Exception e) {
                // The message of all thrown exception is sent back to
                // client as simple plain text
                getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
                e.printStackTrace();
                rep = new StringRepresentation(e.getMessage(), MediaType.TEXT_PLAIN);
            }
        } else {
            // other format != multipart form data
            getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            rep = new StringRepresentation("Multipart/form-data required", MediaType.TEXT_PLAIN);
        }
    } else {
        // POST request with no entity.
        getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        rep = new StringRepresentation("Error", MediaType.TEXT_PLAIN);
    }

    return rep;
}

I'll end up refactoring it to something more generic, but this is what I have by now.

but don't you need an annotation for the Post request, like @Post("form") or something? I get a 405 returned for doing something very similar

I do have an @Post annotation (but with no parameters). The parameter limits the representations you accept (restlet.org/documentation/2.0/jse/api/org/restlet/resource/). The HTTP 405 is method not allowed (w3.org/Protocols/rfc2616/rfc2616-sec10.html) - Restlet may not recognise your resource as 'postable' if no @Post annotation is present.

I had it as just @Post, and it continued to return a 405. Then I added this: getMetadataService().addExtension("multipart", MediaType.MULTIPART_FORM_DATA, true); to the resources constructor, and changed my annotation from @Post to @Post("multipart") and it then worked.

java - RESTlet: How to process multipart/form-data requests? - Stack O...

java restlet multipartform-data
Rectangle 27 7

This is a paste from one of my methods (Restlet 2.0). Here I have a form that includes one file upload plus other fields, therefore it is rather complete:

@Post
public Representation createTransaction(Representation entity) {
    Representation rep = null;
    if (entity != null) {
        if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) {
            // 1/ Create a factory for disk-based file items
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setSizeThreshold(1000240);

            // 2/ Create a new file upload handler
            RestletFileUpload upload = new RestletFileUpload(factory);
            List<FileItem> items;
            try {
                // 3/ Request is parsed by the handler which generates a list of FileItems
                items = upload.parseRequest(getRequest());

                Map<String, String> props = new HashMap<String, String>();
                File file = null;
                String filename = null;

                for (final Iterator<FileItem> it = items.iterator(); it.hasNext(); ) {
                    FileItem fi = it.next();
                    String name = fi.getName();
                    if (name == null) {
                        props.put(fi.getFieldName(), new String(fi.get(), "UTF-8"));
                    } else {
                        String tempDir = System.getProperty("java.io.tmpdir");
                        file = new File(tempDir + File.separator + "file.txt");
                        filename = name;
                        fi.getInputStream();
                        fi.write(file);
                    }
                }

                // [...] my processing code

                String redirectUrl = ...; // address of newly created resource
                getResponse().redirectSeeOther(redirectUrl);
            } catch (Exception e) {
                // The message of all thrown exception is sent back to
                // client as simple plain text
                getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
                e.printStackTrace();
                rep = new StringRepresentation(e.getMessage(), MediaType.TEXT_PLAIN);
            }
        } else {
            // other format != multipart form data
            getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            rep = new StringRepresentation("Multipart/form-data required", MediaType.TEXT_PLAIN);
        }
    } else {
        // POST request with no entity.
        getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        rep = new StringRepresentation("Error", MediaType.TEXT_PLAIN);
    }

    return rep;
}

I'll end up refactoring it to something more generic, but this is what I have by now.

but don't you need an annotation for the Post request, like @Post("form") or something? I get a 405 returned for doing something very similar

I do have an @Post annotation (but with no parameters). The parameter limits the representations you accept (restlet.org/documentation/2.0/jse/api/org/restlet/resource/). The HTTP 405 is method not allowed (w3.org/Protocols/rfc2616/rfc2616-sec10.html) - Restlet may not recognise your resource as 'postable' if no @Post annotation is present.

I had it as just @Post, and it continued to return a 405. Then I added this: getMetadataService().addExtension("multipart", MediaType.MULTIPART_FORM_DATA, true); to the resources constructor, and changed my annotation from @Post to @Post("multipart") and it then worked.

java - RESTlet: How to process multipart/form-data requests? - Stack O...

java restlet multipartform-data
Rectangle 27 1

Then in your loop through your items, you can test whether they are or not fileItems with the method: isFormField().

Testing if a fileItem is a formField... makes sens ? ;) but it works.

java - RESTlet: How to process multipart/form-data requests? - Stack O...

java restlet multipartform-data
Rectangle 27 1

Then in your loop through your items, you can test whether they are or not fileItems with the method: isFormField().

Testing if a fileItem is a formField... makes sens ? ;) but it works.

java - RESTlet: How to process multipart/form-data requests? - Stack O...

java restlet multipartform-data
Rectangle 27 86

This is another approach to parse the the process list from the command "ps -e":

try {
    String line;
    Process p = Runtime.getRuntime().exec("ps -e");
    BufferedReader input =
            new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((line = input.readLine()) != null) {
        System.out.println(line); //<-- Parse data here.
    }
    input.close();
} catch (Exception err) {
    err.printStackTrace();
}

If you are using Windows, then you should change the line: "Process p = Runtime.getRun..." etc... (3rd line), for one that looks like this:

Process p = Runtime.getRuntime().exec
    (System.getenv("windir") +"\\system32\\"+"tasklist.exe");

how to get the process start time and end time

On Windows, run tasklist.exe /fo csv /nh to get the list in CSV format, that's much easier to parse.

but it is not showing the jar name. My executable jar name is helloDemo.jar. but it is not showing anything for it

Why wouldn't it work if I add a | grep java ? i.e. ps -elf | grep java won't return a thing, but ps -elf would work as expected.

It should be noted that the "windows specific" bit at the bottom appears to be unnecessary. On Windows 10, (.exec (Runtime/getRuntime) "tasklist")) (in Clojure, using Java-interop) correctly returns the tasklist process, even without specifying a directory.

How to get a list of current open windows/process with Java? - Stack O...

java process
Rectangle 27 19

$scope.saveForm = function () {
      var formData = new FormData();
      var file = $scope.myFile;
      var json = $scope.myJson;
      formData.append("file", file);
      formData.append("ad",JSON.stringify(json));//important: convert to string JSON!
      var req = {
        url: '/upload',
        method: 'POST',
        headers: {'Content-Type': undefined},
        data: formData,
        transformRequest: function (data, headersGetterFunction) {
          return data;
        }
      };
@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody
    Advertisement storeAd(@RequestPart("ad") String adString, @RequestPart("file") MultipartFile file) throws IOException {

        Advertisement jsonAd = new ObjectMapper().readValue(adString, Advertisement.class);
//do whatever you want with your file and jsonAd

Helped me sort out my issue too.. Thanks a lot @user3207391

consumes = MediaType.MULTIPART_FORM_DATA_VALUE
@RequestMapping

java - How to process a multipart request consisting of a file and a J...

java json spring rest multipartform-data
Rectangle 27 19

$scope.saveForm = function () {
      var formData = new FormData();
      var file = $scope.myFile;
      var json = $scope.myJson;
      formData.append("file", file);
      formData.append("ad",JSON.stringify(json));//important: convert to string JSON!
      var req = {
        url: '/upload',
        method: 'POST',
        headers: {'Content-Type': undefined},
        data: formData,
        transformRequest: function (data, headersGetterFunction) {
          return data;
        }
      };
@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody
    Advertisement storeAd(@RequestPart("ad") String adString, @RequestPart("file") MultipartFile file) throws IOException {

        Advertisement jsonAd = new ObjectMapper().readValue(adString, Advertisement.class);
//do whatever you want with your file and jsonAd

Helped me sort out my issue too.. Thanks a lot @user3207391

consumes = MediaType.MULTIPART_FORM_DATA_VALUE
@RequestMapping

java - How to process a multipart request consisting of a file and a J...

java json spring rest multipartform-data
Rectangle 27 27

Here is a rough but more declarative solution. I haven't been able to get it down to a single annotation, but this seems to work well. Also not sure about performance on large data sets.

{
    "list": [
        {
            "wrapper": {
                "name": "Jack"
            }
        },
        {
            "wrapper": {
                "name": "Jane"
            }
        }
    ]
}
public class RootObject {
    @JsonProperty("list")
    @JsonDeserialize(contentUsing = SkipWrapperObjectDeserializer.class)
    @SkipWrapperObject("wrapper")
    public InnerObject[] innerObjects;
}
public class InnerObject {
    @JsonProperty("name")
    public String name;
}

Where the Jackson voodoo is implemented like:

@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface SkipWrapperObject {
    String value();
}

and

public class SkipWrapperObjectDeserializer extends JsonDeserializer<Object> implements
        ContextualDeserializer {
    private Class<?> wrappedType;
    private String wrapperKey;

    public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
            BeanProperty property) throws JsonMappingException {
        SkipWrapperObject skipWrapperObject = property
                .getAnnotation(SkipWrapperObject.class);
        wrapperKey = skipWrapperObject.value();
        JavaType collectionType = property.getType();
        JavaType collectedType = collectionType.containedType(0);
        wrappedType = collectedType.getRawClass();
        return this;
    }

    @Override
    public Object deserialize(JsonParser parser, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode objectNode = mapper.readTree(parser);
        JsonNode wrapped = objectNode.get(wrapperKey);
        Object mapped = mapIntoObject(wrapped);
        return mapped;
    }

    private Object mapIntoObject(JsonNode node) throws IOException,
            JsonProcessingException {
        JsonParser parser = node.traverse();
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(parser, wrappedType);
    }
}
mapIntoObject(JsonNode)
readValue
JsonNode
traverse()
ObjectMapper.convertValue()
mapIntoObject
return mapper.convertValue(node, wrappedType);

java - Jackson - How to process (deserialize) nested JSON? - Stack Ove...

java json serialization jackson
Rectangle 27 10

This guy calls out to bash to get the PID. I'm not sure if there is an java solution to the problem.

/**
 * Gets a string representing the pid of this program - Java VM
 */
public static String getPid() throws IOException,InterruptedException {

  Vector<String> commands=new Vector<String>();
  commands.add("/bin/bash");
  commands.add("-c");
  commands.add("echo $PPID");
  ProcessBuilder pb=new ProcessBuilder(commands);

  Process pr=pb.start();
  pr.waitFor();
  if (pr.exitValue()==0) {
    BufferedReader outReader=new BufferedReader(new InputStreamReader(pr.getInputStream()));
    return outReader.readLine().trim();
  } else {
    System.out.println("Error while getting PID");
    return "";
  }
}

I think this will give the pid of the JVM process... not the process spawned by Java which I believe is what the question is asking.

I think you need to read a bit closer. It spawns a process thats only job is to echo its own PID to its stdout which itself is piping to the stdin file descriptor associated with the JVM.

Vectors are old-hat, and is synchronized - no real need to use that type of collection - try using an ArrayList instead. Just saying.

Just saying what? This is not germane to the problem. The question is about accessing a PID not java best practices.

How do I find the process ID (pid) of a process started in java? - Sta...

java process pid
Rectangle 27 20

Your data is problematic in that you have inner wrapper objects in your array. Presumably your Vendor object is designed to handle id, name, company_id, but each of those multiple objects are also wrapped in an object with a single property vendor.

I'm assuming that you're using the Jackson Data Binding model.

The first is using a special Jackson config property. Jackson - since 1.9 I believe, this may not be available if you're using an old version of Jackson - provides UNWRAP_ROOT_VALUE. It's designed for cases where your results are wrapped in a top-level single-property object that you want to discard.

objectMapper.configure(SerializationConfig.Feature.UNWRAP_ROOT_VALUE, true);

The second is using wrapper objects. Even after discarding the outer wrapper object you still have the problem of your Vendor objects being wrapped in a single-property object. Use a wrapper to get around this:

class VendorWrapper
{
    Vendor vendor;

    // gettors, settors for vendor if you need them
}

Similarly, instead of using UNWRAP_ROOT_VALUES, you could also define a wrapper class to handle the outer object. Assuming that you have correct Vendor, VendorWrapper object, you can define:

class VendorsWrapper
{
    List<VendorWrapper> vendors = new ArrayList<VendorWrapper>();

    // gettors, settors for vendors if you need them
}

// in your deserialization code:
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readValue(jsonInput, VendorsWrapper.class);
VendorsWrapper:
    vendors:
    [
        VendorWrapper
            vendor: Vendor,
        VendorWrapper:
            vendor: Vendor,
        ...
    ]

Finally, you might use the Jackson Tree Model to parse this into JsonNodes, discarding the outer node, and for each JsonNode in the ArrayNode, calling:

mapper.readValue(node.get("vendor").getTextValue(), Vendor.class);

That might result in less code, but it seems no less clumsy than using two wrappers.

Thank you, this is how I was doing it I just hoped there was a better way. I'll mark it as correct.

I'd also like to see a solution using something like UNWRAP_ROOT_VALUES, only 1 level deeper, but I don't think it's possible. Another option of course is to use a custom deserializer, and just add hooks to look for the actual objects you're interested in and discard everything else, or to use the Jackson Tree Model approach, throw away the top-level JsonNode, and take the JsonNodes that wrap your Vendors, call getTextValue, and pass that to mapper.readValue. Jackson really gives you a surfeit of options.

java - Jackson - How to process (deserialize) nested JSON? - Stack Ove...

java json serialization jackson
Rectangle 27 26865

As hinted from above, the culprit is this if-statement:

Consider a railroad junction:

Now for the sake of argument, suppose this is back in the 1800s - before long distance or radio communication.

You are the operator of a junction and you hear a train coming. You have no idea which way it is supposed to go. You stop the train to ask the driver which direction they want. And then you set the switch appropriately.

  • If you guessed wrong, the captain will stop, back up, and yell at you to flip the switch. Then it can restart down the other path.

Consider an if-statement: At the processor level, it is a branch instruction:

You are a processor and you see a branch. You have no idea which way it will go. What do you do? You halt execution and wait until the previous instructions are complete. Then you continue down the correct path.

Is there a better way? You guess which direction the branch will go!

  • If you guessed wrong, you need to flush the pipeline and roll back to the branch. Then you can restart down the other path.

If you guess right every time, the execution will never have to stop.If you guess wrong too often, you spend a lot of time stalling, rolling back, and restarting.

This is branch prediction. I admit it's not the best analogy since the train could just signal the direction with a flag. But in computers, the processor doesn't know which direction a branch will go until the last moment.

So how would you strategically guess to minimize the number of times that the train must back up and go down the other path? You look at the past history! If the train goes left 99% of the time, then you guess left. If it alternates, then you alternate your guesses. If it goes one way every 3 times, you guess the same...

In other words, you try to identify a pattern and follow it. This is more or less how branch predictors work.

Most applications have well-behaved branches. So modern branch predictors will typically achieve >90% hit rates. But when faced with unpredictable branches with no recognizable patterns, branch predictors are virtually useless.

Notice that the data is evenly distributed between 0 and 255. When the data is sorted, roughly the first half of the iterations will not enter the if-statement. After that, they will all enter the if-statement.

This is very friendly to the branch predictor since the branch consecutively goes the same direction many times. Even a simple saturating counter will correctly predict the branch except for the few iterations after it switches direction.

T = branch taken
N = branch not taken

data[] = 0, 1, 2, 3, 4, ... 126, 127, 128, 129, 130, ... 250, 251, 252, ...
branch = N  N  N  N  N  ...   N    N    T    T    T  ...   T    T    T  ...

       = NNNNNNNNNNNN ... NNNNNNNTTTTTTTTT ... TTTTTTTTTT  (easy to predict)

However, when the data is completely random, the branch predictor is rendered useless because it can't predict random data. Thus there will probably be around 50% misprediction. (no better than random guessing)

data[] = 226, 185, 125, 158, 198, 144, 217, 79, 202, 118,  14, 150, 177, 182, 133, ...
branch =   T,   T,   N,   T,   T,   T,   T,  N,   T,   N,   N,   T,   T,   T,   N  ...

       = TTNTTTTNTNNTTTN ...   (completely random - hard to predict)

So what can be done?

If the compiler isn't able to optimize the branch into a conditional move, you can try some hacks if you are willing to sacrifice readability for performance.

if (data[c] >= 128)
    sum += data[c];
int t = (data[c] - 128) >> 31;
sum += ~t & data[c];

This eliminates the branch and replaces it with some bitwise operations.

data[]

C++ - Visual Studio 2010 - x64 Release

Java - Netbeans 7.1.1 JDK 7 - x64

//  Branch - Random
seconds = 10.93293813

//  Branch - Sorted
seconds = 5.643797077

//  Branchless - Random
seconds = 3.113581453

//  Branchless - Sorted
seconds = 3.186068823
  • With the Branch: There is a huge difference between the sorted and unsorted data.
  • With the Hack: There is no difference between sorted and unsorted data.
  • In the C++ case, the hack is actually a tad slower than with the branch when the data is sorted.

A general rule of thumb is to avoid data-dependent branching in critical loops. (such as in this example)

This goes to show that even mature modern compilers can vary wildly in their ability to optimize code...

@Mysticial To avoid the shifting hack you could write something like int t=-((data[c]>=128)) to generate the mask. This should be faster too. Would be interesting to know if the compiler is clever enough to insert a conditional move or not.

@phonetagger Take a look at this followup question: stackoverflow.com/questions/11276291/ The Intel Compiler came pretty close to completely getting rid of the outer loop.

@Novelocrat Only half of that is correct. Shifting a 1 into the sign-bit when it is zero is indeed UB. That's because it's signed integer overflow. But shifting a 1 out of the sign-bit is IB. Right-shifting a negative signed integer is IB. You can go into the argument that that C/C++ doesn't require that the top bit be the sign indicator. But implementation details are IB.

@Mysticial Thanks so much for the link. It looks promising. I will go though it. One last request. Sorry, but please don't mind, could you tell me how you could do this int t = (data[c] - 128) >> 31; sum += ~t & data[c]; to replace the original if-condition above?

The grammar in me wants me to think this should read "... victim of branch prediction failure" rather than just "... victim of branch prediction fail."

java - Why is it faster to process a sorted array than an unsorted arr...

java c++ performance optimization branch-prediction
Rectangle 27 26332

As hinted from above, the culprit is this if-statement:

Consider a railroad junction:

Now for the sake of argument, suppose this is back in the 1800s - before long distance or radio communication.

You are the operator of a junction and you hear a train coming. You have no idea which way it is supposed to go. You stop the train to ask the driver which direction they want. And then you set the switch appropriately.

  • If you guessed wrong, the captain will stop, back up, and yell at you to flip the switch. Then it can restart down the other path.

Consider an if-statement: At the processor level, it is a branch instruction:

You are a processor and you see a branch. You have no idea which way it will go. What do you do? You halt execution and wait until the previous instructions are complete. Then you continue down the correct path.

Is there a better way? You guess which direction the branch will go!

  • If you guessed wrong, you need to flush the pipeline and roll back to the branch. Then you can restart down the other path.

If you guess right every time, the execution will never have to stop.If you guess wrong too often, you spend a lot of time stalling, rolling back, and restarting.

This is branch prediction. I admit it's not the best analogy since the train could just signal the direction with a flag. But in computers, the processor doesn't know which direction a branch will go until the last moment.

So how would you strategically guess to minimize the number of times that the train must back up and go down the other path? You look at the past history! If the train goes left 99% of the time, then you guess left. If it alternates, then you alternate your guesses. If it goes one way every 3 times, you guess the same...

In other words, you try to identify a pattern and follow it. This is more or less how branch predictors work.

Most applications have well-behaved branches. So modern branch predictors will typically achieve >90% hit rates. But when faced with unpredictable branches with no recognizable patterns, branch predictors are virtually useless.

Notice that the data is evenly distributed between 0 and 255. When the data is sorted, roughly the first half of the iterations will not enter the if-statement. After that, they will all enter the if-statement.

This is very friendly to the branch predictor since the branch consecutively goes the same direction many times. Even a simple saturating counter will correctly predict the branch except for the few iterations after it switches direction.

T = branch taken
N = branch not taken

data[] = 0, 1, 2, 3, 4, ... 126, 127, 128, 129, 130, ... 250, 251, 252, ...
branch = N  N  N  N  N  ...   N    N    T    T    T  ...   T    T    T  ...

       = NNNNNNNNNNNN ... NNNNNNNTTTTTTTTT ... TTTTTTTTTT  (easy to predict)

However, when the data is completely random, the branch predictor is rendered useless because it can't predict random data. Thus there will probably be around 50% misprediction. (no better than random guessing)

data[] = 226, 185, 125, 158, 198, 144, 217, 79, 202, 118,  14, 150, 177, 182, 133, ...
branch =   T,   T,   N,   T,   T,   T,   T,  N,   T,   N,   N,   T,   T,   T,   N  ...

       = TTNTTTTNTNNTTTN ...   (completely random - hard to predict)

So what can be done?

If the compiler isn't able to optimize the branch into a conditional move, you can try some hacks if you are willing to sacrifice readability for performance.

if (data[c] >= 128)
    sum += data[c];
int t = (data[c] - 128) >> 31;
sum += ~t & data[c];

This eliminates the branch and replaces it with some bitwise operations.

data[]

C++ - Visual Studio 2010 - x64 Release

Java - Netbeans 7.1.1 JDK 7 - x64

//  Branch - Random
seconds = 10.93293813

//  Branch - Sorted
seconds = 5.643797077

//  Branchless - Random
seconds = 3.113581453

//  Branchless - Sorted
seconds = 3.186068823
  • With the Branch: There is a huge difference between the sorted and unsorted data.
  • With the Hack: There is no difference between sorted and unsorted data.
  • In the C++ case, the hack is actually a tad slower than with the branch when the data is sorted.

A general rule of thumb is to avoid data-dependent branching in critical loops. (such as in this example)

This goes to show that even mature modern compilers can vary wildly in their ability to optimize code...

@Mysticial To avoid the shifting hack you could write something like int t=-((data[c]>=128)) to generate the mask. This should be faster too. Would be interesting to know if the compiler is clever enough to insert a conditional move or not.

@phonetagger Take a look at this followup question: stackoverflow.com/questions/11276291/ The Intel Compiler came pretty close to completely getting rid of the outer loop.

@Novelocrat Only half of that is correct. Shifting a 1 into the sign-bit when it is zero is indeed UB. That's because it's signed integer overflow. But shifting a 1 out of the sign-bit is IB. Right-shifting a negative signed integer is IB. You can go into the argument that that C/C++ doesn't require that the top bit be the sign indicator. But implementation details are IB.

@Mysticial Thanks so much for the link. It looks promising. I will go though it. One last request. Sorry, but please don't mind, could you tell me how you could do this int t = (data[c] - 128) >> 31; sum += ~t & data[c]; to replace the original if-condition above?

The grammar in me wants me to think this should read "... victim of branch prediction failure" rather than just "... victim of branch prediction fail."

java - Why is it faster to process a sorted array than an unsorted arr...

java c++ performance optimization branch-prediction
Rectangle 27 26306

As hinted from above, the culprit is this if-statement:

Consider a railroad junction:

Now for the sake of argument, suppose this is back in the 1800s - before long distance or radio communication.

You are the operator of a junction and you hear a train coming. You have no idea which way it is supposed to go. You stop the train to ask the driver which direction they want to go. And then you set the switch appropriately.

  • If you guessed wrong, the captain will stop, back up, and yell at you to flip the switch. Then it can restart down the other path.

Consider an if-statement: At the processor level, it is a branch instruction:

You are a processor and you see a branch. You have no idea which way it will go. What do you do? You halt execution and wait until the previous instructions are complete. Then you continue down the correct path.

Is there a better way? You guess which direction the branch will go!

  • If you guessed wrong, you need to flush the pipeline and roll back to the branch. Then you can restart down the other path.

If you guess right every time, the execution will never have to stop.If you guess wrong too often, you spend a lot of time stalling, rolling back, and restarting.

This is branch prediction. I admit it's not the best analogy since the train could just signal the direction with a flag. But in computers, the processor doesn't know which direction a branch will go until the last moment.

So how would you strategically guess to minimize the number of times that the train must back up and go down the other path? You look at the past history! If the train goes left 99% of the time, then you guess left. If it alternates, then you alternate your guesses. If it goes one way every 3 times, you guess the same...

In other words, you try to identify a pattern and follow it. This is more or less how branch predictors work.

Most applications have well-behaved branches. So modern branch predictors will typically achieve >90% hit rates. But when faced with unpredictable branches with no recognizable patterns, branch predictors are virtually useless.

Notice that the data is evenly distributed between 0 and 255. When the data is sorted, roughly the first half of the iterations will not enter the if-statement. After that, they will all enter the if-statement.

This is very friendly to the branch predictor since the branch consecutively goes the same direction many times. Even a simple saturating counter will correctly predict the branch except for the few iterations after it switches direction.

T = branch taken
N = branch not taken

data[] = 0, 1, 2, 3, 4, ... 126, 127, 128, 129, 130, ... 250, 251, 252, ...
branch = N  N  N  N  N  ...   N    N    T    T    T  ...   T    T    T  ...

       = NNNNNNNNNNNN ... NNNNNNNTTTTTTTTT ... TTTTTTTTTT  (easy to predict)

However, when the data is completely random, the branch predictor is rendered useless because it can't predict random data. Thus there will probably be around 50% misprediction. (no better than random guessing)

data[] = 226, 185, 125, 158, 198, 144, 217, 79, 202, 118,  14, 150, 177, 182, 133, ...
branch =   T,   T,   N,   T,   T,   T,   T,  N,   T,   N,   N,   T,   T,   T,   N  ...

       = TTNTTTTNTNNTTTN ...   (completely random - hard to predict)

So what can be done?

If the compiler isn't able to optimize the branch into a conditional move, you can try some hacks if you are willing to sacrifice readability for performance.

if (data[c] >= 128)
    sum += data[c];
int t = (data[c] - 128) >> 31;
sum += ~t & data[c];

This eliminates the branch and replaces it with some bitwise operations.

data[]

C++ - Visual Studio 2010 - x64 Release

Java - Netbeans 7.1.1 JDK 7 - x64

//  Branch - Random
seconds = 10.93293813

//  Branch - Sorted
seconds = 5.643797077

//  Branchless - Random
seconds = 3.113581453

//  Branchless - Sorted
seconds = 3.186068823
  • With the Branch: There is a huge difference between the sorted and unsorted data.
  • With the Hack: There is no difference between sorted and unsorted data.
  • In the C++ case, the hack is actually a tad slower than with the branch when the data is sorted.

A general rule of thumb is to avoid data-dependent branching in critical loops. (such as in this example)

This goes to show that even mature modern compilers can vary wildly in their ability to optimize code...

@Mysticial To avoid the shifting hack you could write something like int t=-((data[c]>=128)) to generate the mask. This should be faster too. Would be interesting to know if the compiler is clever enough to insert a conditional move or not.

@phonetagger Take a look at this followup question: stackoverflow.com/questions/11276291/ The Intel Compiler came pretty close to completely getting rid of the outer loop.

@Novelocrat Only half of that is correct. Shifting a 1 into the sign-bit when it is zero is indeed UB. That's because it's signed integer overflow. But shifting a 1 out of the sign-bit is IB. Right-shifting a negative signed integer is IB. You can go into the argument that that C/C++ doesn't require that the top bit be the sign indicator. But implementation details are IB.

@Mysticial Thanks so much for the link. It looks promising. I will go though it. One last request. Sorry, but please don't mind, could you tell me how you could do this int t = (data[c] - 128) >> 31; sum += ~t & data[c]; to replace the original if-condition above?

The grammar in me wants me to think this should read "... victim of branch prediction failure" rather than just "... victim of branch prediction fail."

java - Why is it faster to process a sorted array than an unsorted arr...

java c++ performance optimization branch-prediction
Rectangle 27 85

The clue is that you compare Doubles (objects), not doubles (primitives). When you allocate objects in one thread, they are typically allocated sequentially in memory. So when indexOf scans a list, it goes through sequential memory addresses. This is good for CPU cache prefetching heuristics.

But after you sort the list, you still have to do the same number of memory lookups in average, but this time memory access will be in random order.

Here is the benchmark to prove that the order of allocated objects matters.

Benchmark            (generator)  (length)  (postprocess)  Mode  Cnt  Score   Error  Units
ListIndexOf.indexOf       random   1000000           none  avgt   10  1,243  0,031  ms/op
ListIndexOf.indexOf       random   1000000           sort  avgt   10  6,496  0,456  ms/op
ListIndexOf.indexOf       random   1000000        shuffle  avgt   10  6,485  0,412  ms/op
ListIndexOf.indexOf   sequential   1000000           none  avgt   10  1,249  0,053  ms/op
ListIndexOf.indexOf   sequential   1000000           sort  avgt   10  1,247  0,037  ms/op
ListIndexOf.indexOf   sequential   1000000        shuffle  avgt   10  6,579  0,448  ms/op

If this is true, shuffling instead of sorting should produce the same result

@DavidSoroko Full benchmark results with unsorted, shuffled, sorted and sorted contiguous at the bottom of the benchmark code.

Just to emphasize, in list.indexOf(list.get(index)) the list.get(index) doesn't benefit in any way from prefetching since index is random. The price of list.get(index) is the same regardless of weather the list was sorted or not. Prefetching kicks in only for list.indexOf()

performance - Why is processing a sorted array *slower* than an unsort...

java performance arraylist
Rectangle 27 7

Similar to the other tools mentioned, there is the jps command line tool that comes with the Java runtime. It spits out the PIDs of all running JVMs. The benefit is the output one needs to parse is confined to only the JVM processes.

How do I find the process ID (pid) of a process started in java? - Sta...

java process pid
Rectangle 27 23

I used the following hack at the end of my main method to get around the problem:

if (Boolean.parseBoolean(System.getenv("RUNNING_IN_ECLIPSE"))) {
    System.out.println("You're using Eclipse; click in this console and " +
            "press ENTER to call System.exit() and run the shutdown routine.");
    try {
        System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.exit(0);
}

Note: You must manually add the environment variable "RUNNING_IN_ECLIPSE" in the run configuration, and set the value to TRUE.

Or you can just do lines without if/else for the purpose of debugging only.

This solution doesn't work if we want to test "shutdown" on a console app that is running halfway.

java - How to get shutdown hook to execute on a process launched from ...

java eclipse shutdown
Rectangle 27 3364

Does branch prediction work better on sorted arrays vs. arrays with different patterns? For example, for the array --> { 10, 5, 20, 10, 40, 20, ... } the next element in the array from the pattern is 80. Would this kind of array be sped up by branch prediction in which the next element is 80 here if the pattern is followed? Or does it usually only help with sorted arrays?

So basically everything I conventionally learned about big-O is out of the window? Better to incur a sorting cost than a branching cost?

@AgrimPathak That depends. For not too large input, an algorithm with higher complexity is faster than an algorithm with lower complexity when the constants are smaller for the algorithm with higher complexity. Where the break-even point is can be hard to predict. Also, compare this, locality is important. Big-O is important, but it is not the sole criterion for performance.

When does branch prediction takes place? When does language will know that array is sorted? I'm thinking of situation of array that looks like: [1,2,3,4,5,...998,999,1000, 3, 10001, 10002] ? will this obscure 3 increase running time? Will it be as long as unsorted array?

@FilipBartuzi Branch prediction takes place in the processor, below the language level (but the language may offer ways to tell the compiler what's likely, so the compiler can emit code suited to that). In your example, the out-of-order 3 will lead to a branch-misprediction (for appropriate conditions, where 3 gives a different result than 1000), and thus processing that array will likely take a couple dozen or hundred nanoseconds longer than a sorted array would, hardly ever noticeable. What costs time is i high rate of mispredictions, one misprediction per 1000 isn't much.

java - Why is it faster to process a sorted array than an unsorted arr...

java c++ performance optimization branch-prediction
Rectangle 27 3274

Does branch prediction work better on sorted arrays vs. arrays with different patterns? For example, for the array --> { 10, 5, 20, 10, 40, 20, ... } the next element in the array from the pattern is 80. Would this kind of array be sped up by branch prediction in which the next element is 80 here if the pattern is followed? Or does it usually only help with sorted arrays?

So basically everything I conventionally learned about big-O is out of the window? Better to incur a sorting cost than a branching cost?

@AgrimPathak That depends. For not too large input, an algorithm with higher complexity is faster than an algorithm with lower complexity when the constants are smaller for the algorithm with higher complexity. Where the break-even point is can be hard to predict. Also, compare this, locality is important. Big-O is important, but it is not the sole criterion for performance.

When does branch prediction takes place? When does language will know that array is sorted? I'm thinking of situation of array that looks like: [1,2,3,4,5,...998,999,1000, 3, 10001, 10002] ? will this obscure 3 increase running time? Will it be as long as unsorted array?

@FilipBartuzi Branch prediction takes place in the processor, below the language level (but the language may offer ways to tell the compiler what's likely, so the compiler can emit code suited to that). In your example, the out-of-order 3 will lead to a branch-misprediction (for appropriate conditions, where 3 gives a different result than 1000), and thus processing that array will likely take a couple dozen or hundred nanoseconds longer than a sorted array would, hardly ever noticeable. What costs time is i high rate of mispredictions, one misprediction per 1000 isn't much.

java - Why is it faster to process a sorted array than an unsorted arr...

java c++ performance optimization branch-prediction
Rectangle 27 3268

Does branch prediction work better on sorted arrays vs. arrays with different patterns? For example, for the array --> { 10, 5, 20, 10, 40, 20, ... } the next element in the array from the pattern is 80. Would this kind of array be sped up by branch prediction in which the next element is 80 here if the pattern is followed? Or does it usually only help with sorted arrays?

So basically everything I conventionally learned about big-O is out of the window? Better to incur a sorting cost than a branching cost?

@AgrimPathak That depends. For not too large input, an algorithm with higher complexity is faster than an algorithm with lower complexity when the constants are smaller for the algorithm with higher complexity. Where the break-even point is can be hard to predict. Also, compare this, locality is important. Big-O is important, but it is not the sole criterion for performance.

When does branch prediction takes place? When does language will know that array is sorted? I'm thinking of situation of array that looks like: [1,2,3,4,5,...998,999,1000, 3, 10001, 10002] ? will this obscure 3 increase running time? Will it be as long as unsorted array?

@FilipBartuzi Branch prediction takes place in the processor, below the language level (but the language may offer ways to tell the compiler what's likely, so the compiler can emit code suited to that). In your example, the out-of-order 3 will lead to a branch-misprediction (for appropriate conditions, where 3 gives a different result than 1000), and thus processing that array will likely take a couple dozen or hundred nanoseconds longer than a sorted array would, hardly ever noticeable. What costs time is i high rate of mispredictions, one misprediction per 1000 isn't much.

java - Why is it faster to process a sorted array than an unsorted arr...

java c++ performance optimization branch-prediction
Rectangle 27 2719

The reason why performance improves drastically when the data is sorted is that the branch prediction penalty is removed, as explained beautifully in Mysticial's answer.

if (data[c] >= 128)
    sum += data[c];

we can find that the meaning of this particular if... else... branch is to add something when a condition is satisfied. This type of branch can be easily transformed into a conditional move statement, which would be compiled into a conditional move instruction: cmovl, in an x86 system. The branch and thus the potential branch prediction penalty is removed.

In C, thus C++, the statement, which would compile directly (without any optimization) into the conditional move instruction in x86, is the ternary operator ... ? ... : .... So we rewrite the above statement into an equivalent one:

sum += data[c] >=128 ? data[c] : 0;

While maintaining readability, we can check the speedup factor.

On an Intel Core i7-2600K @ 3.4GHz and Visual Studio 2010 Release Mode, the benchmark is (format copied from Mysticial):

//  Branch - Random
seconds = 11.302

//  Branch - Sorted
 seconds = 1.830

//  Branchless - Random
seconds = 2.736

//  Branchless - Sorted
seconds = 2.737

The result is robust in multiple tests. We get a great speedup when the branch result is unpredictable, but we suffer a little bit when it is predictable. In fact, when using a conditional move, the performance is the same regardless of the data pattern.

Now let's look more closely by investigating the x86 assembly they generate. For simplicity, we use two functions max1 and max2.

max1 uses the conditional branch if... else ...:

int max1(int a, int b) {
    if (a > b)
        return a;
    else
        return b;
}
max2
... ? ... : ...
int max2(int a, int b) {
    return a > b ? a : b;
}

On a x86-64 machine, GCC -S generates the assembly below.

:max1
    movl    %edi, -4(%rbp)
    movl    %esi, -8(%rbp)
    movl    -4(%rbp), %eax
    cmpl    -8(%rbp), %eax
    jle     .L2
    movl    -4(%rbp), %eax
    movl    %eax, -12(%rbp)
    jmp     .L4
.L2:
    movl    -8(%rbp), %eax
    movl    %eax, -12(%rbp)
.L4:
    movl    -12(%rbp), %eax
    leave
    ret

:max2
    movl    %edi, -4(%rbp)
    movl    %esi, -8(%rbp)
    movl    -4(%rbp), %eax
    cmpl    %eax, -8(%rbp)
    cmovge  -8(%rbp), %eax
    leave
    ret

max2 uses much less code due to the usage of instruction cmovge. But the real gain is that max2 does not involve branch jumps, jmp, which would have a significant performance penalty if the predicted result is not right.

So why does a conditional move perform better?

In a typical x86 processor, the execution of an instruction is divided into several stages. Roughly, we have different hardware to deal with different stages. So we do not have to wait for one instruction to finish to start a new one. This is called pipelining.

In a branch case, the following instruction is determined by the preceding one, so we cannot do pipelining. We have to either wait or predict.

In a conditional move case, the execution conditional move instruction is divided into several stages, but the earlier stages like Fetch and Decode does not depend on the result of the previous instruction; only latter stages need the result. Thus, we wait a fraction of one instruction's execution time. This is why the conditional move version is slower than the branch when prediction is easy.

The book Computer Systems: A Programmer's Perspective, second edition explains this in detail. You can check Section 3.6.6 for Conditional Move Instructions, entire Chapter 4 for Processor Architecture, and Section 5.11.2 for a special treatment for Branch Prediction and Misprediction Penalties.

Sometimes, some modern compilers can optimize our code to assembly with better performance, sometimes some compilers can't (the code in question is using Visual Studio's native compiler). Knowing the performance difference between branch and conditional move when unpredictable can help us write code with better performance when the scenario gets so complex that the compiler can not optimize them automatically.

There's no default optimization level unless you add -O to your GCC command lines. (And you can't have a worst english than mine ;)

I find it hard to believe that the compiler can optimize the ternary-operator better than it can the equivalent if-statement. You've shown that GCC optimizes the ternary-operator to a conditional move; you haven't shown that it doesn't do exactly the same thing for the if-statement. In fact, according to Mystical above, GCC does optimize the if-statement to a conditional move, which would make this answer completely incorrect.

@WiSaGaN The code demonstrates nothing, because your two pieces of code compile to the same machine code. It's critically important that people don't get the idea that somehow the if statement in your example is different from the terenary in your example. It's true that you own up to the similarity in your last paragraph, but that doesn't erase the fact that the rest of the example is harmful.

@WiSaGaN My downvote would definitely turn into an upvote if you modified your answer to remove the misleading -O0 example and to show the difference in optimized asm on your two testcases.

@UpAndAdam At the moment of the test, VS2010 can't optimize the original branch into a conditional move even when specifying high optimization level, while gcc can.

java - Why is it faster to process a sorted array than an unsorted arr...

java c++ performance optimization branch-prediction