
Sonic Golang is a powerful tool that can help you optimize your Go applications for better performance. It achieves this by providing a caching layer that sits between your application and the database.
This caching layer can significantly reduce the number of database queries, which can be a major bottleneck in many applications. By caching frequently accessed data, you can improve the responsiveness and scalability of your application.
One of the key benefits of Sonic Golang is its ability to automatically detect and cache frequently accessed data. This means you don't have to manually configure caching for every single query, which can be a time-consuming and error-prone process.
Sonic Features
Sonic Go library is optimized for speeding up or slowing down speech, making it a great tool for various audio applications.
With its integration-friendly Go library, you can effortlessly use Sonic in your Go applications, making development a breeze.
Here are some key features of Sonic:
- Optimized algorithm for speeding up or slowing down speech.
- Integration-friendly Go library for effortless use in Go applications.
- Adaptation of the original Sonic library for enhanced compatibility with Go applications.
- Great for integration into text-to-speech applications and voice communication systems.
Sonic also boasts fast performance, making it suitable for demanding applications.
Features

Sonic is designed to be fast, with a focus on speed. It achieves this through runtime object binding without code generation, allowing for quick and efficient processing.
One of the key features of Sonic is its ability to manipulate JSON values with complete APIs. This makes it easy to work with JSON data and perform various operations on it.
Sonic's optional features can also improve its speed. By not sorting map keys by default, it saves 10% processing time. This is a smart choice, as it allows Sonic to stay fast for most common uses.
Here are some of Sonic's key features:
- Runtime object binding without code generation
- Complete APIs for JSON value manipulation
- Optimized algorithm for speeding up or slowing down speech
- Integration-friendly Go library for effortless use in Go applications
By making some features optional, Sonic stays fast for most common uses. This is a great advantage, as it allows developers to choose what features they need and use them without slowing down the performance.
Mismatched Types
Sonic is designed to handle mismatched types with ease. It reports a decoder.MismatchTypeError if it encounters a mismatch-typed value for a given key.

When decoding a JSON, Sonic will skip the wrong value and keep decoding the next JSON. This means that it won't get stuck on a single error and can continue to process the rest of the data.
Sonic provides an advanced API for fully parsing JSON into non-standard types without using any intermediate representation. This allows for more flexibility and customization in how you work with JSON data.
For example, you might have types that are similar to interface{} but not quite the same. Sonic can handle these types without requiring an intermediate representation.
Copy String
Copy String is a feature in Sonic that allows you to copy string values without any escaped characters, referencing them from the origin JSON buffer instead of mallocing a new buffer to copy. This helps a lot for CPU performance.
However, this approach may leave the whole JSON buffer in memory as long as the decoded objects are being used. In practice, we found the extra memory introduced by referring JSON buffer is usually 20% ~ 80% of decoded objects.

To avoid this issue, Sonic provides the option for Decode() / Unmarshal() users to choose not to reference the JSON buffer, which may cause a decline in CPU performance to some degree.
If you want to get json more quickly and not care about memory usage, you can use GetFromStringNoCopy() to return a JSON directly referenced from source.
Here are the key differences between GetFromString() and GetFromStringNoCopy():
- GetFromString(): For memory safety, Sonic now copies return JSON.
- GetFromStringNoCopy(): Returns a JSON directly referenced from source.
Performance and Optimization
Sonic GoLang is a high-performance JSON library that offers several features to optimize and speed up JSON processing. It uses a Just-In-Time (JIT) compilation approach, creating specialized code paths for specific JSON structures, which results in faster processing times.
This approach is similar to how a chef creates a special recipe for each dish, instead of following a generic recipe every time. By doing so, Sonic can eliminate reflection at runtime, reducing heap allocations and GC pressure.
Sonic also takes advantage of SIMD instructions, which allow the computer to perform multiple tasks at once, making JSON processing faster. This is similar to having multiple hands to do a task, like sorting cards one by one versus sorting multiple cards at once.
Here are some key benefits of Sonic's optimization techniques:
- Array's performance is much better than Map when Inserting (Deserialize) and Scanning (Serialize) data;
- Hashing (map[x]) is not as efficient as Indexing (array[x]), which ast.Node can conduct on both array and object;
- Using Interface()/Map() means Sonic must parse all the underlying values, while ast.Node can parse them on demand.
By making some features optional, Sonic can stay fast for most common uses, while still allowing users to turn them on if needed. For example, it doesn't sort map keys by default, which saves 10% processing time, and it doesn't escape HTML by default, which saves 15% processing time.
Better Performance
Array's performance is much better than Map when Inserting (Deserialize) and Scanning (Serialize) data.
Sonic's Just-In-Time (JIT) Compilation creates specialized code paths for your specific JSON structures, making it significantly faster than the regular Go JSON library.
Using Interface()/Map() means Sonic must parse all the underlying values, while ast.Node can parse them on demand, resulting in improved performance.
Sonic's lazy-load design doesn't ensure concurrent security directly, but you can call Node.Load()/Node.LoadAll() to achieve that, which may bring performance reduction while still working faster than converting to map or interface{}.
Here are some key benefits of Sonic's performance:
Sonic's SIMD instructions use special CPU features to do many tasks at once, speeding up basic JSON jobs like finding characters, strings, etc.
Making some features optional, like not sorting map keys by default (saves 10% processing time) and not escaping HTML by default (saves 15% processing time), helps Sonic stay fast for most common uses.
Sonic's efficient memory management minimizes memory allocations by avoiding unnecessary string copies and utilizing array-like structures for internal AST parsing, reducing pressure on Go's garbage collector.
Scale
Scaling is a crucial step in performance optimization, and it's worth noting that the Scale function can scale samples starting from a specified position with a given factor.
The Scale function is used in conjunction with AddSamples, which converts uint8 samples to int16 samples and adds them to the inputBuffer. This process can significantly impact performance, especially when dealing with large datasets.
In the context of the SampleBuffer, scaling is a critical operation that can greatly affect the overall performance of the system. The Scale function is designed to handle this task efficiently.
To give you a better idea, the Scale function is used to scale samples starting from a specified position with a given factor. This means that you can control exactly where and how much the scaling occurs.
Design and Usage

