Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clipper2 slower than clipper1 #839

Open
Sachaaaaaa opened this issue May 16, 2024 · 2 comments
Open

Clipper2 slower than clipper1 #839

Sachaaaaaa opened this issue May 16, 2024 · 2 comments

Comments

@Sachaaaaaa
Copy link

Hi there,

I've been using Clipper for generating GDS files, and recently I migrated my code from Clipper 1 to Clipper 2. However, I've noticed a significant decrease in performance for certain operations, especially union.

Interestingly, when I perform random operations, Clipper 2 shows much better performance compared to Clipper 1. However, when I try to generate a "grid cover," which essentially creates a grid with numerous repetitions of a square shape, the performance seems to degrade significantly (Clipper2 two times slower than Clipper1).

grid cover :
Capture d’écran_2024-05-16_10-00-11

I've come across some discussions on this issue, but unfortunately, I haven't found a solution yet, even after trying optimization flags like -O3.

I'm hoping that the problem lies within my code and that there might be some optimizations that could be implemented to improve performance.

Any suggestions or insights would be greatly appreciated.

Clipper2 code :

    Paths64 subj, clip;
    PolyTree64 solution;

    Clipper64 c64;


    subj = makePathsFromFile("clip.txt");
    clip = makePathsFromFile("subj.txt");


    c64.AddSubject(subj);
    c64.AddClip(clip);


    auto start = std::chrono::steady_clock::now();

    c64.Execute(ClipType::Union, FillRule::NonZero, solution);

    auto end = std::chrono::steady_clock::now();
    std::chrono::duration<double> elapsed_seconds = end - start;
    std::cout << "Temps d'exécution : " << elapsed_seconds.count() << " secondes" << std::endl;
    

Clipper 1 :

    Paths subj, clip;
    PolyTree solution;

    Clipper c64;

    subj = makePathsFromFile("clip.txt");
    clip = makePathsFromFile("subj.txt");

    c64.AddPaths(subj, ptSubject, true);
    c64.AddPaths(clip, ptClip, true);

    auto start = std::chrono::steady_clock::now();

    c64.Execute(ClipType::ctUnion, solution, pftNonZero, pftNonZero);

    auto end = std::chrono::steady_clock::now();
    std::chrono::duration<double> elapsed_seconds = end - start;
    std::cout << "Temps d'exécution : " << elapsed_seconds.count() << " secondes" << std::endl;

code of makePathsFromFile for Clipper2 :
/!\ I know the following codes are very inefficient but it doesn't matter i just did that for testing purpose

Paths64 makePathsFromFile(const std::string& filename){
  
  Paths64 benchmark; 

  // Open the file
  std::ifstream infile(filename); 
  
  // Check if the file is not already in use
  if (!infile.is_open()) {
      std::cerr << "Erreur lors de l'ouverture du fichier." << std::endl;
  }

  int he=0;
  std::string line;
  std::vector<std::vector<int>> allValues;
  while (std::getline(infile, line)) { // Go across each line
      Paths64 path;
      std::istringstream iss(line);
      int x, y;
      char comma;
      std::vector<int> values;
      while (iss >> x >> comma >> y >> comma) { // Read the int x, y
          values.push_back(x);
          values.push_back(y);
      }

      values.push_back(x);
      values.push_back(y);   

      allValues.push_back(values);
  }


  // Close the file
  infile.close();

  for (const auto& values : allValues) {
      benchmark.push_back(MakePath(values)); 
  }
  

  return benchmark;
}

code of makePathsFromFile for Clipper1 :

Path makePath(const std::vector<int>& points) {
    Path path;
    for (size_t i = 0; i < points.size(); i += 2) {
        path.push_back(IntPoint(points[i], points[i + 1]));
    }
    return path;
}


Paths makePathsFromFile(const std::string& filename){
  
  Paths benchmark; 

  // Open the file
  std::ifstream infile(filename); 
  
  // Check if the file is not already in use
  if (!infile.is_open()) {
      std::cerr << "Erreur lors de l'ouverture du fichier." << std::endl;
  }

  int he=0;
  std::string line;
  std::vector<std::vector<int>> allValues;
  while (std::getline(infile, line)) { // Go across each line
      Paths path;
      std::istringstream iss(line);
      int x, y;
      char comma;
      std::vector<int> values;
      while (iss >> x >> comma >> y >> comma) { // Read the int x, y
          values.push_back(x);
          values.push_back(y);
      }

      values.push_back(x);
      values.push_back(y);   

      allValues.push_back(values);
  }


  // Close the file
  infile.close();

  for (const auto& values : allValues) {
      benchmark.push_back(makePath(values)); 
  }
  

  return benchmark;
}

And the data :
(Note that those Paths are not random it is real case scenario)
archiveCompressed.zip

@AngusJohnson
Copy link
Owner

AngusJohnson commented May 17, 2024

Hi Sasha.
Clipper2 is indeed a little slower that Clipper1 given your example above.
However, my measurements make it about 20% slower, not "two times" slower as you seem to be measuring.

Anyhow, I'm not entirely sure why Clipper2 is a little slower in this instance. I was initially presuming it was due to all the extra work Clipper2 does to ensure that "touching" polygons in clipping solutions are properly merged. (This was very incomplete in Clipper1). Unfortunately, disabling all this additional merging code doesn't seem to make much difference in Clipper2's performance.

ComparePerformanceWithClipper1.zip

@niki-sp
Copy link

niki-sp commented May 17, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants