Wednesday, March 13, 2024

Heavy Memory Usage of the Regression Test in XCTest

While recently wrestling with a regression test for my Swift project using the XCTest framework, I ventured into the tumultuous world of a heavily multithreaded application. Ensuring the absence of timing issues was like defusing a bomb – one wrong wire, and boom! So, what's a developer to do? Run the code tens of thousands of times, of course (or even more for the marathon runners of applications), and keep your fingers crossed for any anomalies.

My trusty XCTest framework was the stage, and my test code, which cheerfully returned results asynchronously, demanded I master the classic XCTest dance of expectation(...) and waitForExpectations(...) moves. All seemed well as the test passed with flying colors for a few thousand iterations. However, as I ambitiously cranked up the numbers, strange gremlins began to pop up. The memory usage skyrocketed, adding a whopping 90 MB per 10,000 iterations – definitely not a feature I had intended to implement!

I dove into the depths of Instruments, yet no memory leaks waved back at me. Puzzling! Yet, I noticed waitForExpectations was having a bit too much fun allocating objects like NSString left and right. My online detective work on Google and StackOverflow turned up zilch – it seemed I was charting uncharted waters.

Refusing to be defeated, me and my digital buddy ChatGPT kept on going into the uncharted territory with me as novis to Swift while ChatGPT blind beyond 20 ft. I scrutinized the allocation patterns revealed by Instruments with a detective's eye. This careful observation led to a pivotal "Eureka!" moment. It dawned on me to employ an autoreleasepool within the scope of each iteration. This strategic move, akin to a well-crafted chess play, was not just a stroke of luck but a calculated decision based on the insights gleaned. And, like a charm, this adept adjustment worked wonders, effectively dispelling the memory bloat gremlins back into the abyss from whence they came.


Here is a sample test code anyone can run in our XCTest project, to reproduce the issue and verify the solution.

  1.     func testAsyncOperationInLoop() {
  2.         let iterationCount = 50000  // Number of iterations in the loop
  3.         
  4.         for i in 1...iterationCount {
  5.             autoreleasepool {
  6.                 
  7.                 // Create a new expectation for each iteration of the loop
  8.                 let expectation = self.expectation(description: "Async operation \(i)")
  9.                 
  10.                 if i > 0 && i % (10000) == 0 {
  11.                     print("breaking for a breath")
  12.                 }
  13.                 
  14.                 // Simulate an asynchronous operation
  15.                 DispatchQueue.global().asyncAfter(deadline: .now() + 1.0/100000) {
  16.                     expectation.fulfill()  // Fulfill the expectation once the operation is completed
  17.                 }
  18.                 
  19.                 // Wait for the expectation to be fulfilled, with a timeout
  20.                 waitForExpectations(timeout: 2) { error in
  21.                     if let error = error {
  22.                         XCTFail("waitForExpectations errored: \(error)")
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.         
  28.         print("All iterations completed")
  29.     }

Wednesday, February 07, 2024

Decoding the Mystery: How Food Label Decoder Simplifies Your Grocery Shopping

This blog is about a custom ChatGPT model that you can use if you have ChatGPT 4 account. Follow this link to use the custom model: https://chat.openai.com/g/g-c2AWb78Mr-food-label-decoder 


In today’s fast-paced world, our dependence on packaged foods has become more a necessity than a choice. Amidst the rush of daily life, who has the time to decode the complex list of ingredients on the back of every package? Yet, understanding these labels is crucial, as it's about more than just counting calories—it's about knowing what we're truly feeding ourselves and our loved ones.

The Hidden Truth Behind Packaged Foods

Packaged food items are a marvel of modern convenience, but this convenience often comes with a hidden cost. To stay competitive, ensure a long shelf life, and appeal to consumer tastes, companies often resort to using a range of additives, preservatives, and sugars. These ingredients can be cleverly disguised under scientific names, making them nearly impossible for the average shopper to recognize.

Moreover, with the ever-evolving "health trends," many products are marketed as "healthy" options, leveraging buzzwords like "natural," "organic," or "low-fat." However, a closer inspection of their ingredient lists often reveals a different story. How many times have you picked up a product, enticed by its health claims, only to find it packed with unpronounceable chemicals or excessive sugars?

Enter the Food Label Decoder

This is where the Food Label Decoder comes into play—a tool designed to bridge the gap between confusing labels and informed dietary choices. Powered by ChatGPT's Artificial Intelligence, it offers a simple yet revolutionary approach: just take a picture of the food label, share any specific dietary preferences or health concerns, and let the AI do the rest.
The Decoder breaks down the jargon into simple, understandable terms. It evaluates the major ingredients, sugars, preservatives, artificial components, potential allergens, and more. It even gives a health snapshot using emojis, making it easier to grasp the nutritional value at a glance.

Why Trust Food Label Decoder?

You might wonder, why trust an AI with something as personal as your dietary choices? The answer is simple: because it was created to solve real, everyday dilemmas—ones that I faced in my own life. Choosing between Product A and Product B, both claiming to be the healthier option, was a constant struggle due to the coded language of ingredients lists.

Food Label Decoder was born out of a need for transparency and simplicity. It cuts through the marketing noise, offering clear, unbiased information. It doesn't just tell you what's in your food; it helps you understand what that means for you, based on your unique health goals or dietary restrictions.

Making Informed Choices Has Never Been Easier

The beauty of the Food Label Decoder lies in its simplicity and accessibility. It empowers you to make informed decisions, turning the daunting task of reading labels into a quick, straightforward process. Whether you're navigating dietary restrictions, managing health conditions, or simply striving for a healthier lifestyle, the Decoder is your personal guide in the complex world of food shopping.

Act on Your Health

So, the next time you find yourself standing in the grocery aisle, perplexed by the list of ingredients on a product, remember that help is just a click away. The Food Label Decoder is more than just a tool; it's a movement towards a healthier, more informed society. It's time to take control of what we eat, one product at a time.

Why not start today? Take the first step towards becoming a more informed consumer and see the difference it makes in your shopping habits. After all, the best dietary choices are those made with confidence and clarity. Let's embark on this journey together, towards a healthier, happier you.