The Sonic Go Library can be used in two main modes: streaming and batch processing. Streaming mode allows you to continuously feed input data to a Sonic stream and read processed data in real-time.
You can use the library's function for batch processing to process audio data in chunks. This can be useful for applications that require predictable CPU usage.
The default configuration for a Sonic stream assumes no alterations to the sound stream, with speed, pitch, rate, and volume all set to 1.0. This means no change, and the library optimally handles this by directly copying input to output, minimizing CPU usage.
You can modify the sonic characteristics using the following functions:
- Set speed to double the speed of speech (2.0)
- Reduce pitch by about 5% (0.95)
- Multiply sound samples by 1.4 (1.4)
These parameters enable flexible adjustments, and you can change them at any time without having to flush or create a new sonic stream.
Marshal/Unmarshal
Marshal/Unmarshal is a crucial part of working with JSON data in Sonic.
Default behaviors are mostly consistent with encoding/json, but there are some key differences. Sonic doesn't follow the same rules for HTML escaping and SortKeys, which is not in conformity to RFC8259.
When you need to unmarshal JSON data, Sonic will handle it for you, but be aware that it doesn't follow the exact same rules as encoding/json.
Sonic's marshal behavior is also worth noting, especially when it comes to primitive objects like structs and maps. By default, these are encoded in compact-format JSON.
However, there are some exceptions to this rule. If you're working with json.RawMessage or json.Marshaler, Sonic will validate their output JSON but won't compact it for performance concerns.
Sort Keys
Sonic doesn't enable the feature to sort keys by default to avoid a 10% performance loss.
This decision was likely made to prioritize speed and efficiency in most common use cases.
Design
To achieve high performance in processing JSON data, the design of the system is crucial. The design is easy to implement, utilizing JIT tech to assemble opcodes corresponding to the schema at runtime, which is then cached into off-heap memory in the form of Golang functions.
This approach allows for the best adaptation to practical scenarios where big data and small data coexist. Pre-conditional judgment is used to combine SIMD with scalar instructions based on factors like string size and floating precision.
For core computational functions, C/Clang is used to write and compile the code, and a set of asm2asm tools is developed to translate the fully optimized x86 assembly into plan9 and finally load it into Golang runtime.
The lazy-load mechanism is used in the AST parser, reducing the overhead of multiple-key queries. This is done in a more adaptive and efficient way compared to traditional lazy-load approaches.
To further optimize the system, some native-asm functions are reimplemented as lightweight function-calls in JIT, as inlining these functions in Golang resulted in increased costs.
A high-performance and concurrent-safe cache is also implemented using open-addressing-hash + RCU tech, which is more suitable for quasi-static scenarios with fewer elements.
If you're working with large amounts of JSON data on AMD64 or ARM64 processors, consider the following design strategies:
- Use JIT tech to assemble opcodes at runtime
- Combine SIMD with scalar instructions based on data characteristics
- Use C/Clang for core computational functions and asm2asm tools for translation
- Implement a lazy-load mechanism in the AST parser
- Reimplement native-asm functions as lightweight function-calls in JIT
- Use a high-performance and concurrent-safe cache with open-addressing-hash + RCU tech
Usage

