Possible bug: GetMagickInfo is really slow in 6.9.9

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
Jackie Lee
Posts: 5
Joined: 2017-09-11T00:20:13-07:00
Authentication code: 1151

Possible bug: GetMagickInfo is really slow in 6.9.9

Post by Jackie Lee »

I go on my test and find that IM6.9.9 is much slower than IM6.7.2. I do nothing but read a jpeg image from memory and destory it with 7-threads.
It seems that IM6.9.9 would use much more time in GetMagickInfo() so that the Semaphore Lock would occupy more time. The following code would use almost 700% CPU when using 6.7.2 but only 140% when it is 6.9.9. I believe there must be something wrong when the magick_list SplayTree is used.

Code: Select all

#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string>
#include <pthread.h>
#include <wand/magick_wand.h>
void ReadJpgFile(const char *filename, unsigned char *&buff, size_t &size)
{
  std::ifstream infile;
  infile.open(filename);
  infile.seekg(0, std::ios::end);
  size = infile.tellg();
  infile.seekg(0, std::ios::beg);
  buff = new unsigned char[size];
  infile.read((char *)buff, size);
  infile.close();
}
void WriteJpgFile(const char *filename, unsigned char *buff, size_t size)
{
  std::ofstream fout; // create an ofstream object named fout
  fout.open(filename,std::ios::trunc);
  fout.write((char *)buff,size);
  fout.close();
}
void CompressJpgUsingImageMagick(unsigned char *orig_buff, unsigned char *&buff, size_t &img_size)
{
  MagickWand *im = NULL;
  im = NewMagickWand();
  MagickReadImageBlob(im, (const unsigned char *)orig_buff, img_size);
  //for(int i = 0; i < 5000000; i++);
  // MagickResetIterator(im);
  // MagickSetImageCompressionQuality(im, 50);
  // double factors420[3] = {2.0, 1.0, 1.0};
  // MagickSetSamplingFactors(im, 3, factors420);
  // //MagickSetInterlaceScheme(im, LineInterlace);
  // MagickSetImageFormat(im, "jpg");
  // MagickStripImage(im);
  // if(buff != NULL) free(buff);
  // img_size = 0;
  // buff = (unsigned char *)MagickGetImageBlob(im, &img_size);
  DestroyMagickWand(im);
}
void* WholeJpegCompress(void*)
{
  unsigned char *orig_buff = NULL;
  unsigned char *buff = NULL;
  size_t ori_img_size = 0;
  size_t img_size = 0;
  for(int i = 0; i < 1; i++)
  {
    ReadJpgFile("1.jpg",orig_buff,ori_img_size);
    for(int j = 0; j < 5000000; j++)
    {
      img_size = ori_img_size;
      if(buff != NULL)
      {
        free(buff);
        buff = NULL;
      }
      CompressJpgUsingImageMagick(orig_buff,buff,img_size);
    }
    //WriteJpgFile("2.jpg",buff,img_size);
    printf("outsize: %d\n",img_size);
    free(buff);
    buff = NULL;
    delete orig_buff;
    orig_buff = NULL;
  }
  pthread_exit(0);
  return NULL;
}
void* EmptyRun(void*)
{
  int i = 0;
  while(i < 1000000000)
  {
    int j = 0;
    while(j < 100) j++;
    i++;
  }
  pthread_exit(0);
}
int main()
{
  pthread_t deal_thr[7];
  for(int i = 0; i < 7; i ++)
  {
    int ret=pthread_create(&deal_thr[i],NULL,WholeJpegCompress,NULL);
    if(ret!=0)
    {
      printf("Create pthread error!\n");
      return -1;
    }
  }
  for(int i = 0; i < 7; i ++)
  {
    pthread_join(deal_thr[i],NULL);
  }
  return 0;
}
Jackie Lee
Posts: 5
Joined: 2017-09-11T00:20:13-07:00
Authentication code: 1151

Re: Possible bug: GetMagickInfo is really slow in 6.9.9

Post by Jackie Lee »

It seems that is not a bug. The IM 6.9.9 configured with "--enable-shared --with-modules" is much faster than "--enable-static". Now I want to know how to make the static IM as fast as shared one.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Possible bug: GetMagickInfo is really slow in 6.9.9

Post by dlemstra »

We just pushed some changes to our github repository to improve the performance of the GetMagickInfo() function for static builds. Could you give it another try with the beta that will be available sometime tomorrow or the latest code from our github repository now?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply