Saturday, August 24, 2024

Singleton Pattern


One of the easiest models, used in Creational design pattern

What?

It is a design pattern that restricts the instantiation of a class to one object. 

This is useful when exactly one object is needed to coordinate actions across the system.

Why?

I can think of a few scenarios where the Singleton pattern is useful.

On usage of a shared resource, like a database connection, or a logger, Singleton pattern makes sure that it has one instance of that resource.

When?

When you need to have a single instance of a class, and you want to provide a global point of access to the object.

It can be instantiated in two ways, either lazy or eager.

1. Eager instantiation is when the object is created when the class is loaded.

2. Lazy instantiation is when the object is created only when it is needed.

<pre><code>

private static readonly Lazy<Singleton> _instance = 

            new Lazy<Singleton>(() => new Singleton());

</code></pre>

How?

Key components of Singleton pattern are:

1. Static member: 

Static member ensures that memory is allocated only once, preserving the single instance of the Singleton class.

<pre><code>

private static Singleton instance;

</code></pre>

2. Private constructor:

Private constructor ensures that the class cannot be instantiated from outside the class.

<pre><code>

private Singleton() {}

</code></pre>

3. Public static method:

Public static method provides a global point of access to the Singleton object.

<pre><code>

public static Singleton Instance

{

get

{

return _instance.Value;

}

}

</code></pre>

Where?

Code repository: DesignCreational/singleton at main · gsenthilvel/DesignCreational (github.com)


Friday, August 16, 2024

sarvam.ai


Sarvam-2B: India's first open-source small language model trained on 10 India languages (Hindi, Tamil, Telugu, Malayalam, Punjabi, Odia, Gujarati, Marathi, Kannada, and Bengali) & English

Founded by a team of passionate experts, they aim to lead transformative research in AI that will make development, deployment, and distribution of GenAI apps in India significantly robust, performant, and cost-effective.

With Sarvam's generative AI building blocks, enterprises can unlock new market opportunities, establish direct and deeper connect with their customers.

Demo website: https://www.sarvam.ai/

HF checkpoint sarvam-2b-v0.5: https://huggingface.co/sarvamai/sarvam-2b-v0.5


Friday, August 2, 2024

String Compare performance


In .NET code, "ToUpper" and "ToLower" methods can impact performance due to memory allocation, string copying, and potential garbage collection, especially in situations involving large strings or frequent conversions.

Performance-wise, string.Equals is faster than the above methods due to direct character comparison, avoiding memory allocation, and reducing overhead for case-insensitive string comparison.

Best approach is to leverage SringComparion.OrdinalIgnoreCase to compare strings using ordinal(binary) sort rules and ignoring the case of the strings being compared.