The Sonic Go Library is incredibly versatile, allowing you to use it in two main modes: streaming and batch processing.
In streaming mode, you can continuously feed the library with input data and read processed data in real-time. This is a basic example of how you can use the library in streaming mode.
You can also use the library's function for batch processing, which is useful for processing large amounts of data at once.
The default configuration for a Sonic stream assumes no alterations to the sound stream, with speed, pitch, rate, and volume all set to 1.0, meaning no change.
To modify the sonic characteristics, you can use the speed, pitch, rate, and volume parameters, represented as floating-point numbers. These parameters enable flexible adjustments.
Setting a speed of 2.0 doubles the speed of speech, while a pitch of 0.95 reduces pitch by about 5%. A volume of 1.4 multiplies sound samples by 1.4, with clipping if the maximum range of a 16-bit integer is exceeded.
You can change the speed, pitch, rate, and volume parameters at any time, without having to flush or create a new sonic stream. This makes it easy to experiment and find the perfect settings for your application.
Discover more: Golang Mode
Go Library
The Go Library is a great tool for speeding up or slowing down speech, and it's specifically optimized for significant speed changes, exceeding 2X.
The Sonic Go Library is a Golang implementation of the Sonic algorithm, which is a simple yet effective method for modifying speech rates.
One of the most useful functions in the Sonic Go Library is ChangeByteSpeed, which allows you to change the speed of speech by modifying the byte speed.
Here are some of the key functions and data types in the Sonic Go Library:
- ChangeByteSpeed: changes the byte speed of speech
- ChangeFloatSpeed: changes the float speed of speech
- ChangeSpeed: changes the speed of speech
- Buffer: a data type used to store audio data
- SampleBuffer: a data type used to store sample audio data
- Stream: a data type used to stream audio data
The Sonic Go Library is designed to be a seamless integration into text-to-speech and voice streaming applications, making it a great choice for developers who need to modify speech rates.
Search Option
The Search Option is a crucial aspect of the Searcher, allowing you to tailor your search experience to your specific needs.
You can choose to copy the result JSON string instead of referencing it from the input to reduce memory usage if you cache the results.

The Searcher also allows you to specify ConcurrentRead, which is essential if you want to read the ast.Node concurrently.
However, keep in mind that ast.Node uses a Lazy-Load design, so ConcurrentRead is not enabled by default.
You can also validate the entire JSON by specifying the ValidateJSON option, which is enabled by default but may slow down the search speed a little.
Here are the available SearchOption settings:
- CopyReturn: Copy the result JSON string instead of referencing it from the input.
- ConcurrentRead: Enable concurrent reading of ast.Node.
- ValidateJSON: Validate the entire JSON (enabled by default).
Compatibility
Sonic provides three integrated configs as sonic.API to meet different scenarios: ConfigDefault, ConfigStd, and ConfigFastest.
ConfigDefault is the sonic's default config, designed to run fast while ensuring security with settings like EscapeHTML=false and SortKeys=false.
The ConfigStd config is std-compatible, meaning it's suitable for most environments, with settings like EscapeHTML=true and SortKeys=true.
Sonic doesn't guarantee support for all environments, so if it's not supported, the implementation will fall back to encoding/json, effectively making it equal to ConfigStd.
Here's a quick rundown of the three configs:
Stream and Buffer Management

Streaming IO is a key feature of Sonic, allowing it to decode JSON from an input stream or encode objects into an output writer. This approach helps handle multiple values and reduces memory consumption.
Sonic's Stream type has two important methods: AddSamples and Flush. AddSamples adds int16 samples to the input buffer, while Flush forces the stream to generate output using the current data, with no extra delay added.
Flushing a stream in the middle of words can introduce distortion, so it's essential to use this method judiciously.
*Stream) AddFloatSamples
The *Stream) AddFloatSamples method is a powerful tool for managing streams and buffers. It converts float64 samples to int16 samples and adds them to the inputBuffer.
This method is particularly useful for handling floating-point data, which is often used in audio and scientific applications.
The number of elements in the input slice must be a multiple of the buffer's channel count, so be sure to plan your data accordingly.

For example, if your buffer has 4 channels, you'll need to pass in an input slice with a length that's a multiple of 4.
This method is also closely related to the *SampleBuffer) AddFloatSamples method, which scales float64 samples to the range of int16 and adds them to the SampleBuffer.
*SampleBuffer) AddByteSamples
The SampleBuffer class has a method called AddByteSamples that appends specified uint8 samples to the buffer. Each uint8 sample is converted to an int16 value and shifted to the range of int16.
To use AddByteSamples, the number of elements in the input slice must be a multiple of the buffer's channel count. This is a crucial detail to keep in mind when working with audio samples.
The AddByteSamples method is designed to handle uint8 samples, which are a common data type for audio data. By converting these samples to int16, the method ensures that the data is in a format that can be easily processed.

If you're working with a SampleBuffer that has a channel count of 2, for example, your input slice must have a length that is a multiple of 2. This ensures that the samples are properly aligned and can be processed correctly.
In practice, this means that if you're working with a SampleBuffer that has a channel count of 2, you can't pass in a slice with a length of 3. You need to make sure that the length of the slice is a multiple of the channel count.
The AddByteSamples method is a powerful tool for working with audio samples, but it requires careful attention to detail to use correctly. By following the rules for input slice length, you can ensure that your audio data is properly processed and aligned.
*Stream) GetPitch
The GetPitch function is a straightforward way to retrieve the current pitch of a stream. It's a simple yet essential tool for understanding the audio properties of your stream.

To use GetPitch, you'll need to call the function on a *Stream object, which will return the current pitch value. This value can be used for various purposes, such as adjusting the pitch of the stream or comparing it to a target pitch.
The GetPitch function is a complementary counterpart to the SetPitch function, which allows you to set the pitch of the stream. By using both functions together, you can dynamically adjust the pitch of your stream to suit your needs.
Stream Operations
Stream Operations are a crucial part of Sonic Golang, allowing for efficient and flexible data handling.
Sonic supports decoding JSON from an io.Reader, making it easy to work with multiple values and reduce memory consumption.
When writing data to a stream, you can choose from three different methods: Write, WriteBytes, and WriteFloats. Each method writes different types of samples to the stream: int16, uint8, and float64, respectively.
These methods process the data as they write it, making it a convenient and efficient way to handle data in your Golang applications.
Actions
Stream operations involve several key actions. One of the most important actions is creating a new sonic stream, which can be done with the NewSonicStream function.
This function creates a new sonic stream, allowing you to work with audio data in a more efficient way. You can use the NewSonicStream function to create a new stream with the desired properties.
Creating a new sonic stream is a fundamental step in many stream operations, and understanding how to do it is crucial for working with audio data.
MoveAllTo
The MoveAllTo operation is a powerful tool for stream operations. It allows you to move all audio samples from one SampleBuffer to another.
The destination SampleBuffer must have the same number of channels as the current SampleBuffer. This ensures that the audio samples are properly aligned.
If the current SampleBuffer is empty, the MoveAllTo operation has no effect. This is because there are no audio samples to move.
The MoveAllTo operation is a convenient way to transfer audio samples between buffers without having to manually handle the data.
ReadSlice

The ReadSlice function is a powerful tool for extracting audio samples from a SampleBuffer. It reads and returns a slice of audio samples based on the specified number of samples 'n' and the number of channels in the SampleBuffer.
The length of the resulting slice is determined by these two factors, making it a flexible and customizable option for stream operations.
To use ReadSlice, you simply need to specify the number of samples you want to read, and the function will take care of the rest.
GetSlice
GetSlice is a powerful operation that allows you to retrieve a portion of data from a buffer without removing it.
The GetSlice function is available in both Buffer and SampleBuffer types, and it gets the next n elements from the buffer without removing them. This is a key difference from other operations that might modify the original data.
For example, the GetSlice function in SampleBuffer gets and returns a slice of audio samples from the current SampleBuffer without removing them from the underlying Buffer. The length of the resulting slice is determined by the specified number of samples 'n' and the number of channels in the SampleBuffer.

In contrast, the GetSlice function in Buffer gets the next n elements from the buffer without removing them from a buffer, but it doesn't specify the type of data being retrieved. This means you'll need to check the type of your buffer to ensure you're getting the right data.
One thing to keep in mind is that GetSlice doesn't modify the original data, so you can use it to retrieve data without worrying about losing any information. This makes it a great choice for operations where you need to inspect or process data without altering it.
Check this out: Golang Types
WriteSlice
The WriteSlice method is a powerful tool for writing slices of samples to a SampleBuffer. It's a straightforward way to add multiple values to the buffer at once.
To use WriteSlice, you'll need to have a SampleBuffer object, which is a type of buffer that stores audio samples. The WriteSlice method takes a slice of samples as input and writes it directly to the buffer.

One thing to keep in mind when using WriteSlice is that it doesn't remove the samples from the buffer after writing them. This means you can reuse the same buffer multiple times, which can be a big memory saver.
The WriteSlice method is part of the SampleBuffer type, which is a key component of Sonic's streaming IO capabilities. By using WriteSlice, you can write slices of samples to the buffer and then retrieve them later using the GetSlice method.
Featured Images: pexels.com

